5.inheritance in Java
5.inheritance in Java
Inheritance in Java
• Inheritance is one of the key features of OOP (Object-
oriented Programming) that allows us to define a new
class from an existing class.
• Inheritance provided mechanism that allowed a class to
inherit property of another class.
• When a Class extends another class it inherits all non-
private members including fields and methods.
• In Java, we use the “extends” keyword to inherit from a
class.
• For Method Overriding (so runtime polymorphism can be
achieved).
• For Code Reusability.
Terms used in Inheritance
• Class: A class is a group of objects which have common properties. It
is a template or blueprint from which objects are created.
Ex:
class Vehicle
{
......
}
class Car extends Vehicle
{
....... //extends the property of vehicle class
}
• 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.
• In the terminology of Java, a class which is inherited is
called a parent or superclass, and the new class is called
child or subclass.
Types of inheritance in java
• On the basis of class, there can be three types of inheritance in java: