Inheritance
(IS-A)
Inheritance
is one of the key features of Object Oriented Programming. Inheritance provided
mechanism that allowed a class to inherit property of another
class.
When a Class extends another class it inherits all non-private members
including fields and methods. Inheritance in Java can be best understood in
terms of Parent and Child relationship, also known as Super class(Parent) and Sub class(child) in Java language.
Inheritance
defines is-a relationship between a
Super class and its Sub class. extends and implementskeywords are used to
describe inheritance in Java.
Picture-1:
Let us see how extend keyword is used to achieve Inheritance.
class Vehicle.
{
......
}
class Car extends Vehicle
{
.......
//extends the property of vehicle class.
}
Now
based on above example. In OOPs term we can say that,
·
Vehicle is super class of Car.
·
Car is sub class of Vehicle.
·
Car
IS-A Vehicle.
Purpose
of Inheritance
1. To promote code reuse.
2. To use Polymorphism.
Simple
example of Inheritance
class Parent
{
public void p1()
{
System.out.println("Parent
method");
}
}
public class Child extends Parent {
public void c1()
{
System.out.println("Child
method");
}
public static void main(String[] args)
{
Child cobj = new Child();
cobj.c1(); //method of
Child class
cobj.p1(); //method of
Parent class
}
}
Output
Child method
Parent method
Another
example of Inheritance
class Vehicle
{
String vehicleType;
}
public class Car extends Vehicle {
String modelType;
public void showDetail()
{
vehicleType = "Car"; //accessing
Vehicle class member
modelType = "sports";
System.out.println(modelType+"
"+vehicleType);
}
public static void main(String[] args)
{
Car car =new Car();
car.showDetail();
}
}
Output
sports Car
Types
of Inheritance
1. Single Inheritance
2. Multilevel Inheritance
3. Heirarchical Inheritance
NOTE
:Multiple
inheritance is not supported in java
Picture-2:
Why
multiple inheritance is not supported in Java
·
To
remove ambiguity.
·
To
provide more maintainable and clear design.
Picture-3:
super
keyword
In
Java, super keyword is used to refer
to immediate parent class of a class. In other words super keyword is used by a
subclass whenever it need to refer to its immediate super class.
Example
of Child class refering Parent class property using super keyword
class Parent
{
String name;
}
public class Child extends Parent {
String name;
public void details()
{
super.name = "Parent";
//refers to parent class member
name = "Child";
System.out.println(super.name+"
and "+name);
}
public static void main(String[] args)
{
Child cobj = new Child();
cobj.details();
}
}
Output
Parent and Child
Example
of Child class refering Parent class methods using super keyword
class Parent
{
String name;
public void details()
{
name = "Parent";
System.out.println(name);
}
}
public class Child extends Parent {
String name;
public void details()
{
super.details(); //calling Parent class details() method
name = "Child";
System.out.println(name);
}
public static void main(String[] args)
{
Child cobj = new Child();
cobj.details();
}
}
Output
Parent
Child
Example
of Child class calling Parent class constructor using super keyword
class Parent
{
String name;
public Parent(String n)
{
name = n;
}
}
public class Child extends Parent {
String name;
public Child(String n1, String n2)
{
super(n1); //passing
argument to parent class constructor
this.name = n2;
}
public void details()
{
System.out.println(super.name+"
and "+name);
}
public static void main(String[] args)
{
Child cobj = new
Child("Parent","Child");
cobj.details();
}
}
Output
Parent and Child
Super
class reference pointing to Sub class object.
In
context to above example where Class B extends class A.
A a=new B();
is legal syntax because of IS-A relationship is there
between class A and Class B.
Q.
Can you use both this() and super() in a Constructor?
NO,
because both super() and this() must be first statement inside a constructor.
Hence we cannot use them together.
Q: why interface can not implement another interface?
Ans by Jigar Joshi:
implements means implementation, when interface is meant to declare just to provide interface not for implementation.
100% abstract class is functionally equivalent to interface but it can also have implementation if you wish(in this case it won't remain 100 %), so from JVM perspective they are different things.
Also the member variable in 100% abstract class can have any access qualifier, where in interface they are implicitly public static final
Ans by Colin Hebert:
implements means a behaviour will be defined for abstract methods (except for abstract classes obviously), you define the implementation.
extends means that a behaviour is inherited.
With interfaces it is possible to say that one interface should have that the same behaviour as another, there is not even an actual implementation. That's why it makes more sense for an interface to extends another interface instead of implementing it.
On a side note, remember that even if an abstract class can define abstract methods (the same way an interface does), it is still a class and still has to be inherited (extended) and not implemented.
Q: What is the purpose of an interface implementing another interface?
Ans by JB Nizet:
An interface defines behavior. For example, a Vehicle interface might define the move() method.
A Car is a Vehicle, but has additional behavior. For example, the Car interface might define the startEngine() method. Since a Car is also a Vehicle, the Car interface extends the Vehicle interface, and thus defines two methods: move() (inherited) and startEngine().
The Car interface doesn't have any method implementation. If you create a class (Volkswagen) that implements Car, it will have to provide implementations for all the methods of its interface: move() and startEngine().
An interface may not implement any other interface. It can only extend it.
Related Links:
1. http://stackoverflow.com/questions/3921412/why-interface-can-not-implement-another-interface
2. http://stackoverflow.com/questions/10227410/interface-extends-another-interface-but-implements-its-methods
3. http://www.studytonight.com/java/inheritance-in-java.php
No comments:
Post a Comment