Вы находитесь на странице: 1из 11

Inherita

nce in
Inheritance in java
Inheritance is an important pillar of
OPP(object oriented programming). It
is the mechanism in java by which one
class is allowed to inherit the
features(fields and methods) of
another class.
Important Terminologies
• Super Class: the class whose
features are inherited. Also called as
Base Class or Parent Calss.
• Sub Class: the class that inherits the
other class' features. Also called a
Derived Class, Extended Class, or
Child Class.
Important Terminologies
• Reusability: inheritance supports the
concept of “reusability”, i.e. when we
want to create a new class and there
is already a class that includes some
of the codes that we want, we can
derive our new class from the existing
class and just reuse the existing
fields and methods.
Inheritance in java
The keyword for inheritance is
extends.
Syntax:
class <SubClass> extends <ParentClass>
{
//methods and fields
}
Types of Inheritance
• Single Inheritance: a sub class
inherits one super class.

*SEE EXAMPLE*
Types of Inheritance
• Multilevel Inheritance: a sub class is
also a super class of another sub
class, and so on... like
Grandparent,parent and child.
Note that the child class cannot directly
access the grandparent's features.

*SEE EXAMPLE*
Types of Inheritance
• Hierarchical Inheritance: one super
class for more than one sub
classes...

*SEE EXAMPLE*
Types of Inheritance
Note: the following types of inheritance
only works through interfaces.
interface: used to provide total
abstraction. That means all the
methods in interface are declared with
empty body and are public and all
fields are public, static and final by
default.
Types of Inheritance
• Multiple Inheritance: one sub class
can have more than one super
classes...

*SEE EXAMPLE*
Types of Inheritance
• Hybrid Inheritance: it is a
combination of two or more of the first
4 types of inheritance.

*SEE EXAMPLE*

Вам также может понравиться