Professional Documents
Culture Documents
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
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…
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
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
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