Anda di halaman 1dari 21

SKILLS FACTORY

CHAPTER 6
EXTENDING CLASSES &
INHERITANCE
Inheritance
The mechanism of deriving a new class from
an old one is called inheritance.
The old class is known as the base
class/parent class/super class.
The new one is called the derived class/child
class/subclass.
Functionality of
Inheritance
Java supports reusability.
This is done by creating new classes, reusing
the properties of existing ones.
Subclass can be a super class for another
subclass.
A super class object can refer a subclass
object.
Private members cannot be inherited.
Types of Inheritance
Single Inheritance (Only one super class)
Multiple Inheritance (Several super classes)
Hierarchical Inheritance (One Super class, many
subclass)
Multilevel Inheritance (Derived from a derived
class)
Single Inheritance Hierarchical Inheritance
A
A

B B C D

Multi-level Inheritance Multiple Inheritance

A
A B

B
C
C
Defining a subclass
General form:
class subclassname extends superclassname
{
variables declaration;
methods declaration;
}
Subclass will now contain its own variables & methods
as well as those of the super class.
Hierarchical
Inheritance
Multiple classes inherits from a single
class.
There is one super class and multiple sub
classes.
Multi-level Inheritance
Derives class as a super class.

A Grandfather

B Father

C
Child
This chain ABC known as inheritance path.
Multiple Inheritance
Java doesnt directly implement multiple
Inheritance.
It results in unwanted complexity when
further extending the class.
New object oriented languages, such as
SmallTalk, Java, C# do not support multiple
inheritance.
But it is supported in C++.
Use of super keyword
It is used inside a sub-class method definition
to call a method defined in the super class.
Private methods of the super class cannot be
called.
Only public & protected methods can called
by the super keyword.
It is also used by class constructors to invoke
constructors of its parent class.
Note while using super
Super may only be used within a subclass method or
constructor.
The call to a super class method/constructor must
appear as the first statement within the subclass
constructor.
The parameter in the super call must match the order
& type of the instance variable declared in the super
class.
Using super
General Form:
To call the super class constructor/method.
super (parameter list);
Access the member of the super class when it
is hidden by the member of the subclass.
super.member;
Overriding
When the methods in the super class &
subclass have the same name, return type
& same argument.
Then the subclass method will override
the super class method.
Polymorphism in
Inheritance
Dynamic Method Dispatch
DMD is a mechanism by which a call to an
overridden is resolved at runtime rather than
compile time.
It is a prime characteristic of OOP.
When an overridden method is called by a
reference, java determines which version of that
method to execute based on the type of object it
refer to.
Supports runtime polymorphism.
Interface
Itspecifies what to do but not how to do.
Syntactically interface is similar to class but lacks
instance variable, methods are declared without
code/body.
Any number of classes can implement an interface.
One class can implement any number of interfaces.
Interface supports one interface multiple method
definition aspect of polymorphism.
When access is public all the member of the interface
are public.
Interface General
Form
interface interface_name
{
variables declaration;
methods declaration;
}
Variable declaration:
static final datatype variable_name=value;
Method declaration:
returntype methodname1(Parameter list);
Extending Interface
Interfaces can also be extended.
It can be sub interfaced from other interfaces.
The new interface will inherit all the members of the
super interface in the manner similar to subclass.
Syntax:
interface name2 extends name1
{
body of name2
}
Note
While interfaces are allowed to extend other
interfaces, sub interfaces cannot define the
methods in the super interfaces.
An interface cannot extend classes.
Implementing interfaces
Syntax 1:
class class_name implements interface_name
{
body of the class
}
Syntax 2:
class class_name extends superclass implements
interface1, interface2,.
{
body of the class
}
Partial Implementation
If a class includes an interface but doesnt
fully implement the methods defined by that
interface.
Then the class must be declared as abstract.
For further details & doubts
Mail me : skillsfactory.viswa@gmail.com

Anda mungkin juga menyukai