Inheritance
Inheritance
• 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.
class Employee
{
float salary=40000;
}
• If you don't want other classes to inherit from a class, use the final
keyword.
• If you try to access a final class, Java will generate an error.
TYPES OF INHERITANCE IN JAVA
• If subclass (child class) has the same method as declared in the parent
class, it is known as method overriding in Java.
• In other words, If a subclass provides the specific implementation of the
method that has been declared by one of its parent class, it is known as
method overriding.
• Method overriding is used to provide the specific implementation of a
method which is already provided by its superclass.
• Method overriding is used for runtime polymorphism.
RULES FOR JAVA METHOD OVERRIDING
1. The method must have the same name as in the parent class
2. The method must have the same parameter as in the parent class.
3. There must be an IS-A relationship (inheritance).
4. A static method cannot be overridden