INHERITANCE
IN ARNAB BHAUMIK
ECE, 3RD YEAR
UNIVERSITY ROLL NO. :
23900315009
GUIDED BY :
SANKHAMITRA ROY
• WHY PROGRAMMING?
• WHY JAVA?
• CLASSES AND OBJECTS
• INTRODUCTION TO INHERITANCE
TYPES OF INHERITANCE
ADVANTAGES AND DISADVANTAGES
CONTENTS
WHY PROGRAMMING?
• Programming is the term that refers to teaching, instructing or giving commands to the computer.
WHY JAVA?
• Simple
• Object-Oriented
• Platform Independent
• Secure
• Robust
• Multithreaded
CLASSES AND OBJECTS
• A class is a blueprint from which individual objects are created.
colour
name
INTRODUCTION TO INHERITANCE
• Inheritance in java is a mechanism in which one class acquires all the properties and behaviours of
another class.
• Sub Class : The class that inherits properties and behaviours from another class is called Sub class or
Derived Class.
• Super Class : The class whose properties and behaviours are inherited by sub class is called Base
Class or Super class.
• Syntax of Java Inheritance
class Subclass-name extends Superclass-name
{
//methods and fields
}
INTRODUCTION TO INHERITANCE
• Why and when to use inheritance?
TYPES OF INHERITANCE
• On the basis of class, there can be mainly three types of inheritance in java:
1. Single
2. Multilevel
3. Hierarchical
SINGLE INHERITANCE
Class A
Class B
SINGLE INHERITANCE
PROGRAM
class Animal {
void eat() {
System.out.println("eating...");
}
}
class Dog extends Animal {
void bark() {
System.out.println("barking...");
}
}
class TestInheritance {
public static void main(String args[ ]) {
Dog d=new Dog();
d.bark();
d.eat();
}
}
OUTPUT
barking...
eating...
MULTILEVEL INHERITANCE
Class A
Class B
Class C
MULTILEVEL INHERITANCE
PROGRAM
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...
HIERARCHICAL INHERITANCE
Class A
Class B Class C
HIERARCHICAL INHERITANCE
PROGRAM
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();
Dog d=new Dog();
c.meow();
c.eat();
d.bark();
d.eat();
}
}
OUTPUT
meowing...
eating...
barking…
eating…
MULTIPLE AND HYBRID INHERITANCE
Class A Class B
Class C
Class B Class C
Class D
Class A
ADVANTAGES AND DISADVANTAGES
 ADVANTAGES
• Inheritance promotes reusability. When a class inherits or derives another class, it can access all the functionality of
inherited class.
• Reusability enhanced reliability. The base class code will be already tested and debugged.
• As the existing code is reused, it leads to less development and maintenance costs.
 DISADVANTAGES
• Inherited functions work slower than normal function as there is indirection.
• Improper use of inheritance may lead to wrong solutions.
• Often, data members in the base class are left unused which may lead to memory wastage.
• Inheritance increases the coupling between base class and derived class. A change in base class will affect all the
child classes.
References:
• “The Complete Reference Java” by Herbert Schildt.
• “Java Programming Black Book” by Kogent Learning solution.
Inheritance In Java

Inheritance In Java

  • 1.
    INHERITANCE IN ARNAB BHAUMIK ECE,3RD YEAR UNIVERSITY ROLL NO. : 23900315009 GUIDED BY : SANKHAMITRA ROY
  • 2.
    • WHY PROGRAMMING? •WHY JAVA? • CLASSES AND OBJECTS • INTRODUCTION TO INHERITANCE TYPES OF INHERITANCE ADVANTAGES AND DISADVANTAGES CONTENTS
  • 3.
    WHY PROGRAMMING? • Programmingis the term that refers to teaching, instructing or giving commands to the computer.
  • 4.
    WHY JAVA? • Simple •Object-Oriented • Platform Independent • Secure • Robust • Multithreaded
  • 5.
    CLASSES AND OBJECTS •A class is a blueprint from which individual objects are created. colour name
  • 6.
    INTRODUCTION TO INHERITANCE •Inheritance in java is a mechanism in which one class acquires all the properties and behaviours of another class. • Sub Class : The class that inherits properties and behaviours from another class is called Sub class or Derived Class. • Super Class : The class whose properties and behaviours are inherited by sub class is called Base Class or Super class. • Syntax of Java Inheritance class Subclass-name extends Superclass-name { //methods and fields }
  • 7.
    INTRODUCTION TO INHERITANCE •Why and when to use inheritance?
  • 8.
    TYPES OF INHERITANCE •On the basis of class, there can be mainly three types of inheritance in java: 1. Single 2. Multilevel 3. Hierarchical
  • 9.
  • 10.
    SINGLE INHERITANCE PROGRAM class Animal{ void eat() { System.out.println("eating..."); } } class Dog extends Animal { void bark() { System.out.println("barking..."); } } class TestInheritance { public static void main(String args[ ]) { Dog d=new Dog(); d.bark(); d.eat(); } } OUTPUT barking... eating...
  • 11.
  • 12.
    MULTILEVEL INHERITANCE PROGRAM 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...
  • 13.
  • 14.
    HIERARCHICAL INHERITANCE PROGRAM 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(); Dog d=new Dog(); c.meow(); c.eat(); d.bark(); d.eat(); } } OUTPUT meowing... eating... barking… eating…
  • 15.
    MULTIPLE AND HYBRIDINHERITANCE Class A Class B Class C Class B Class C Class D Class A
  • 16.
    ADVANTAGES AND DISADVANTAGES ADVANTAGES • Inheritance promotes reusability. When a class inherits or derives another class, it can access all the functionality of inherited class. • Reusability enhanced reliability. The base class code will be already tested and debugged. • As the existing code is reused, it leads to less development and maintenance costs.  DISADVANTAGES • Inherited functions work slower than normal function as there is indirection. • Improper use of inheritance may lead to wrong solutions. • Often, data members in the base class are left unused which may lead to memory wastage. • Inheritance increases the coupling between base class and derived class. A change in base class will affect all the child classes.
  • 17.
    References: • “The CompleteReference Java” by Herbert Schildt. • “Java Programming Black Book” by Kogent Learning solution.