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 that 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 an existing feature of some class and also wants to provide some special feature, so we can say inheritance has given the advantage of reusability.

By using Inheritance between Superclass and Subclass, an 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 the 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 that 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 multiple interfaces.
  2. A private member of the super class can not be inherited in subclasses like private fields and private methods.
  3. Default members can only be inherited in the 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 until it is not abstract.
  6. Multiple inheritances are not supported in java but we can achieve this by using the interface. One class can implement multiple interfaces
  7. In Java class never extends the interface rather it implements an 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 properties of Inheritance, it's time to see a real-life example of Inheritance in Java. This program will help you to learn how to use the Inheritance object-oriented concepts 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 the above example of Inheritance in Java, Currency is called parent class while Rupee is a child of Currency class. In object-oriented programming terms, Currency is superclass 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 the 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 purposes. The concept of Inheritance in Java and OOPS is used to make things from general to more specific.


Other BooksJava, and OOP Tutorials you may like:

Thanks for reading this article so far. If you like an object-oriented programming tutorial then please share it with your friends and colleagues. If you have any questions or feedback then please drop a note.

8 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
  3. can parent class inherit the properties of child class? why or why not?

    ReplyDelete
  4. No, because it was designed in Java that subclass can access to parent

    ReplyDelete

Feel free to comment, ask questions if you have any doubt.