Inheritance in Java
JAY PRAJAPATI
ENROLL NO:161350116044
VRUSHANK PATEL
ENROLL NO:161350116044
CONTENTS
• What is inheritance
• Why use inheritance in java
• Syntax of Java Inheritance
• TYPES OF INHERITANCE
• Why multiple inheritance is not supported in java?
• Advantages of Inheritance
• Disadvantages of Inheritance
What is inheritance ????
• Inheritance in java is a mechanism in which one object acquires
all the properties andbehaviors ofparentobject.
• 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 addnew methods andfields also.
• Inheritance represents the IS-A relationship, also known as
parentchildrelationship.
Why use inheritance in java
▪ For Method Overriding (so runtime polymorphism can be
achieved).
▪ For Code Reusability.
Syntax of Java Inheritance
• classSubclass-nameextendsSuperclass-name
{
//methodsandfields
}
• 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.
TYPES OF INHERITANCE
• On the basis of class, there can be mainly three types of inheritance
in java:
1. Single inheritance
2. Multiline inheritance
3. Hierarchical inheritance
4. Multiple inheritance
5. Hybrid inheritance
• 1,2,3 Can be implement by using class and object
• 4,5 Can be implement using interface
SINGLE INHERITANCE
• classAnimal {
void eat() {
System.out.println("eating...");
}
}
class Dog extends Animal { OUTPUT
void bark() { barking...
eating...
System.out.println("barking...");
}
}
class TestInheritance {
public static void main(String args[ ]) {
Dog d=new Dog(); d.bark(); d.eat();
}
}
MULTILEVEL INHERITANCE
• classAnimal {
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...");
}
}
MULTILEVEL INHERITANCE
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 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...");
}
}
HIERARCHICAL INHERITANCE
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…
Why multiple inheritance is not supported in java?
• To reduce the complexity and simplify the language, multiple inheritance is not supportedin java.
class A {
void msg(){
System.out.println("Hello");}
}
class B {
void msg(){
System.out.println("Welcome");}
}
class C extends A,B
{
Public Static void main(String args[])
{
C obj=new C(); obj.msg(); //Now which msg() method would be invoked?
} }
Advantages of Inheritance
● Reusability -- facility to use public methods of base class
without rewriting the same
● Extensibility -- extending the base class logic as per business logic
of the derived class
● Data hiding -- base class can decide to keep some data private so
that it cannot be altered by the derived class
● Overriding -- With inheritance, we will be able to override the
methods of the base class so that meaningful implementation of
the base class method can be designed in the derived class.
Disadvantages of Inheritance
• Unsupported- Java not support Multiple inheritance as well as
Hybrid inheritance.
• Permits only one class- The extends keyword permits to connect
a class with only one class.
• Undefined- In Interface, properties are only declared and
assigned, but never defined.
• Re-factor- If a method is deleted in the "super class" or
aggregate, then we will have to re-factor in case of using
that method.
Thank you !!

inheritance

  • 1.
    Inheritance in Java JAYPRAJAPATI ENROLL NO:161350116044 VRUSHANK PATEL ENROLL NO:161350116044
  • 2.
    CONTENTS • What isinheritance • Why use inheritance in java • Syntax of Java Inheritance • TYPES OF INHERITANCE • Why multiple inheritance is not supported in java? • Advantages of Inheritance • Disadvantages of Inheritance
  • 3.
    What is inheritance???? • Inheritance in java is a mechanism in which one object acquires all the properties andbehaviors ofparentobject. • 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 addnew methods andfields also. • Inheritance represents the IS-A relationship, also known as parentchildrelationship.
  • 4.
    Why use inheritancein java ▪ For Method Overriding (so runtime polymorphism can be achieved). ▪ For Code Reusability.
  • 5.
    Syntax of JavaInheritance • classSubclass-nameextendsSuperclass-name { //methodsandfields } • 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.
  • 6.
    TYPES OF INHERITANCE •On the basis of class, there can be mainly three types of inheritance in java: 1. Single inheritance 2. Multiline inheritance 3. Hierarchical inheritance 4. Multiple inheritance 5. Hybrid inheritance • 1,2,3 Can be implement by using class and object • 4,5 Can be implement using interface
  • 7.
    SINGLE INHERITANCE • classAnimal{ void eat() { System.out.println("eating..."); } } class Dog extends Animal { OUTPUT void bark() { barking... eating... System.out.println("barking..."); } } class TestInheritance { public static void main(String args[ ]) { Dog d=new Dog(); d.bark(); d.eat(); } }
  • 8.
    MULTILEVEL INHERITANCE • classAnimal{ 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..."); } }
  • 9.
    MULTILEVEL INHERITANCE class TestInheritance2{ public static void main(String args[ ]) { BabyDog d=new BabyDog(); d.weep(); d.bark(); d.eat(); } } OUTPUT weeping... barking... eating...
  • 10.
    HIERARCHICAL INHERITANCE 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..."); } }
  • 11.
    HIERARCHICAL INHERITANCE 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…
  • 12.
    Why multiple inheritanceis not supported in java? • To reduce the complexity and simplify the language, multiple inheritance is not supportedin java. class A { void msg(){ System.out.println("Hello");} } class B { void msg(){ System.out.println("Welcome");} } class C extends A,B { Public Static void main(String args[]) { C obj=new C(); obj.msg(); //Now which msg() method would be invoked? } }
  • 13.
    Advantages of Inheritance ●Reusability -- facility to use public methods of base class without rewriting the same ● Extensibility -- extending the base class logic as per business logic of the derived class ● Data hiding -- base class can decide to keep some data private so that it cannot be altered by the derived class ● Overriding -- With inheritance, we will be able to override the methods of the base class so that meaningful implementation of the base class method can be designed in the derived class.
  • 14.
    Disadvantages of Inheritance •Unsupported- Java not support Multiple inheritance as well as Hybrid inheritance. • Permits only one class- The extends keyword permits to connect a class with only one class. • Undefined- In Interface, properties are only declared and assigned, but never defined. • Re-factor- If a method is deleted in the "super class" or aggregate, then we will have to re-factor in case of using that method.
  • 15.