By
VINOTH R
St.Joseph’s college(Arts & Science)
Kovur, Chennai
• The mechanism of deriving a new class from an old
class is called inheritance.
• The old class is known as the Base class or Parent
class
• The new class is called the Sub class or Derived class
or Child class.
• The inheritance allows subclasses to inherit all the
variables and methods of their parent classes.
• Syntax
• The extends keyword indicates that making a
new class that derives from an existing class.
class Subclass-name extends Superclass-name
{
//methods and fields
}
As displayed in the figure
• Programmer is the subclass and Employee is the superclass.
• Relationship between two classes is Programmer IS-A
Employee.
• It means that Programmer is a type of Employee.
class Employee
{
float salary=40000;
}
class Programmer extends Employee
{
int bonus=10000;
public static void main(String args[])
{
Programmer p=new Programmer();
System.out.println("Programmer salary is:"+p.salary);
System.out.println("Bonus of Programmer is:"+p.bonus);
} }
OUTPUT: Programmer salary is:40000.0
Bonus of programmer is:10000
• On the basis of class, there can be three types of
inheritance in java
1. Single
2. Multilevel
3. Hierarchical
• Multiple and hybrid inheritance is supported through
interface only (Will see in Unit 3).
• One sub class is derived from one super class
• From the below diagram class A serves as a Super class or
parent class
• Class B serves as a Base class or Sub class.
class Animal
{
void eat()
{
System.out.println("eating...");
} }
class Dog extends Animal
{
void bark()
{
System.out.println("barking...");
} }
Class Dog inherits the properties of
Class Animal.
class TestInheritance
{
public static void main(String args[])
{
Dog d=new Dog();
d.bark();
d.eat();
}}
OUTPUT:
barking...
eating...
Animal
Void eat()
Dog
Void bark()
Void eat()
• In Multilevel inheritance there is a concept of grandparent
class.
• From the Below diagram then class C inherits class B and
class B inherits class A
• Which means B is a parent class of C and A is a parent class
of B.
• So in this case class C is implicitly inheriting the properties
and method of class A along with B that’s what is called
multilevel inheritance.
class Animal
{
void eat()
{
System.out.println("eating...");
}}
class Dog extends Animal
{
void bark()
{
System.out.println("barking...");
}}
class BabyDog extends Dog
{
void weep()
{
System.out.println("weeping...");
}}
class TestInheritance2
{
public static void main(String args[])
{
BabyDog d=new BabyDog();
d.weep();
d.bark();
d.eat();
}}
Output:
weeping...
barking...
eating...
Animal
Void eat()
Dog
Void bark()
Void eat()
BabyDog
void weep()
void bark()
Void eat()
• A class has more than one child classes (sub classes).
• More than one child classes have the same parent
class.
• Class A is a parent class of class B and class C and
class C.
class Animal
{
void eat()
{
System.out.println("eating...");
} }
class Dog extends Animal
{
void bark()
{
System.out.println("barking...");
} }
class Cat extends Animal
{
void meow()
{
System.out.println("meowing...");
} }
class TestInheritance3
{
public static void main(String args[])
{
Cat c=new Cat();
c.meow();
c.eat();
}}
Output:
meowing...
eating...

Java Inheritance

  • 1.
    By VINOTH R St.Joseph’s college(Arts& Science) Kovur, Chennai
  • 2.
    • The mechanismof deriving a new class from an old class is called inheritance. • The old class is known as the Base class or Parent class • The new class is called the Sub class or Derived class or Child class. • The inheritance allows subclasses to inherit all the variables and methods of their parent classes.
  • 3.
    • Syntax • Theextends keyword indicates that making a new class that derives from an existing class. class Subclass-name extends Superclass-name { //methods and fields }
  • 4.
    As displayed inthe figure • Programmer is the subclass and Employee is the superclass. • Relationship between two classes is Programmer IS-A Employee. • It means that Programmer is a type of Employee.
  • 5.
    class Employee { float salary=40000; } classProgrammer extends Employee { int bonus=10000; public static void main(String args[]) { Programmer p=new Programmer(); System.out.println("Programmer salary is:"+p.salary); System.out.println("Bonus of Programmer is:"+p.bonus); } } OUTPUT: Programmer salary is:40000.0 Bonus of programmer is:10000
  • 6.
    • On thebasis of class, there can be three types of inheritance in java 1. Single 2. Multilevel 3. Hierarchical • Multiple and hybrid inheritance is supported through interface only (Will see in Unit 3).
  • 7.
    • One subclass is derived from one super class • From the below diagram class A serves as a Super class or parent class • Class B serves as a Base class or Sub class.
  • 8.
    class Animal { void eat() { System.out.println("eating..."); }} class Dog extends Animal { void bark() { System.out.println("barking..."); } } Class Dog inherits the properties of Class Animal. class TestInheritance { public static void main(String args[]) { Dog d=new Dog(); d.bark(); d.eat(); }} OUTPUT: barking... eating... Animal Void eat() Dog Void bark() Void eat()
  • 9.
    • In Multilevelinheritance there is a concept of grandparent class. • From the Below diagram then class C inherits class B and class B inherits class A • Which means B is a parent class of C and A is a parent class of B. • So in this case class C is implicitly inheriting the properties and method of class A along with B that’s what is called multilevel inheritance.
  • 10.
    class Animal { void eat() { System.out.println("eating..."); }} classDog extends Animal { void bark() { System.out.println("barking..."); }} class BabyDog extends Dog { void weep() { System.out.println("weeping..."); }} class TestInheritance2 { public static void main(String args[]) { BabyDog d=new BabyDog(); d.weep(); d.bark(); d.eat(); }} Output: weeping... barking... eating... Animal Void eat() Dog Void bark() Void eat() BabyDog void weep() void bark() Void eat()
  • 11.
    • A classhas more than one child classes (sub classes). • More than one child classes have the same parent class. • Class A is a parent class of class B and class C and class C.
  • 12.
    class Animal { void eat() { System.out.println("eating..."); }} class Dog extends Animal { void bark() { System.out.println("barking..."); } } class Cat extends Animal { void meow() { System.out.println("meowing..."); } } class TestInheritance3 { public static void main(String args[]) { Cat c=new Cat(); c.meow(); c.eat(); }} Output: meowing... eating...