Dive into the world of Java Inheritance and discover how it forms the backbone of robust and scalable code architecture. Learn how this powerful object-oriented programming concept allows you to effortlessly reuse and extend existing code, promoting a modular and maintainable design. Whether you're a novice or a seasoned developer, grasp the key principles of inheritance and elevate your Java programming skills. Join us on this journey to empower your code with the inheritance advantage, setting the stage for efficient development and enhanced software flexibility. Don't miss out on this essential building block for creating elegant and efficient Java applications!Unlock the potential of Java Inheritance with a concise exploration of single, multiple, multilevel, and hierarchical types. Discover how each type enhances code structure and flexibility. Elevate your Java skills by understanding the nuances of inheritance, optimizing code for efficiency and maintainability. Join us on this journey of code mastery and harness the diverse power of inheritance in your Java projects.
2. What is inheritance?
Inheritance necessity
Terminologies
Syntax
Types of inheritance
Java access modifiers and access control
Examples
References
2/17/2024 2
naeem_mia
3. Inheritance in Java is a mechanism in which one
object acquires all the properties and behaviors of a
parent object. It is an important part of OOPs (Object
Oriented programming system).
Inheritance represents the IS-A relationship which is
also known as a parent-child relationship.
Animal
Cat Dog
Animal has some common
features
Dog has the features
of animal
Cat has the features of
animal
2/17/2024 3
naeem_mia
4. Code Reusability: The code written in the Superclass
is common to all subclasses. Child classes can directly
use the parent class code.
Method Overriding: Method Overriding is
achievable only through Inheritance. It is one of the
ways by which Java achieves Run Time Polymorphism.
Abstraction: The concept of abstract where we do not
have to provide all details is achieved through
inheritance. Abstraction only shows the functionality
to the user.
2/17/2024 4
naeem_mia
5. Class: Class is a set of objects which shares common characteristics/ behavior and
common properties/ attributes. Class is not a real-world entity. It is just a template or
blueprint or prototype from which objects are created.
Super Class/Parent Class: The class whose features are inherited is known as a
superclass(or a base class or a parent class).
Sub Class/Child Class: The class that inherits the other class is known as a subclass(or a
derived class, extended class, or child class). The subclass can add its own fields and
methods in addition to the superclass fields and methods.
Reusability: Inheritance supports the concept of “reusability”, i.e. when we want to
create a new class and there is already a class that includes some of the code that we
want, we can derive our new class from the existing class. By doing this, we are reusing
the fields and methods of the existing class.
2/17/2024 5
naeem_mia
6. class derived-class extends base-class
{
//methods and fields
}
The extends keyword indicates that you are making a
new class that derives from an existing class. The
meaning of "extends" is to increase the functionality.
2/17/2024 6
naeem_mia
7. As displayed in the above figure, Programmer is the subclass and Employee is
the superclass. The relationship between the two classes is Programmer IS-
A Employee. It means that Programmer is a type of Employee.
2/17/2024 7
naeem_mia
9. In java programming, multiple and hybrid inheritance is supported
through interface only. We will learn about interfaces later.
2/17/2024 9
naeem_mia
12. 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...
2/17/2024 12
naeem_mia
13. 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...
2/17/2024 13
naeem_mia
14. 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
}
}
Output:
meowing...
eating...
2/17/2024 14
naeem_mia
15. class A{
void msg(){System.out.println("Hello");}
}
class B{
void msg(){System.out.println("Welcome");}
}
class C extends A,B{//suppose if it were
public static void main(String args[]){
C obj=new C();
obj.msg();//Now which msg() method would be invoked?
}
}
Output:
Compile Time Error
2/17/2024 15
naeem_mia