Inheritance in Java
Inheritance in Java
Inheritance in java is a mechanism in which one object acquires all the properties and behaviors of
parent object.
The idea behind inheritance in java is that you can create new classes that are built upon existing classes.
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.
Inheritance represents the IS-A relationship, also known as parent-child relationship.
Why use inheritance in java
For Method Overriding (so runtime polymorphism can be achieved).
For Code Reusability.
Syntax of Java Inheritance
1. class Subclass-name extends Superclass-name
2. {
3.
//methods and fields
4. }
extends Keyword
extends is the keyword used to inherit the properties of a class. Below given is the syntax of extends
keyword.
class Super{
.....
.....
}
class Sub extends Super{
.....
.....
}
The extends keyword indicates that you are making a new class that derives from an existing class.
In the terminology of Java, a class that is inherited is called a super class. The new class is called a
subclass.
Understanding the simple example of inheritance
As displayed in the above figure, Programmer is the subclass and Employee is the superclass.
Relationship between two classes is Programmer IS-A Employee.It means that Programmer is a type of
Employee.
1. class Employee{
2. float salary=40000;
3. }
4. class Programmer extends Employee{
5. int bonus=10000;
6. public static void main(String args[]){
7.
Programmer p=new Programmer();
8.
System.out.println("Programmer salary is:"+p.salary);
9.
System.out.println("Bonus of Programmer is:"+p.bonus);
10. }
11. }
Test it Now
Programmer salary is:40000.0
Bonus of programmer is:10000
In the above example, Programmer object can access the field of own class as well as of Employee class
i.e. code reusability.
Types of inheritance in java
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.
}
The output is
Color of Car : Blue
Speed of Car : 200
Size of Car : 22
CC of Car : 1000
No of gears of Car : 5
Visibility Control
It is possible to inherit all the members of a class by a subclass using the keyword extends. The
variables and methods of a class are visible everywhere in the program. However, it may be
necessary in some situations we may want them to be not accessible outside. We can achieve this in
Java by applying visibility modifiers to instance variables and methods. The visibility modifiers are
also known as access modifiers. Access modifiers determine the accessibility of the members of a
class.
Java provides three types of visibility modifiers: public, private and protected. They provide
different levels of protection as described below.
Public Access: Any variable or method is visible to the entire class in which it is defined. But, to
make a member accessible outside with objects, we simply declare the variable or method as
public. A variable or method declared as public has the widest possible visibility and accessible
everywhere.
Friendly Access (Default): When no access modifier is specified, the member defaults to a limited
version of public accessibility known as "friendly" level of access. The difference between the
"public" access and the "friendly" access is that the public modifier makes fields visible in all
classes, regardless of their packages while the friendly access makes fields visible only in the same
package, but not in other packages.
Protected Access: The visibility level of a "protected" field lies in between the public access and
friendly access. That is, the protected modifier makes the fields visible not only to all classes and
subclasses in the same package but also to subclasses in other packages
Private Access: private fields have the highest degree of protection. They are accessible only with
their own class. They cannot be inherited by subclasses and therefore not accessible in subclasses.
In the case of overriding public methods cannot be redefined as private type.
Private protected Access: A field can be declared with two keywords private and protected
together. This gives a visibility level in between the "protected" access and "private" access. This
modifier makes the fields visible in all subclasses regardless of what package they are in.
Remember, these fields are not accessible by other classes in the same package.
Access Control and Inheritance:
The following rules for inherited methods are enforced:
Methods declared protected in a superclass must either be protected or public in subclasses; they
cannot be private.
Methods declared without access control (no modifier was used) can be declared more private in
subclasses.
Methods declared private are not inherited at all, so there is no rule for them.
Access Modifier
Private
Default
Protected
Public
within class
Y
Y
Y
Y
within package
N
Y
Y
Y
outside package
N
N
N
Y