0% found this document useful (0 votes)
2 views23 pages

Java Inheritance Concepts Explained

Uploaded by

Rajeshwari K S
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views23 pages

Java Inheritance Concepts Explained

Uploaded by

Rajeshwari K S
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd

Module-3

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)

2
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

4
5
6
super( ) must always be the first statement executed inside a
subclass’ constructor

7
8
9
Multilevel Inheritance

Inside X's constructor


Inside Y's
constructor Inside
Z's constructor

10
11
Dynamic Method
Dispatch

For practical example


please refer to
[Link]

12
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

13
Abstract
Class

14
15
Using final with Inheritance

To prevent
overriding

To prevent
inheritance

16
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

17
Local Variable Type Inference and Inheritance

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
18
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

19
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

20
21
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

22
23

You might also like