What is Inheritance in Java with example - Object Oriented Programming Tutorial

What is Inheritance in Java
Inheritance in Java or OOPS (Object oriented programming) is a feature which allows coding reusability.  In other words, Inheritance  self-implies inheriting or we can say acquiring something from others. Along with Abstraction, Encapsulation and Polymorphism, Inheritance forms the backbone of Object oriented programming and Java.  In Java, we use the term inheritance when one object acquires some property from other objects. In Java, inheritance is defined in terms of superclass and subclass. it is normally used when some object wants 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.

By using Inheritance between Superclass and Subclass, a IS-A Relationship is formed which means you can use any subclass object in place of the super class object e.g. if a method expects a superclass object, you can pass a subclass object to it. Inheritance in Java is also used to provide a concrete implementation of abstract class and interface in Java.


The best way to learn object-oriented programming is following both Head First Java and Head First object-oriented analysis and design, followed by Head First design pattern.These three books have taught me most of the object oriented programming concepts I know today. It's the Head First design pattern which taught me how to write flexible code using Inheritance and Polymorphism.




Inheritance in Java- Things to remember

Here are some important points about Inheritance in Java which is worth remembering:
  1. One subclass can extend only one super class in Java but it can implement the multiple interfaces.
  2. A private member of the 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. The constructor in Java is not inherited by the subclass.
  5. If a class implements Interface or extends an abstract class, it needs to override all abstract methods untile it is not abstract.
  6. Multiple inheritances are not supported in java but we can achieve this by using interface.One class can implement multiple interfaces
  7. In Java class never extends the interface rather it implements interface
  8. One interface can extend another 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 the property of that class so always one class extends another class. While implements are used when some interface is there and some class wants to give an implementation of that interface according to depending on their requirement.

What is Inheritance in Java with Example



Example of Inheritance in Java
Now that we know what is Inheritance in Java and some specific property of Inheritance, its time to see a real life example of Inheritance in Java. This program will help you to learn how to use Inheritance object oriented concept in your Java code. 

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;
 }
}


In above example of Inheritance in Java, Currency is called parent class while Rupee is a child of Currency class. In object oriented programming term, Currency is super class while Rupee is a 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 providing a concrete implementation of it.


That’s all on what is Inheritance 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.



3 comments:

  1. why subclass reference cannot refer superclass object?

    ReplyDelete
    Replies
    1. It'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
  2. Currency class should have been declared as abstract.

    ReplyDelete