 The process by which one class acquires the
properties(data members) and functionalities(methods)
of another class is called inheritance.
 The aim of inheritance is to provide the reusability of
code so that a class has to write only the unique
features and rest of the common properties and
functionalities can be extended from the another class.
 Child Class:
The class that extends the features of another class is
known as child class, sub class or derived class.
 Parent Class:
The class whose properties and functionalities are
used(inherited) by another class is known as parent
class, super class or Base class.
 To inherit a class we use extends keyword. Here
class XYZ is child class and class ABC is parent
class.
 The class XYZ is inheriting the properties and
methods of ABC class.
class XYZ extends ABC
{
}
class Teacher {
String designation = "Teacher";
String collegeName =
"Beginnersbook";
void does()
{ System.out.println("Teaching");
}
}
public class PhysicsTeacher
extends Teacher
{
String mainSubject = "Physics";
public static void main(String
args[])
{
System.out.println(obj.collegeName);
System.out.println(obj.designation);
System.out.println(obj.mainSubject);
obj.does();
}
}
Output:
Beginnersbook
Teacher
Physics
Teaching
 Constructors are not inherited, even though they
have public visibility
 Yet we often want to use the parent's constructor to
set up the "parent's part" of the object
 The super reference can be used to refer to the
parent class, and often is used to invoke the
parent's constructor
 A child’s constructor is responsible for calling the parent’s
constructor
 The first line of a child’s constructor should use the super
reference to call the parent’s constructor
 The super reference can also be used to reference other
variables and methods defined in the parent’s class
 Java supports single inheritance, meaning that a
derived class can have only one parent class
 Multiple inheritance allows a class to be derived
from two or more classes, inheriting the members of
all parents
 Collisions, such as the same variable name in two
parents, have to be resolved
 Java does not support multiple inheritance
 In most cases, the use of interfaces gives us aspects
of multiple inheritance without the overhead
A class inherits properties
from a class which again has
inherits properties.
EXAMPLE
class Shape
{
public void display() {
System.out.println("Inside display");
}
}
class Rectangle extends Shape
{
public void area()
{
System.out.println("Inside area");
}
}
class Cube extends Rectangle
{
public void volume() {
System.out.println("Inside volume");
}
}
public class Tester { public static void main(String[]
arguments) {
Cube cube =newCube();
cube.display();
cube.area();
cube.volume();
}
}
Output
Inside display
Inside area
Inside volume
 A child class can override the definition of an
inherited method in favor of its own
 The new method must have the same signature as
the parent's method, but can have a different body
 The type of the object executing the method
determines which version of the method is invoked
 A parent method can be invoked explicitly using the super
reference
 If a method is declared with the final modifier, it cannot be
overridden
 The concept of overriding can be applied to data and is
called shadowing variables
 Shadowing variables should be avoided because it tends to
cause unnecessarily confusing code
 A child class can override the definition of an
inherited method in favor of its own
 The new method must have the same
signature as the parent's method, but can
have a different body
 The type of the object executing the method
determines which version of the method is
invoked
 A parent method can be invoked explicitly using
the super reference
 If a method is declared with the final modifier, it
cannot be overridden
 The concept of overriding can be applied to data
and is called shadowing variables
 Shadowing variables should be avoided because it
tends to cause unnecessarily confusing code
Class A
{
int i,j;
A(int a,int b)
{
i=a;
j=b;
}
Void show()
{
System.out.println(“i and j:”+i+”
“+j);
}
}
Class B extends A {
B(int a,int b,int c) {
Super(a,b);
K=c;
}
Void show() {
System.out.println(“K:”+k);
}
}
Class override {
Public static void main(strng args[]) {
B subob=new B(1,2,3);
Subob.show();
}
}
OUTPUT:
 An abstract class is a placeholder in a class
hierarchy that represents a generic concept
 An abstract class cannot be instantiated
 We use the modifier abstract on the class header to
declare a class as abstract:
public abstract class Whatever
{
// contents
}
 Single inheritance is damn easy to
understand. When a class extends another
one class only then we call it a single
inheritance. The below flow diagram shows
that class B extends only one class which is
A. Here A is a parent class of B and B would
be a child class of A.
Class A
{
public
void methodA()
{
System.out.println("Base class method");
}
}
Class B extends A
{
public
void methodB()
{
System.out.println("Child class method");
}
public static void main(String args[])
{
B obj = new B(); obj.methodA();
method obj.methodB();
}
}
 Multilevel inheritance refers to a
