02 - Inheritance, Interface & Abstraction Class in Java
02 - Inheritance, Interface & Abstraction Class in Java
1) Inheritance in java is a mechanism in which one object acquires all the properties and
behaviors of parent object.
2) The idea behind inheritance in java is that you can create new classes that are built
upon existing classes.
3) When you inherit from an existing class, you can reuse methods and fields of parent
class, and you can add new methods and fields also. i.e. the subclass inherit the
MEMBERS of the Superclass. Here, Members=data members + methods.
4) Inheritance represents the IS-A relationship (Programmer is Employee), also known as
parent-child relationship.
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 parent or super class and
the new class is called child or subclass.
In the side example, Programmer object can access the field of own
class as well as of Employee class i.e. code reusability.
On the basis of class, there can be three types of inheritance in java: single, multilevel and
hierarchical.
In java programming, multiple and hybrid inheritance is supported through interface only. We
will learn about interfaces later.
Note: Multiple inheritance is not supported in java
through class.
To reduce the complexity and simplify the language, multiple inheritance is not supported in
java.
Consider a scenario where A, B and C are three classes. The C class inherits A and B classes.
If A and B classes have same method and you call it from child class object, there will be
ambiguity to call method of A or B class.
Since compile time errors are better than runtime errors, java renders compile time error if you
inherit 2 classes. So whether you have same method or different, there will be compile time
error now.
Method Overriding 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 subclass provides the specific implementation of the method that has
been provided by one of its parent class, it is known as method overriding.
The super keyword in java is a reference variable which is used to refer immediate
parent class object.
Whenever you create the instance of subclass, an instance of parent class is created
implicitly which is referred by super reference variable.
1) Super is used to refer immediate parent class instance 2) Super can be used to invoke parent class method
variable.
We can use super keyword to access the data member or field We can use super keyword to access the data member or
of parent class. It is used if parent class and child class have field of parent class. It is used if parent class and child class
same fields. have same fields.
Super example: real use Let’s see the real use of super keyword. Here, Emp class inherits Person class
so all the properties of Person will be inherited to Emp by default. To initialize all the property, we are
using parent class constructor from child class. In such way, we are reusing the parent class constructor.
Upcasting
When reference variable of Parent class refers to the object of Child class, it
is known as upcasting. For example:
Note: Superclass reference variable can refer to Subclass Object (it’s nothing but polymorphism)
Note: you can only invoke methods that are either overridden or inherited from the superclass
TypeCasting
In right side e.g. Class A can’t access
printFromB() method of subclass then
typecasting is needed.
Syntax:
((subclass_name).object of Superclass)
Runtime and Compile time polymorphism
No, static method cannot be overridden. It can be proved by runtime polymorphism, so we will learn it
later.
Because static method is bound with class whereas instance method is bound with object. Static
belongs to class area and instance belongs to heap area.
In case of method overloading, parameter must In case of method overriding, parameter must be
3)
be different. same.
Method overloading is the example of compile Method overriding is the example of run time
4)
time polymorphism. polymorphism.
In java, method overloading can't be performed
by changing return type (data type of method) of
Return type must be same or covariant in method
5) the method only. Return type can be same or
overriding.
different in method overloading. But you must
have to change the parameter.
Abstract class in Java
Abstraction is a process of hiding the implementation details and showing only functionality to
the user.
Another way, it shows only important things to the user and hides the internal details for
example sending sms, you just type the text and send the message. You don't know the internal
processing about the message delivery.
Abstraction lets you focus on what the object does instead of how it does it.
Abstract methods are usually declared where two or more subclasses are expected to
do a similar thing in different ways through different implementations. These
subclasses extend the same Abstract class and provide different implementations for
the abstract methods.
Abstract classes are used to define generic types of behaviors at the top of an object-
oriented programming class hierarchy, and use its subclasses to provide
implementation details of the abstract class.
Ways to achieve Abstraction: There are two ways to achieve abstraction in java
1. Abstract class (0 to 100%) because it can also have concrete method(non-abstract method)
2. Interface (100%)
1. whenever superclass is there only for to give you structure, also if we don’t want to
create object of that class make it abstract class
2. A class that is declared as abstract is known as abstract class.
3. It needs to be extended and its method implemented.
4. It cannot be instantiated (we can’t make object of abstract class).
5. An abstract class may also have concrete (non abstract) methods.
6. For design purpose, a class can be declared abstract even if it does not contain any
abstract methods
Consider the right side class hierarchy consisting of a Shape class which is inherited
by three classes Rectangle , Circle and Triangle. The Shape class is created to save
on common attributes and methods shared by the three classes Rectangle , Circle
and Triangle. calculateArea() is one such method shared by all 3 child classes and
present in Shape class.
Now, assume you write code to create objects for the classes depicted above. Let’s observe how these objects will look in a
practical world.
But what would an object of Class Shape look like in a practical world??
If you observe the Shape class serves in our goal of achieving inheritance and polymorphism. But it was not built to be
instantiated. Such classes can be labeled Abstract. An abstract class cannot be instantiated.
Note: It is possible that you DO NOT label Shape class as Abstract and then instantiate it. But such object will have no use in
your code and will open a room for potential errors. Hence this is not desirable.
Example abstract class: abstract class A{}
Abstract method
An abstract class can have data member, abstract method, method body, constructor
and even main() method.
Rule2: If you are extending any abstract class that have abstract
method, you must either provide the implementation of the method or
make this class abstract.
Rule3: An abstract class may or may not have an abstract method. But
if any class has even a single abstract method, then it must be declared
abstract.
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:
1. variable
2. method
3. class
If you make any variable as final, you cannot change the value of final
variable (It will be constant).
If you make any method as final, you cannot override If you make any class as final, you cannot
it. extend it.
Q) Is final method inherited? : Yes, final method is inherited but you cannot override it.
You should use interfaces when you want a full implementation of methods defined in
interfacess and use abstract classes when you want partial pieces for your design (for
reusability)
Cohesion & coupling
Highly cohesive classes are much easier to maintain and less frequently changed.
Such classes are more usable than others as they are designed with a well-focused purpose.
2) Coupling:
It refers to how related are two classes / modules and how dependent they are on each
other.
Being low coupling would mean that changing something major in one class should
not affect the other.
High coupling would make your code difficult to make changes as well as to maintain
it, as classes are coupled closely together, making a change could mean an entire
system revamp.
E.g. iPods are a good example of tight coupling: once the battery dies you might as well buy a new
iPod because the battery is soldered fixed and won’t come loose, thus making replacing very expensive.
A loosely coupled player would allow effortlessly changing the battery.
{All good software design will go for high cohesion and low coupling.}
Java 8 Interface Improvement: Since Java 8, interface can have default and static methods.
Internal addition by compiler
The java compiler adds public and
abstract keywords before the
interface method. More, it adds
public, static and final keywords
before data members.
In other words, Interface fields are
public, static and final by default,
and methods are public and
abstract.
Since Java 8, we can have static method in interface. An interface that have no member is known as marker or
tagged interface. For example: Serializable, Cloneable,
Remote etc. They are used to provide some essential
information to the JVM so that JVM may perform some
useful operation.
Abstract class Interface
1) Abstract class can have abstract and non-abstract Interface can have only abstract methods. Since Java 8, it can
methods. have default and static methods also.
2) Abstract class doesn't support multiple inheritance. Interface supports multiple inheritance.
3) Abstract class can have final, non-final, static and non-
Interface has only static and final variables.
static variables.
4) Abstract class can provide the implementation of
Interface can't provide the implementation of abstract class.
interface.
Example of abstract class and interface in Java: Let's see a simple example where we are
using interface and abstract class both.
Of course.
Declaring a class abstract only
means that you don't allow it to
be instantiated on its own.
Declaring a method abstract
means that subclasses have to
provide an implementation for
that method.
The two are separate concepts,
but obviously you can't have an
abstract method in a non-abstract
class. You can even have abstract
classes with final methods but
never the other way around.
Note: An interface can have another interface i.e. known as nested interface. We will learn it
in detail in the nested classes chapter. For example: