JAVA UNIT-II-part-I
JAVA UNIT-II-part-I
Inheritance, Packages and Interfaces – Hierarchical abstractions, Base class object, subclass, subtype,
substitutability, forms of inheritance specialization, specification, construction, extension, limitation,
combination, benefits of inheritance, costs of inheritance. Member access rules, super uses, using final with
inheritance, polymorphism- method overriding, abstract classes, the Object class. Defining, Creating and
Accessing a Package, Understanding CLASSPATH, importing packages, differences between classes and
interfaces, defining an interface, implementing interface, applying interfaces, variables in interface and
extending interfaces. Exploring java.io.
INHERITANCE IN JAVA
Inheritance is an important pillar of OOP (Object-Oriented Programming).
The process of obtaining the data members and methods from one class to another class is known
as inheritance.
Important Terminologies Used in Java Inheritance
Super Class/Parent Class: The class whose features are inherited is known as a superclass (or a base
class or a parent class).
Sub Class/Child 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.
Why Do We Need Java Inheritance?
Code Reusability: The code written in the Superclass is common to all subclasses. Child classes can
directly use the parent class code.
Method Overriding: Method Overriding is achievable only through Inheritance. It is one of the ways by
which Java achieves Run Time Polymorphism.
Abstraction: The concept of abstract where we do not have to provide all details is achieved through
inheritance. Abstraction only shows the functionality to the user.
How to Use Inheritance in Java?
The extends keyword is used for inheritance in Java.
Using the extends keyword indicates you are derived from an existing class. In other words, “extends”
refers to increased functionality.
Syntax
class SubclassName extends SuperclassName
{
//methods and fields
}
TYPES OF INHERITANCE
Based on number of ways inheriting the feature of base class into derived class we have five types of
inheritance they are:
1. Single inheritance
2. Multilevel inheritance
3. Hierarchical inheritance
4. Multiple inheritance
5. Hybrid inheritance
1. Single inheritance
In single inheritance there exists single base class and single derived class.
Example
class Animal
{
String name;
void show()
{
System.out.println(“Animal name is:"+name);
}
}
class Dog extends Animal
{
void bark()
{
System.out.println("Barking");
}
}
class TestInheritance
{
public static void main(String args[])
{
Dog d=new Dog();
d.name="DOG";
d.show();
d.bark();
}
}
2. Multilevel inheritances in Java
Example
In the example, BabyDog class inherits the Dog class which again inherits the Animal class, so there is a
multilevel inheritance.
class Animal
{
String name;
void show()
{
System.out.println("Animal Name is"+name);
}
}
class Dog extends Animal
{
void bark()
{
System.out.println("Mother Dog Barking...");
}
}
class BabyDog extends Dog
{
void weep()
{
System.out.println("Baby Dog weeping");
}
}
class TestInheritance2
{
public static void main(String args[])
{
BabyDog d=new BabyDog();
d.name="MotherDog";
d.show();
d.bark();
d.weep();
}
}
3. Hierarchical Inheritance
In Hierarchical Inheritance, one class serves as a superclass (base class) for more than one subclass. In
the below image, class A serves as a base class for the derived class B, C and D.
Example
class Animal
{
void eat()
{
System.out.println("eating...");
}
}
class Dog extends Animal
{
void bark()
{
System.out.println("barking...");
}
}
class Cat extends Animal
{
void meow()
{
System.out.println("meowing...");
}
}
class TestInheritance3
{
public static void main(String args[])
{
Cat c=new Cat();
c.meow();
c.eat();
//c.bark();//C.T.Error
}
}
4. Multiple inheritance
In multiple inheritance there exist multiple classes and single derived class.
The concept of multiple inheritance is not supported in java through concept of classes but it can be
supported through the concept of interface.
5. Hybrid inheritance
It is a mix of two or more of the above types of inheritance. Since Java doesn’t support multiple inheritances
with classes, hybrid inheritance is also not possible with classes. In Java, we can achieve hybrid inheritance
only through Interfaces.
SUBSTITUTABILITY
The inheritance concept used for the number of purposes in the java programming language. One of
the main purposes is substitutability.
The substitutability means that when a child class acquires properties from its parent class, the object
of the parent class may be substituted with the child class object.
For example, if B is a child class of A, anywhere we expect an instance of A we can use an instance of
B.
The substitutability can achieve using inheritance, whether using extends or implements keywords.
FORMS OF INHERITANCE
The following are the different forms of inheritance in java.
Specialization
Specification
Construction
Extension
Limitation
Combination
Specialization
It is the most ideal form of inheritance. The subclass is a special case of the parent class. It holds the
principle of substitutability.
Specification
This is another commonly used form of inheritance. In this form of inheritance, the parent class just specifies
which methods should be available to the child class but doesn't implement them. The java provides
concepts like abstract and interfaces to support this form of inheritance. It holds the principle of
substitutability.
Construction
This is another form of inheritance where the child class may change the behavior defined by the parent
class (overriding). It does not hold the principle of substitutability.
Extension
This is another form of inheritance where the child class may add its new properties. It holds the principle
of substitutability.
Limitation
This is another form of inheritance where the subclass restricts the inherited behavior. It does not hold
the principle of substitutability.
Combination
This is another form of inheritance where the subclass inherits properties from multiple parent classes.
Java does not support multiple inheritance type.
BENEFITS OF INHERITANCE
Inheritance helps in code reuse. The child class may use the code defined in the parent class without re-
writing it.
Inheritance can save time and effort as the main code need not be written again.
Inheritance provides a clear model structure which is easy to understand.
An inheritance leads to less development and maintenance costs.
With inheritance, we will be able to override the methods of the base class so that the meaningful
implementation of the base class method can be designed in the derived class.
In inheritance base class can decide to keep some data private so that it cannot be altered by the derived
class.
THE COSTS OF INHERITANCE
Inheritance decreases the execution speed due to the increased time and effort it takes, the program
to jump through all the levels of overloaded classes.
Inheritance makes the two classes (base and inherited class) get tightly coupled. This means one
cannot be used independently of each other.
The changes made in the parent class will affect the behavior of child class too.
The overuse of inheritance makes the program more complex.