Inheritance
❑Inheritance:
• Inheritance is an important pillar of OOP(Object-Oriented Programming). It is the
mechanism in java by which one class is allowed to inherit the features(fields and
methods) of another class.
➢Important terminology:
• Super Class: The class whose features are inherited is known as a superclass(or a base
class or a parent class).
• Sub 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.
Extends keyword:
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.
➢ Types of inheritance in java:
1) Single Inheritance:
2) Multilevel Inheritance:
3) Hierarchical Inheritance:
4) Multiple Inheritance (Through Interfaces):
• In Multiple inheritances, one class can have more than one superclass and inherit
features from all parent classes.
• Please note that Java does not support multiple inheritances with classes.
• In java, we can achieve multiple inheritances only through Interfaces.
• In the image below, Class C is derived from interface A and B.
Interface:
• An interface is a fully abstract class. It includes a group of abstract methods
(methods without a body).
• To use an interface, other classes must implement it. We use the
implements keyword to implement an interface.
• Example:
// interface
interface Animal
{
public void animalSound(); // interface method (does not have a body)
public void run(); // interface method (does not have a body)
}
5)Hybrid Inheritance:
• In general, the meaning of hybrid (mixture) is made of more than one thing.
• In Java, the hybrid inheritance is the composition of two or more types of inheritance.
• The main purpose of using hybrid inheritance is to modularize the code into well-
defined classes.
• It also provides the code reusability.
• Example:(Using Multilevel and Hierarchical Inheritance):
Method Overriding:
super keyword:
• We can use the super keyword to call methods and data members of
the immediate parent class.
• Whenever we create an instance of the child class, then the instance of
the parent class is created implicitly, which is referred by
the super reference variable.
• If we have the same method name in a child as well as parent
class then the super keyword is used to call the parent class method.
1. Use of super with variables:
2. Use of super with methods:
3. Use of super with constructors:
Abstract Class and methods:
An abstract class in Java is one that is declared with the abstract
keyword. It may have both abstract and non-abstract methods(methods
with bodies). An abstract is a java modifier applicable
for classes and methods in java but not for Variables.
For any abstract java class we are not allowed to create an object i.e., for abstract class
instantiation is not possible.
this keyword:
• The this keyword refers to the current object in a method or constructor.
• The most common use of the this keyword is to eliminate the confusion between class
attributes and parameters with the same name (because a class attribute is shadowed by a
method or constructor parameter).
• If you omit the keyword in the example above, the output would be "0" instead of "5".
➢ this can also be used to:
•Invoke current class constructor
•Invoke current class method
•Return the current class object
•Pass an argument in the method call
•Pass an argument in the constructor call
1) this: to refer current class instance variable:
The this keyword can be used to refer current class instance variable. If there is
ambiguity between the instance variables and parameters, this keyword resolves the
problem of ambiguity.
Understanding the problem without this keyword:
In the above example, parameters (formal arguments) and instance variables are same. So, we are
using this keyword to distinguish local variable and instance variable.
Solution of the above problem by this keyword:
If local variables(formal arguments) and instance variables are different, there is no
need to use this keyword like in the following program:
Program where this keyword is not required:
2) this: to invoke current class method:
• You may invoke the method of the current class by using the this
keyword. If you don't use the this keyword, compiler automatically
adds this keyword while invoking the method.
3) this() : to invoke current class constructor
The this() constructor call can be used to invoke the current class constructor. It is
used to reuse the constructor.
Real usage of this() constructor call:
The this() constructor call should be used to reuse the constructor from the
constructor.
4) this: to pass as an argument in the method
The this keyword can also be passed as an argument in the method.
5) this: to pass as argument in the constructor call
We can pass the this keyword in the constructor also. It is useful if we have to use
one object in multiple classes.
6) this keyword can be used to return current class instance
We can return this keyword as an statement from the method.
Final Keyword:
• The final keyword in java is used to restrict the user. The java final
keyword can be used in many context. Final can be:
[Link]
[Link]
[Link]
1) Java final variable:
If you make any variable as final, you cannot change the value of final variable(It
will be constant).
There is a final variable speedlimit, we are going to change the value of
this variable, but It can't be changed because final variable once
assigned a value can never be changed.
2) Java final method:
If you make any method as final, you cannot override it.
3) Java final class:
If you make any class as final, you cannot extend it.
• Final method is inherited, but you can not override it.
Example:
• We can initialize blank final variable, but only in constructor.
Example:
➢What is blank or uninitialized final variable?
• A final variable that is not initialized at the time of declaration is known as blank final
variable.
• If you want to create a variable that is initialized at the time of creating object and once
initialized may not be changed, it is useful. For example PAN CARD number of an
employee.
• It can be initialized only in constructor.
➢ static blank final variable:
A static final variable that is not initialized at the time of declaration is known as
static blank final variable.
It can be initialized only in static block.
➢ Note:
We can not declare constructor as final, because constructor is never inherited.
➢ Final parameter: