Java
Inheritance
Inheritance
• Same inheritance concept of C++ in Java with some
modifications
– 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
• Inheritance provides code reusability (code of any
class can be used by extending that class)
Prepared By - Rifat Shahriyar 2
Simple Inheritance
Prepared By - Rifat Shahriyar 3
Inheritance and Member Access
• A class member that has been
declared as private will remain
private to its class
• It is not accessible by any code
outside its class, including
subclasses
Prepared By - Rifat Shahriyar 4
Practical Example
Prepared By - Rifat Shahriyar 5
Superclass variable reference to
Subclass object
Prepared By - Rifat Shahriyar 6
Constructors are not inherited
• Constructors are not members, so they are not
inherited by subclasses
– the constructor of the superclass can be invoked from the
subclass
Prepared By - Rifat Shahriyar 7
Using super to call Superclass Constructors
super( ) must always be the
first statement executed inside
a subclass’ constructor
Prepared By - Rifat Shahriyar 8
Using super to call Superclass
Constructors
Prepared By - Rifat Shahriyar 9
Using super to access Superclass
hidden members
Prepared By - Rifat Shahriyar 10
Multilevel Inheritance
Inside X's constructor
Inside Y's constructor
Inside Z's constructor
Prepared By - Rifat Shahriyar 11
Method Overriding
Prepared By - Rifat Shahriyar 12
Dynamic Method Dispatch
For practical example please
refer to [Link]
Prepared By - Rifat Shahriyar 13
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
Prepared By - Rifat Shahriyar 14
Abstract Class
For practical example please
refer to [Link]
Prepared By - Rifat Shahriyar 15
Anonymous Subclass
Prepared By - Rifat Shahriyar 16
Using final with Inheritance
To prevent overriding
To prevent inheritance
Prepared By - Rifat Shahriyar 17
Local Variable Type Inference and
Inheritance
• A superclass reference can refer to a derived class
object in Java
• When using local variable type inference, the
inferred type of a variable is based on the declared
type of its initializer
– Therefore, if the initializer is of the superclass type, that
will be the inferred type of the variable
– It does not matter if the actual object being referred to by
the initializer is an instance of a derived class
Prepared By - Rifat Shahriyar 18
Local Variable Type Inference and
Inheritance
For detail example please refer to
[Link]
The inferred type is determined by the return type of getObject( ),
not by the actual type of the object obtained. Thus, all three
variables will be of type A
Prepared By - Rifat Shahriyar 19
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
Prepared By - Rifat Shahriyar 20
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
Prepared By - Rifat Shahriyar 21
Object’s toString()
Prepared By - Rifat Shahriyar 22
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
Prepared By - Rifat Shahriyar 23
Object’s equals() and hashCode()
Prepared By - Rifat Shahriyar 24