What is Inheritance in Java
Inheritance in Java or OOPS (Object oriented programming) is a feature which allows to code reusability. In other words Inheritance self implies inheriting or we can say acquiring something from others. Along with Abstraction, Encapsulation and Polymorphism, Inheritance forms back bone of Object oriented programming and Java. In Java we use the term inheritance when one object acquires some property from other object. In Java, inheritance is defined in terms of super class and subclass. it is normally used when some object want to use existing feature of some class and also want to provide some special feature, so we can say inheritance has given the advantage of reusability. So by using Inheritance between Super class and Subclass IS-A Relationship is formed means we can use any subclass object in place of super class object. Inheritance in Java is also use to provide concrete implementation of abstract class and interface in Java.
Inheritance in Java or OOPS (Object oriented programming) is a feature which allows to code reusability. In other words Inheritance self implies inheriting or we can say acquiring something from others. Along with Abstraction, Encapsulation and Polymorphism, Inheritance forms back bone of Object oriented programming and Java. In Java we use the term inheritance when one object acquires some property from other object. In Java, inheritance is defined in terms of super class and subclass. it is normally used when some object want to use existing feature of some class and also want to provide some special feature, so we can say inheritance has given the advantage of reusability. So by using Inheritance between Super class and Subclass IS-A Relationship is formed means we can use any subclass object in place of super class object. Inheritance in Java is also use to provide concrete implementation of abstract class and interface in Java.
Inheritance in Java- Things to remember
Here are some important points about Inheritance in Java which is worth
remembering:
1) One sub class can extend only one super class in Java but it can
implement multipel interface.
2) Private member of super class can not be inherited in subclass e.g.
private field and private methods.
3) Default member can only be inherited in same package subclass not in
another package.
4) Constructor in Java is not inherited by subclass.
5) If a class implements Interface or extends abstract class , it needs
to override all abstract methods untile it is not abstract.
6) Multiple inheritance is not supported in java but we can achieve this
by using interface .One class can implement multiple interface
7) In Java class never extends the interface rather it implements
interface
8) One interface can extends other interface in Java.
How
to achieve Inheritance in java
Inheritance can be achieved in Java through two keywords:
·
extends
·
implements
Use extends when there is some class and another class wants to
use property of that class so always one class extends another class. While implements is used
when some interface is there and some class wants to give implementation of
that interface according to depending of their requirement.
Example
of Inheritance in Java
Now we know What is Inheritance in
Java and some specific property of Inheritance in Java, its time to see a
real life example of Inheritance in Java
and learn How to use Inheritance in Java.
public class
Currency {
String description = "Unknown currency";
public String getCurrencyDescription() {
return description;
}
public abstract double cost(double value);
}
public class Rupee extends Currency {
double value;
public Rupee() {
description = "indian rupees";
}
public double cost(double v){
value=v;
return value;
}
}
String description = "Unknown currency";
public String getCurrencyDescription() {
return description;
}
public abstract double cost(double value);
}
public class Rupee extends Currency {
double value;
public Rupee() {
description = "indian rupees";
}
public double cost(double v){
value=v;
return value;
}
}
In above example of Inheritance in Java, Currency is called
parent class while Rupee is child of Currency class. In
object oriented programming term, Currency is super
class while Rupee is subclass. It means Rupee inherits
all non private members e.g. fields and methods.Abstract class and Interface
can only be used after extending and proviiding concrete implementation of it.
That’s all on What is Inheritnace in Java, How to use Inheritance
and some specific rules of Inheritance in Java programming language. In Summary
we can say that Inheritance is one of the most important features of Object
Oriented Programming (OOPS) and Java. Inheritance is the concept that is used
for code reusability purpose. The concept of Inheritance in Java and OOPS is
used to make the things from general to more specific.
Other Java and OOPS Tutorials you may like:


why subclass reference cannot refer superclass object?
ReplyDeleteIt's designed like that in Java programming. One reason is that because Subclass may contain more functionality than Super class. See here for more on What is Inheritance in Java
Delete