Java - Inheritance ppt.1-1
Java - Inheritance ppt.1-1
Single inheritance
Multilevel inheritance
Multiple inheritance
Hierarchical inheritance
Hybrid inheritance
Single inheritance
Code Reusability: Inheritance lets you reuse Polymorphism: Inheritance is essential for
code from an existing class, which means you achieving polymorphism, which allows objects of
don't have to write the same code again. This different classes to be treated as objects of a
saves time and effort. common parent class. This simplifies code and
makes it more flexible.
Organization: It helps in organizing classes in a
Maintenance: When you need to make changes to a
hierarchy. You can have a base (parent) class
common feature shared by multiple classes, you can
with common attributes and methods, and then
make the change in the parent class, and it will
create specialized (child) classes that inherit from
automatically reflect in all the child classes. This
the base class and add their unique features. reduces the chances of errors and makes
Extensibility: You can extend the functionality maintenance easier.
of a class by creating a new class (child class)
that inherits from it and adds or overrides
methods. This makes it easier to make changes
and additions to your code.
Java program to implement inheritance
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);
}
}