Name: Ariful Islam
Subjects: OOP
Topics: Java Inheritance
Java Inheritance:
1. What is Inheritance
2. Single Inheritance
○ Implementation of single inheritance
3. Hierarchical Inheritance
○ Implementation of Hierarchical Inheritance
4. Multi-Level Inheritance
○ Implementation of Multi-Level Inheritance
5. Multiple Inheritance
○ Implementation of Multiple Inheritance
6. Hybrid Inheritance
○ Implementation of Hybrid Inheritance
I will try to cover:
1. What is inheritance?
Inheritance refers to a Java programming functionality that allows you to build classes
derived from other classes. The other class inherits a class that's based on another
class. The parent class, the foundation class, or the superclass, is the class that is
inherited. The inheriting class is the child class, the class derived, or the subclass.
A subclass takes on all the actions and characteristics of its base class automatically.
Thus you can build a base class that describes all the common features if you need
to create many classes to classify forms that are not identical but have several
features in common. You can then create subclasses that inherit the characteristics
that are common.
By defining its own methods and fields, a subclass may add features to the base
class it inherits. This is one of the ways in which a derivative class differentiates itself
from its base class.
What is inheritance?
A subclass can also change the behaviour that the base class provides. A base class
may provide that all classes derived from it have a method named play, for example,
but each class is free to provide its own implementation of the play method. In this
case, all classes that extend the base class include the play method with their own
implementation.
To build a subclass, you use the extension keyword in the class declaration to define
the name of the base class. The basic format of a declaration of a subclass is this:
public class ClassName extends BaseClass
{
// class body goes here
}
2. Single Inheritance
When a class inherits another class, it is known as a single inheritance. The Dog
Class inherits the Animal Class in the example below, so there is a single inheritance.
Class A
Class B
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();
}} //Implementation
3. Hierarchical Inheritance
If a single class is inherited by two or more classes, it is known as hierarchical
inheritance. In the example below the animal class is inherited by the Dog and Cat
classes, so there is hierarchical inheritance.
Class A
Class B
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();
//c.bark();//C.T.Error
}} //Implementation
Class C Class D
4. Multi-Level Inheritance
Multiple inheritance is not supported in Java to reduce the complexity and simplify the
language. Consider a case where three groups are A, B and C. The C class inherits
A and B classes. If A and B classes have same method and you call it from child
class object, there would be uncertainty to call method of A or B class. We have
virtual keywords in C++ to address this ambiguity, but multiple inheritance in Java is
only possible via interfaces.
Class A
Class B
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();}}
Class C
6. Hybrid Inheritance
A hybrid inheritance is a combination of more than one types of inheritance. For
example when class A and B extends class C & another class D extends class A
then this is a hybrid inheritance, because it is a combination of single and hierarchical
inheritance. Let me show you this diagrammatically:
Class A
Class B
Class C
Class d
5. Multiple Inheritance
Did you notice that we I am skip number 5?
Multiple inheritance is not supported in Java to reduce the complexity and simplify the
language.
Consider a situation where three groups are A, B and C. The C class inherits classes
A and B. If A and B classes have the same method and you call it from the child class
object, the method of calling A or B classes would be unclear.
Since compile-time errors are better than runtime errors, if you inherit 2 classes, Java
returns a compile-time error.
So whether you have same method or different, there will be compile time error.
End of presentation
Date: 20-NOV-2020
arifulislamat@gmail.com
Note: I am sorry that the presentation is not quite good, I am not aware of
The presentation. Roughly in 2 hours this is what I can present.
I have used the internet to get this info.
Thank you!

Inheritance in java

  • 1.
    Name: Ariful Islam Subjects:OOP Topics: Java Inheritance
  • 2.
    Java Inheritance: 1. Whatis Inheritance 2. Single Inheritance ○ Implementation of single inheritance 3. Hierarchical Inheritance ○ Implementation of Hierarchical Inheritance 4. Multi-Level Inheritance ○ Implementation of Multi-Level Inheritance 5. Multiple Inheritance ○ Implementation of Multiple Inheritance 6. Hybrid Inheritance ○ Implementation of Hybrid Inheritance I will try to cover:
  • 3.
    1. What isinheritance? Inheritance refers to a Java programming functionality that allows you to build classes derived from other classes. The other class inherits a class that's based on another class. The parent class, the foundation class, or the superclass, is the class that is inherited. The inheriting class is the child class, the class derived, or the subclass. A subclass takes on all the actions and characteristics of its base class automatically. Thus you can build a base class that describes all the common features if you need to create many classes to classify forms that are not identical but have several features in common. You can then create subclasses that inherit the characteristics that are common. By defining its own methods and fields, a subclass may add features to the base class it inherits. This is one of the ways in which a derivative class differentiates itself from its base class.
  • 4.
    What is inheritance? Asubclass can also change the behaviour that the base class provides. A base class may provide that all classes derived from it have a method named play, for example, but each class is free to provide its own implementation of the play method. In this case, all classes that extend the base class include the play method with their own implementation. To build a subclass, you use the extension keyword in the class declaration to define the name of the base class. The basic format of a declaration of a subclass is this: public class ClassName extends BaseClass { // class body goes here }
  • 5.
    2. Single Inheritance Whena class inherits another class, it is known as a single inheritance. The Dog Class inherits the Animal Class in the example below, so there is a single inheritance. Class A Class B 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(); }} //Implementation
  • 6.
    3. Hierarchical Inheritance Ifa single class is inherited by two or more classes, it is known as hierarchical inheritance. In the example below the animal class is inherited by the Dog and Cat classes, so there is hierarchical inheritance. Class A Class B 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(); //c.bark();//C.T.Error }} //Implementation Class C Class D
  • 7.
    4. Multi-Level Inheritance Multipleinheritance is not supported in Java to reduce the complexity and simplify the language. Consider a case where three groups are A, B and C. The C class inherits A and B classes. If A and B classes have same method and you call it from child class object, there would be uncertainty to call method of A or B class. We have virtual keywords in C++ to address this ambiguity, but multiple inheritance in Java is only possible via interfaces. Class A Class B 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();}} Class C
  • 8.
    6. Hybrid Inheritance Ahybrid inheritance is a combination of more than one types of inheritance. For example when class A and B extends class C & another class D extends class A then this is a hybrid inheritance, because it is a combination of single and hierarchical inheritance. Let me show you this diagrammatically: Class A Class B Class C Class d
  • 9.
    5. Multiple Inheritance Didyou notice that we I am skip number 5? Multiple inheritance is not supported in Java to reduce the complexity and simplify the language. Consider a situation where three groups are A, B and C. The C class inherits classes A and B. If A and B classes have the same method and you call it from the child class object, the method of calling A or B classes would be unclear. Since compile-time errors are better than runtime errors, if you inherit 2 classes, Java returns a compile-time error. So whether you have same method or different, there will be compile time error.
  • 10.
    End of presentation Date:20-NOV-2020 arifulislamat@gmail.com Note: I am sorry that the presentation is not quite good, I am not aware of The presentation. Roughly in 2 hours this is what I can present. I have used the internet to get this info. Thank you!