1. Inheritance: Definition
Inheritance: A parent-child
relationship between classes allows
sharing of the behavior of the parent
class into its child classes
one of the major benefits of object-
oriented programming (OOP) is this code
sharing between classes through
inheritance
Child class can add new behavior or
override existing behavior from parent
Reusability- building new components
by utilising existing components- is yet
another important aspect of OO
paradigm.
22-03-2018SALEEMQAISAR
1
Parent
Child
2. Inheritance terms
subclass, derived class, child class: Terms to describe the child in
the relationship, which accepts functionality from its parent.
class SubClassName extends SuperClassName
{
fields declaration;
methods declaration;
}
The keyword “extends” signifies that the properties of super class
are extended to the subclass. That means, subclass contains its own
members as well of those of the super class. This kind of situation
occurs when we want to enhance properties of existing class
without actually modifying it.
22-03-2018SALEEMQAISAR
2
3. Syntax Of Inheritance
public class Class extends ParentClass
{
//new variable or methods here
}
Example:
22-03-2018SALEEMQAISAR
3
4. superclass, base class, parent class: terms to describe the
parent in the relationship, which shares its functionality
extend, inherit, derive: Become a subclass of another class.
22-03-2018SALEEMQAISAR
4
6. Single inheritance
Single Inheritance is the simple
inheritance of all, When a class
extends another class(Only one
class) then we call it as Single
inheritance. The below diagram
represents the single inheritance
in java where Class B extends
only one class Class
A. Here Class B will be the Sub
class and Class A will be one and
only Super class.
22-03-2018SALEEMQAISAR
6
7. Multiple inheritance
Multiple Inheritance is nothing
but one class extending more tha
n one class. Multiple Inheritance
is basically not supported by
many Object Oriented
Programming languages such
as Java, Small Talk, C# etc.. (C++
Supports Multiple Inheritance). As
the Child class has to manage the
dependency of more than
one Parent class. But you can
achieve multiple inheritance in
Java using Interfaces.
22-03-2018SALEEMQAISAR
7
8. Multi-Level inheritance
In Multilevel Inheritance a
derived class will be inheriting a
parent class and as well as the
derived class act as the parent
class to other class. As seen in
the below diagram. Class B
inherits the property of Class
A and again Class B act as a
parent for Class C. In Short Class
A parent for Class B and Class
B parent for Class C.
22-03-2018SALEEMQAISAR
8
9. Hierarchical inheritance
In Hierarchical
inheritance one parent
class will be inherited
by many sub classes.
As per the below
example Class A will be
inherited by Class B,
Class C and Class
D. Class A will be acting
as a parent class
for Class B, Class
C and Class D.
22-03-2018SALEEMQAISAR
9
10. Hybrid Inheritance
Hybrid Inheritance is the
combination of both Single
and Multiple Inheritance.
Again Hybrid inheritance is
also not directly supported
in Java only through
interface we can achieve
this. Flow diagram of the
Hybrid inheritance will look
like below. As you can Class
A will be acting as
the Parent class for Class B &
Class C and Class B & Class
C will be acting
as Parent for Class D.
22-03-2018SALEEMQAISAR
10
11. Access modifiers
Java provides a number of access modifiers to set access
levels for classes, variables, methods, and constructors. The
four access levels are:
Default Access Modifier - No Keyword
Private Access Modifier - Private
Public Access Modifier - Public
Protected Access Modifier - Protected
22-03-2018SALEEMQAISAR
11
12. Default - No Keyword
Default access modifier means we do not explicitly declare an
access modifier for a class, field, method, etc.
A variable or method declared without any access control
modifier is available to any other class in the same package.
The fields in an interface are implicitly public static final and
the methods in an interface are by default public.
Example:
Variables and methods can be declared without any modifiers, as
in the following examples −
String version = "1.5.1";
boolean processOrder()
{ return true;
}
22-03-2018SALEEMQAISAR
12
13. Access Modifier - Private
Methods, variables, and constructors that are declared private
can only be accessed within the declared class itself.
Private access modifier is the most restrictive access level.
Class and interfaces cannot be private.
Private access modifier is the most restrictive access level.
Class and interfaces cannot be private.
Using the private modifier is the main way that an object
encapsulates itself and hides data from the outside world.
22-03-2018SALEEMQAISAR
13
14. Example
public class Logger
{
private String format;
public String getFormat()
{
return this.format;
}
public void setFormat(String format)
{
this.format = format;
}
}
22-03-2018SALEEMQAISAR
14
15. Access Modifier - Public
A class, method, constructor, interface, etc. declared public
can be accessed from any other class. Therefore, fields,
methods, blocks declared inside a public class can be accessed
from any class belonging to the Java Universe.
However, if the public class we are trying to access is in a
different package, then the public class still needs to be
imported. Because of class inheritance, all public methods and
variables of a class are inherited by its subclasses.
Example:
public static void main(String[] arguments) {
// ...
}
22-03-2018SALEEMQAISAR
15
16. Access Modifier - Protected
Variables, methods, and constructors, which are declared
protected in a superclass can be accessed only by the
subclasses in other package or any class within the package of
the protected members' class.
The protected access modifier cannot be applied to class and
interfaces. Methods, fields can be declared protected,
however methods and fields in a interface cannot be declared
protected.
Protected access gives the subclass a chance to use the helper
method or variable, while preventing a nonrelated class from
trying to use it.
22-03-2018SALEEMQAISAR
16
17. Example
The following parent class uses protected access control, to
allow its child class override openSpeaker() method −
class AudioPlayer
{
protected boolean openSpeaker(Speaker sp)
{
// implementation details
}
}
class StreamingAudioPlayer { boolean openSpeaker(Speaker sp)
{ // implementation details } }
22-03-2018SALEEMQAISAR
17