Super Class: The class whose
features are inherited is known as
super class(or a base class or a
SUPERCLASSES AND SUBCLASSES INTRO parent class)
Inheritance allow code to be reused, without Inheritance is an important pillar of OOP’s. Sub Class: The class that inherits
copying it into the definitions of the derived It is the mechanism in java by which one TERMINOLOGIES the other class is known as sub
classes. Inheritance have many types such class is allow to inherit the features(fields class(or a derived class, extended
and methods) of another class. class, or child class). The subclass
The keyword extends tells the compiler that the as the following:
can add its own fields and methods
Circle class extends the GeometricObject class,
in addition to the superclass fields
thus inheriting the methods getColor, setColor,
and methods.
isFilled, setFilled, and toString.
TYPES OF INHERITANCE
Reusability: Inheritance supports
Java, does not allow multiple inheritance. CAUTION WITH INHERITANCE the concept of “reusability”, i.e.
A Java class may inherit directly from only
Inheritance allow code to be reused, without
when we want to create a new
one superclass, i.e. single inheritance. Inheritance is used to model the is-a relationship copying it into the definitions of the derived class and there is already a class
Do not blindly extend a class just for the sake of classes. Inheritance have many types such that includes some of the code
reusing methods. as the following: that we want, we can derive our
it makes no sense for a Tree class to extend a new class from theexisting class.
Person class, even though they share common
properties such as height and weight.
CONSTRUCTOR CHAINING
INHERITANCE Multiple Hybrid Multi-level Hierarchical Single
Inheritance Inheritance Inheritance Inheritance
Inheritance
Use of super with
Constructing an instance of a class invokes variables
EXAMPLE
all the superclasses constructors along the USING THE KEYWORD SUPER
inheritance chain Use of super with
The super keyword in java is used as a USAGE
methods
reference to variable and methods defined in
parent class
Use of super with
constructors
OVERRIDING VS. OVERLOADING HOW TO CODE INHERITANCE
OVERRIDING
overloaded methods can be either in the same class class derived-class
or different classes related by inheritance.
extends base-class { KEY WORD The keyword used for
overloaded methods have the same name but
inheritance is extends.
adifferent parameter list. //methods and fields
Overridden methods are
}
in different classes related
by inheritance.
Overridden methods have
OVERLOADING
the same signature and
return type