java inheritance -...

20
Java Inheritance

Upload: others

Post on 04-Jul-2020

6 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Java Inheritance - rifatshahriyar.github.iorifatshahriyar.github.io/files/CSE107/Old/Java-Inheritance.pdf · •Same inheritance concept of C++ in Java with some modifications •In

Java

Inheritance

Page 2: Java Inheritance - rifatshahriyar.github.iorifatshahriyar.github.io/files/CSE107/Old/Java-Inheritance.pdf · •Same inheritance concept of C++ in Java with some modifications •In

Inheritance

• Same inheritance concept of C++ in Java with some modifications

• In Java,

– One class inherits the other using extends keyword

– The classes involved in inheritance are known as superclass and subclass

– Multilevel inheritance but no multiple inheritance

– There is a special way to call the superclass’s constructor

– There is automatic dynamic method dispatch

2Prepared By - Rifat Shahriyar

Page 3: Java Inheritance - rifatshahriyar.github.iorifatshahriyar.github.io/files/CSE107/Old/Java-Inheritance.pdf · •Same inheritance concept of C++ in Java with some modifications •In

Simple Inheritance

3Prepared By - Rifat Shahriyar

Page 4: Java Inheritance - rifatshahriyar.github.iorifatshahriyar.github.io/files/CSE107/Old/Java-Inheritance.pdf · •Same inheritance concept of C++ in Java with some modifications •In

Practical Example

4Prepared By - Rifat Shahriyar

Page 5: Java Inheritance - rifatshahriyar.github.iorifatshahriyar.github.io/files/CSE107/Old/Java-Inheritance.pdf · •Same inheritance concept of C++ in Java with some modifications •In

Superclass variable reference to Subclass object

5Prepared By - Rifat Shahriyar

Page 6: Java Inheritance - rifatshahriyar.github.iorifatshahriyar.github.io/files/CSE107/Old/Java-Inheritance.pdf · •Same inheritance concept of C++ in Java with some modifications •In

Using super

6Prepared By - Rifat Shahriyar

Page 7: Java Inheritance - rifatshahriyar.github.iorifatshahriyar.github.io/files/CSE107/Old/Java-Inheritance.pdf · •Same inheritance concept of C++ in Java with some modifications •In

Using super

7Prepared By - Rifat Shahriyar

Page 8: Java Inheritance - rifatshahriyar.github.iorifatshahriyar.github.io/files/CSE107/Old/Java-Inheritance.pdf · •Same inheritance concept of C++ in Java with some modifications •In

Using super

8Prepared By - Rifat Shahriyar

Page 9: Java Inheritance - rifatshahriyar.github.iorifatshahriyar.github.io/files/CSE107/Old/Java-Inheritance.pdf · •Same inheritance concept of C++ in Java with some modifications •In

Multilevel Inheritance

9

Inside X's constructorInside Y's constructorInside Z's constructor

Prepared By - Rifat Shahriyar

Page 10: Java Inheritance - rifatshahriyar.github.iorifatshahriyar.github.io/files/CSE107/Old/Java-Inheritance.pdf · •Same inheritance concept of C++ in Java with some modifications •In

Method Overriding

10Prepared By - Rifat Shahriyar

Page 11: Java Inheritance - rifatshahriyar.github.iorifatshahriyar.github.io/files/CSE107/Old/Java-Inheritance.pdf · •Same inheritance concept of C++ in Java with some modifications •In

Dynamic Method Dispatch

11Prepared By - Rifat Shahriyar

Page 12: Java Inheritance - rifatshahriyar.github.iorifatshahriyar.github.io/files/CSE107/Old/Java-Inheritance.pdf · •Same inheritance concept of C++ in Java with some modifications •In

Abstract Class

• abstract class A

• contains abstract method abstract method f()

• No instance can be created of an abstract class

• The subclass must implement the abstract method

• Otherwise the subclass will be a abstract class too

12Prepared By - Rifat Shahriyar

Page 13: Java Inheritance - rifatshahriyar.github.iorifatshahriyar.github.io/files/CSE107/Old/Java-Inheritance.pdf · •Same inheritance concept of C++ in Java with some modifications •In

Abstract Class

13Prepared By - Rifat Shahriyar

Page 14: Java Inheritance - rifatshahriyar.github.iorifatshahriyar.github.io/files/CSE107/Old/Java-Inheritance.pdf · •Same inheritance concept of C++ in Java with some modifications •In

Anonymous Subclass

14Prepared By - Rifat Shahriyar

Page 15: Java Inheritance - rifatshahriyar.github.iorifatshahriyar.github.io/files/CSE107/Old/Java-Inheritance.pdf · •Same inheritance concept of C++ in Java with some modifications •In

Using final with Inheritance

15

To prevent overriding

To prevent inheritance

Prepared By - Rifat Shahriyar

Page 16: Java Inheritance - rifatshahriyar.github.iorifatshahriyar.github.io/files/CSE107/Old/Java-Inheritance.pdf · •Same inheritance concept of C++ in Java with some modifications •In

Object Class

• There is one special class, Object, defined by Java

• All other classes are subclasses of Object

• That is, Object is a superclass of all other classes

• This means that a reference variable of type Object can refer to an object of any other class

• Also, since arrays are implemented as classes, a variable of type Object can also refer to any array

16Prepared By - Rifat Shahriyar

Page 17: Java Inheritance - rifatshahriyar.github.iorifatshahriyar.github.io/files/CSE107/Old/Java-Inheritance.pdf · •Same inheritance concept of C++ in Java with some modifications •In

Object’s toString()

• The toString( ) method returns a string that contains a description of the object on which it is called

• Also, this method is automatically called when an object is output using println()

• Many classes override this method

• Doing so allows them to provide a description specifically for the types of objects that they create

17Prepared By - Rifat Shahriyar

Page 18: Java Inheritance - rifatshahriyar.github.iorifatshahriyar.github.io/files/CSE107/Old/Java-Inheritance.pdf · •Same inheritance concept of C++ in Java with some modifications •In

Object’s toString()

18Prepared By - Rifat Shahriyar

Page 19: Java Inheritance - rifatshahriyar.github.iorifatshahriyar.github.io/files/CSE107/Old/Java-Inheritance.pdf · •Same inheritance concept of C++ in Java with some modifications •In

Object’s equals() and hashCode()

• == is a reference comparison, whether both variables refer to the same object

• Object’s equals() method does the same thing

• String class override equals() to check contents

• If you want two different objects of a same class to be equal then you need to override equals() and hashCode() methods

– hashCode() needs to return same value to work properly as keys in Hash data structures

19Prepared By - Rifat Shahriyar

Page 20: Java Inheritance - rifatshahriyar.github.iorifatshahriyar.github.io/files/CSE107/Old/Java-Inheritance.pdf · •Same inheritance concept of C++ in Java with some modifications •In

Object’s equals() and hashCode()

20Prepared By - Rifat Shahriyar