JAVA-INHERITANCE
Elizabeth Alexander
Hindustan University
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
● Inheritance in java is a mechanism in which one object acquires all the properties
and behaviors of parent object.
● The idea behind inheritance in java is that you can create new classes that are built
upon existing classes. When you inherit from an existing class, you can reuse
methods and fields of parent class, and you can add new methods and fields also.
INHERITANCE Cont...
Why use inheritance in java?
● For Method Overriding (so runtime polymorphism can be achieved).
● For Code Reusability.
SYNTAX OF JAVA INHERITANCE
class Super {
.....
.....
}
class Sub extends Super {
.....
.....
}
● The extends keyword indicates that a new class is derived from an existing
class. The meaning of "extends" is to increase the functionality.
INHERITANCE TERMS
● superclass, base class, parent class: terms to describe the parent in the
relationship, which shares its functionality
● subclass, derived class, child class: terms to describe the child in the
relationship, which accepts functionality from its parent
● extend, inherit, derive: become a subclass of another class
THE Super KEYWORD
● The super keyword is similar to this keyword. Following are the scenarios where
the super keyword is used.
■ It is used to differentiate the members of superclass from the
members of subclass, if they have same names.
■ It is used to invoke the superclass constructor from subclass.
● Differentiating the Members
● If a class is inheriting the properties of another class. And if the members of the
superclass have the names same as the sub class, to differentiate these variables
we use super keyword as shown below.
super.variable
super.method();
INVOKING SUPERCLASS CONSTRUCTOR
● If a class is inheriting the properties of another class, the subclass automatically
acquires the default constructor of the superclass. But if you want to call a
parameterized constructor of the superclass, you need to use the super keyword
as shown below.
super(values);
JAVA INHERITANCE EXAMPLE
TYPES OF INHERITANCE
● Three types of inheritance in java: single, multilevel and hierarchical.
● In java programming, multiple and hybrid inheritance is supported through
interface only.
TYPES OF INHERITANCE Cont...
Inheritance in Java

Inheritance in Java

  • 1.
  • 2.
    INHERITANCE ● A parent-childrelationship 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 ● Inheritance in java is a mechanism in which one object acquires all the properties and behaviors of parent object. ● The idea behind inheritance in java is that you can create new classes that are built upon existing classes. When you inherit from an existing class, you can reuse methods and fields of parent class, and you can add new methods and fields also.
  • 3.
    INHERITANCE Cont... Why useinheritance in java? ● For Method Overriding (so runtime polymorphism can be achieved). ● For Code Reusability.
  • 5.
    SYNTAX OF JAVAINHERITANCE class Super { ..... ..... } class Sub extends Super { ..... ..... } ● The extends keyword indicates that a new class is derived from an existing class. The meaning of "extends" is to increase the functionality.
  • 6.
    INHERITANCE TERMS ● superclass,base class, parent class: terms to describe the parent in the relationship, which shares its functionality ● subclass, derived class, child class: terms to describe the child in the relationship, which accepts functionality from its parent ● extend, inherit, derive: become a subclass of another class
  • 7.
    THE Super KEYWORD ●The super keyword is similar to this keyword. Following are the scenarios where the super keyword is used. ■ It is used to differentiate the members of superclass from the members of subclass, if they have same names. ■ It is used to invoke the superclass constructor from subclass. ● Differentiating the Members ● If a class is inheriting the properties of another class. And if the members of the superclass have the names same as the sub class, to differentiate these variables we use super keyword as shown below. super.variable super.method();
  • 8.
    INVOKING SUPERCLASS CONSTRUCTOR ●If a class is inheriting the properties of another class, the subclass automatically acquires the default constructor of the superclass. But if you want to call a parameterized constructor of the superclass, you need to use the super keyword as shown below. super(values);
  • 9.
  • 10.
    TYPES OF INHERITANCE ●Three types of inheritance in java: single, multilevel and hierarchical. ● In java programming, multiple and hybrid inheritance is supported through interface only.
  • 11.