mechanism in OO technology where one can
inherit from a derived class, thereby making
this derived class the base class for the new
class. As you can see in below flow diagram C
is subclass or child class of B and B is a child
class of A.
Class X
{
public
void methodX()
{
System.out.println("Class X method");
} }
Class Y extends X
{
public void methodY()
{
System.out.println("class Y method");
}
}
Class Z extends Y
{
public
void methodZ()
{
System.out.println("class Z
method");
}
public static void main(String
args[])
{
Z obj = new Z();
obj.methodX();
obj.methodY();
obj.methodZ();
}
}
 Multiple inheritance is a feature of
some object-oriented computer programming
languages in which an object
or class can inherit characteristics and
features from more than one parent object
or parent class.
Class X
{
public void methodX()
{
System.out.println("Class X
method");
}
}
Class Y extends X
{
public void methodY()
{
System.out.println("class Y
method");
}
}
Class Z extends Y
{
public void methodZ()
{
System.out.println("class Z method");
}
public static void main(String args[])
{
Z obj = new Z();
obj.methodX();
obj.methodY();
obj.methodZ();
}
}

Java

  • 2.
     The processby which one class acquires the properties(data members) and functionalities(methods) of another class is called inheritance.  The aim of inheritance is to provide the reusability of code so that a class has to write only the unique features and rest of the common properties and functionalities can be extended from the another class.  Child Class: The class that extends the features of another class is known as child class, sub class or derived class.  Parent Class: The class whose properties and functionalities are used(inherited) by another class is known as parent class, super class or Base class.
  • 3.
     To inherita class we use extends keyword. Here class XYZ is child class and class ABC is parent class.  The class XYZ is inheriting the properties and methods of ABC class. class XYZ extends ABC { }
  • 4.
    class Teacher { Stringdesignation = "Teacher"; String collegeName = "Beginnersbook"; void does() { System.out.println("Teaching"); } } public class PhysicsTeacher extends Teacher { String mainSubject = "Physics"; public static void main(String args[]) { System.out.println(obj.collegeName); System.out.println(obj.designation); System.out.println(obj.mainSubject); obj.does(); } } Output: Beginnersbook Teacher Physics Teaching
  • 5.
     Constructors arenot inherited, even though they have public visibility  Yet we often want to use the parent's constructor to set up the "parent's part" of the object  The super reference can be used to refer to the parent class, and often is used to invoke the parent's constructor
  • 6.
     A child’sconstructor is responsible for calling the parent’s constructor  The first line of a child’s constructor should use the super reference to call the parent’s constructor  The super reference can also be used to reference other variables and methods defined in the parent’s class
  • 7.
     Java supportssingle inheritance, meaning that a derived class can have only one parent class  Multiple inheritance allows a class to be derived from two or more classes, inheriting the members of all parents  Collisions, such as the same variable name in two parents, have to be resolved  Java does not support multiple inheritance  In most cases, the use of interfaces gives us aspects of multiple inheritance without the overhead
  • 8.
    A class inheritsproperties from a class which again has inherits properties. EXAMPLE class Shape { public void display() { System.out.println("Inside display"); } } class Rectangle extends Shape { public void area() { System.out.println("Inside area"); } } class Cube extends Rectangle { public void volume() { System.out.println("Inside volume"); } } public class Tester { public static void main(String[] arguments) { Cube cube =newCube(); cube.display(); cube.area(); cube.volume(); } } Output Inside display Inside area Inside volume
  • 9.
     A childclass can override the definition of an inherited method in favor of its own  The new method must have the same signature as the parent's method, but can have a different body  The type of the object executing the method determines which version of the method is invoked
  • 10.
     A parentmethod can be invoked explicitly using the super reference  If a method is declared with the final modifier, it cannot be overridden  The concept of overriding can be applied to data and is called shadowing variables  Shadowing variables should be avoided because it tends to cause unnecessarily confusing code
  • 11.
     A childclass can override the definition of an inherited method in favor of its own  The new method must have the same signature as the parent's method, but can have a different body  The type of the object executing the method determines which version of the method is invoked
  • 12.
     A parentmethod can be invoked explicitly using the super reference  If a method is declared with the final modifier, it cannot be overridden  The concept of overriding can be applied to data and is called shadowing variables  Shadowing variables should be avoided because it tends to cause unnecessarily confusing code
  • 13.
    Class A { int i,j; A(inta,int b) { i=a; j=b; } Void show() { System.out.println(“i and j:”+i+” “+j); } } Class B extends A { B(int a,int b,int c) { Super(a,b); K=c; } Void show() { System.out.println(“K:”+k); } } Class override { Public static void main(strng args[]) { B subob=new B(1,2,3); Subob.show(); } } OUTPUT:
  • 14.
     An abstractclass is a placeholder in a class hierarchy that represents a generic concept  An abstract class cannot be instantiated  We use the modifier abstract on the class header to declare a class as abstract: public abstract class Whatever { // contents }
  • 15.
     Single inheritanceis damn easy to understand. When a class extends another one class only then we call it a single inheritance. The below flow diagram shows that class B extends only one class which is A. Here A is a parent class of B and B would be a child class of A.
  • 16.
    Class A { public void methodA() { System.out.println("Baseclass method"); } } Class B extends A { public void methodB() { System.out.println("Child class method"); } public static void main(String args[]) { B obj = new B(); obj.methodA(); method obj.methodB(); } }
  • 17.
     Multilevel inheritancerefers to a mechanism in OO technology where one can inherit from a derived class, thereby making this derived class the base class for the new class. As you can see in below flow diagram C is subclass or child class of B and B is a child class of A.
  • 18.
    Class X { public void methodX() { System.out.println("ClassX method"); } } Class Y extends X { public void methodY() { System.out.println("class Y method"); } } Class Z extends Y { public void methodZ() { System.out.println("class Z method"); } public static void main(String args[]) { Z obj = new Z(); obj.methodX(); obj.methodY(); obj.methodZ(); } }
  • 19.
     Multiple inheritanceis a feature of some object-oriented computer programming languages in which an object or class can inherit characteristics and features from more than one parent object or parent class.
  • 20.
    Class X { public voidmethodX() { System.out.println("Class X method"); } } Class Y extends X { public void methodY() { System.out.println("class Y method"); } } Class Z extends Y { public void methodZ() { System.out.println("class Z method"); } public static void main(String args[]) { Z obj = new Z(); obj.methodX(); obj.methodY(); obj.methodZ(); } }