You are on page 1of 21

INHERITANCE

Extending A Class

Navneet Sandhu
Assistant Professor
Sri Guru Gobind Singh College, Sector – 26,
Chandigarh
Introduction
 Reusability is an important aspect of OOP paradigm.
 Java classes can be reused:
 By creating new classes from existing classes
 Reusing the properties of existing classes
 The mechanism of deriving a new class from an old
one is called inheritance.
 The existing class is known as the base class or super
class or parent class.
 The new class is called the subclass or derived class
or child class.
 Inheritance allows subclasses to inherit all the
variables and methods of their parent classes.
NK SGGS26
Inheritance Basics
 Inheritance may take different forms:
 Single Inheritance (only one super class)
 Multiple Inheritance (several super classes)
 Hierarchical Inheritance (one super class, many
subclasses)
 Multilevel Inheritance (derived from a derived class)
 Java does not directly implement multiple
inheritance. However, this concept is implemented
using a secondary inheritance path in the form of
interfaces.

NK SGGS26
Inheritance Basics Contd…
 Syntax or general form:
class subclassname extends superclassname
{
//body of the class
} Name of Name of
the derived Keyword the base
class class

 The keyword extends signifies that the properties of the


class superclassname are extended to the class
subclassname.
 The subclass will contain its own variables and methods
as well as those of the superclass.
 This allows to add some properties to an existing class
without actually modifying it.
NK SGGS26
Types of Inheritance
Single Inheritance Hierarchical Inheritance

A
Super or
Base or A Super Class

Parent Class

Sub or
B Derived or
Child Class
B C D
Sub Classes
class A
class A { }
class B extends A
{ } { }
class B extends A class C extends A
{ } { }
class D extends A
{ }
NK SGGS26
Types of Inheritance
Multilevel Inheritance Multiple Inheritance

class A
{ } A A B
class B extends A
{ }
class C extends B
{ } B C
*Java does not support multiple
inheritance
class A
C { }
class B
{ }
class C cannot extend both A & B
{ } NK SGGS26
Subclass Constructor
 A subclass constructor is used to construct the
instance variables of both the subclass and the
super class.
 The subclass constructor uses the keyword super to
invoke the constructor method of the super class.
 Conditions for using super keyword are:
1. super may only be used within a subclass constructor.
2. Call to the superclass constructor must appear as the
first statement within the subclass constructor.
3. Parameters in the super call must match the order and
type of the instance variables declared in the superclass.
NK SGGS26
Method Overriding
 A method defined in a super class is inherited by its
subclass and is used by the objects created by the
subclass.
 Method inheritance enables to define and use
methods repeatedly in subclasses without having to
define the methods again in subclass.
 Sometimes it may be required for an object to
respond to the same method but have different
behavior when that method is called.
 So the method defined in the super class must be
overridden.
NK SGGS26
Method Overriding Contd…

 Method overriding is implemented by defining a


method in the subclass that has the same name,
same arguments and same return type as a method
in the super class.
 Then, when that method is called, the method
defined in the subclass is invoked and executed
instead of the one in the super class. This is known as
method overriding.

NK SGGS26
Final variables and Methods
 By default, all methods and variables can be overridden in
subclasses.
 To prevent the subclasses from overriding the members
of the superclass, declare them using the keyword final as
a modifier. For e.g.
final int SIZE = 100;
final void display ( ) { ……. }
 Making a method final ensures that the functionality
defined in this method will never be altered by the
subclass.
 Value of a final variable cannot be changed.
 Final variables behave like class variables and do not take
space on individual objects of the class. NK SGGS26
Final Classes
 To prevent a class from being further subclassed, declare
it as final.
 A class that cannot be subclassed is called a final class.
 For e.g. final class classname { ……… }
final subclassname extends superclassname
{
...
...
}
 Any attempt to inherit final classes will cause an error.

NK SGGS26
• A symbolic constant
variable • Value cannot be changed using
an assignment statement

final method • Cannot be overridden

class • Cannot be inherited

Fig. Using final keyword with variables, methods and classes


NK SGGS26
Finalizer Methods
 Initialization - A constructor is used to initialize an object
when it is declared.
 Similarly, Java supports a concept called finalization.
 Finalization – just opposite to initialization
 Java runtime is an automatic garbage collecting system;
automatically frees up the memory resources used by the
objects.
 Objects may also hold other resources such as window
system fonts. Garbage collector cannot free such
resources.
 To free resources such as window system fonts, use a
finalizer method (similar to destructors in C++)
NK SGGS26
Finalizer Methods Contd …

 finalize ( ) – can be added to any class


 Used to perform cleanup operation in Java; executed when any
object is available for garbage collection.
 Called whenever space for an object is to be reclaimed.
 finalize( ) should explicitly define the tasks to be performed.
 No parameters passed to finalize ( )
 Uses protected access specifier
For e.g.
class GarbageCollect
{
protected void finalize ( ) throws Throwable
{
System.out.println(“Finalized…!”);
}
NK SGGS26
}
Abstract Methods and Classes
 Using final ensures that the method is not redefined in a
subclass i.e. prevents overriding
 abstract keyword does exactly opposite i.e. indicates that
the method must always be redefined in a subclass, thus
making overriding compulsory.
 For e.g.
abstract class Shape
{


abstract void draw ( );


}

NK SGGS26
Abstract Methods and Classes
 When a class contains one or more abstract methods, it
should be declared abstract.
 Following conditions must be satisfied while using
abstract:
 Cannot use abstract classes to instantiate objects directly. For
e.g.
Shape s1 = new Shape ( );
is illegal because Shape class is an abstract class.
 Abstract methods of an abstract class must be defined in its
subclass.
 Not permitted to declare abstract constructors or abstract static
methods.

NK SGGS26
Dynamic Method Dispatch
 Helps in implementing runtime polymorphism in Java

 Is a technique which helps in resolving a call to an


overridden method at runtime

 Whenever an overridden method is called by a reference


object (only declared but not instantiated), then Java
chooses the method of that class whose object is assigned
to reference object of super/base class.

 This selection is made at runtime.

NK SGGS26
Dynamic Method Dispatch Contd…

 Upcasting: If a super class reference variable refers to


a subclass object
 Java uses upcasting to resolve calls to overridden
methods at runtime.
 Advantages of Dynamic Method Dispatch:
1. Supports overriding of methods which is necessary for
runtime polymorphism
2. Allows a class to specify methods that will be common to
all of its derivatives, while allowing subclasses to define the
specific implementation of some or all of its methods.
NK SGGS26
Dynamic Method Dispatch Contd…

MySuperClass
extends Class having main( ) method
void display( )
MySuperClass obj;
MySubClass1 obj1 = new MySubClass1 ( );
Sub Class 1 executes obj = obj1;
obj.display ( );
MySubClass1
extends
void display( ) MySuperClass obj;
MySubClass2 obj2= new MySubClass2 ( );
executes obj = obj2;
Sub Class 2
obj.display ( );
MySubClass2

void display( )

NK SGGS26
Garbage Collection
 The objects that occupy memory but are not required are
termed as garbage.
 The process of removing such objects is called garbage
collection.
 When an object of a class is instantiated, memory is
allocated to that object.
 If an object is no longer required, it should be removed
from the memory so that memory can be allocated to
other objects/variables
 JRE detects such objects and removes them
automatically.

NK SGGS26
NK SGGS26

You might also like