Module 4
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
● Inheritance provides code reusability (code of any class can be used by
extending that class)
Simple Inheritance
Inheritance and Member Access
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
Superclass variable reference to Subclass object
Superclass variable reference to Subclass object
Super Keyword
Superclass variable reference to Subclass object
Using super to call Superclass Constructors
Using super to access Superclass hidden
members
WAP to create a super class having a variable. Let the variable be initialized to
some value within a constructor. This class should have a method display () to
display the initial value of the variable. Derive a sub class that accesses the
constructor, variable and method of the super class using super keyword.
Multilevel Inheritance
● When a subclass calls super( ), it is
calling the constructor of its
immediate superclass.
● Thus, super( ) always refers to the
superclass immediately above the
calling class.
● This is true even in a multileveled
hierarchy.
● Also, super( ) must always be the
first statement executed inside a
subclass constructor.
Hierarchical Inheritance Example
Method Overriding
● In a class hierarchy, when a method in a subclass has the same name
and type signature as a method in its superclass, then the method in the
subclass is said to override the method in the superclass.
● When an overridden method is called from within its subclass, it will
always refer to the version of that method defined by the subclass.
● The version of the method defined by the superclass will be hidden.
Method Overriding
Method Overriding
● Can we override static method?
○ No, a static method cannot be overridden.
● Why can we not override static method?
○ It is because the static method is bound with class whereas
instance method is bound with an object.
○ Static belongs to the class area, and an instance belongs to the
heap area.
● Can we override java main method?
○ No, because the main is a static method.
Dynamic Method Dispatch
● Method overriding forms the basis for one of Java’s most powerful
concepts: dynamic method dispatch.
● Dynamic method dispatch is the mechanism by which a call to an
overridden method is resolved at run time, rather than compile time.
● Java uses this fact to resolve calls to overridden methods at run time.
Here is how.
● When an overridden method is called through a superclass reference,
Java determines which version of that method to execute based upon
the type of the object being referred to at the time the call occurs.
Dynamic Method Dispatch
● When different types of objects are referred to, different versions of an
overridden method will be called.
● In other words, it is the type of the object being referred to (not the type of
the reference variable) that determines which version of an overridden
method will be executed.
● Therefore, if a superclass contains a method that is overridden by a subclass,
then when different types of objects are referred to through a superclass
reference variable, different versions of the method are executed.
Final Keyword
The keyword final has three uses.
● it can be used to create the equivalent of a named constant.
● The other two uses of final apply to inheritance:
○ Using final to Prevent Overriding
○ Using final to Prevent Inheritance
Java final variable
● If you make any variable as final, you cannot change the value of final variable(It will be
constant).
Using final to Prevent Overriding:
● To disallow a method from being overridden, specify final as a modifier at the
start of its declaration.
● Methods declared as final cannot be overridden.
Using final to Prevent Overriding
● If you make any method as final, you cannot override it.
Using final to Prevent Inheritance
● Sometimes you will want to prevent a class from being inherited.
● To do this, precede the class declaration with final.
● Declaring a class as final implicitly declares all of its methods as final, too.
Using final to prevent Inheritance
Is final method inherited?
● Yes, final method is inherited but you cannot override it
What is blank or uninitialized final variable?
● A final variable that is not initialized at the time of declaration is known as blank final
variable.
● If you want to create a variable that is initialized at the time of creating object and once
initialized may not be changed, it is useful.
● Can we initialize blank final variable?
○ Yes, but only in constructor.
static blank final variable
● A static final variable that is not initialized at the time of declaration is known as static
blank final variable. It can be initialized only in static block.
What is final parameter
● If you declare any parameter as final, you cannot change the value of it.
Abstract Classes
● There are situations in which you will want to define a superclass that
declares the structure of a given abstraction without providing a complete
implementation of every method.
● That is, sometimes you will want to create a superclass that only defines a
generalized form that will be shared by all of its subclasses, leaving it to each
subclass to fill in the details.
● Such a class determines the nature of the methods that the subclasses must
implement.
Abstract Classes
● You can require that certain methods be overridden by subclasses by
specifying the abstract type modifier.
● These methods are sometimes referred to as subclasser responsibility
because they have no implementation specified in the superclass.
● Thus, a subclass must override them—it cannot simply use the version
defined in the superclass. To declare an abstract method, use this general
form:
abstract type name(parameter-list);
Abstract Classes
● Any class that contains one or more abstract methods must also be declared
abstract.
● To declare a class abstract, you simply use the abstract keyword in front of the
class keyword at the beginning of the class declaration.
● There can be no objects of an abstract class. That is, an abstract class cannot be
directly instantiated with the new operator.
● Such objects would be useless, because an abstract class is not fully defined.
● Also, you cannot declare abstract constructors, or abstract static methods.
● Any subclass of an abstract class must either implement all of the abstract
methods in the superclass, or be declared abstract itself.
Static Binding and Dynamic Binding
● Connecting a method call to the method
body is known as binding.
● There are two types of binding
○ Static Binding (also known as Early Binding).
○ Dynamic Binding (also known as Late
Binding).
static binding
● When type of the object is determined at compiled time(by the compiler), it is known as
static binding.
● If there is any private, final or static method in a class, there is static binding.
Dynamic binding
● When type of the object is determined at run-time, it is known as dynamic binding.
object type cannot be determined by the
compiler, because the instance of Dog is also
an instance of Animal.
So compiler doesn't know its type, only its
base type.