Tuesday, April 17, 2018

Inheritance in java

In the last tutorial, we talked about Object Oriented Programming, and we mention various concepts in OOP. In that same tutorial, we went into detail about the concepts of Objects and Classes in Java.
In this tutorial, we’ll be dealing with one of the concepts we mention in the last one: Java Inheritance

Before we move further let see what all topic we have covered previously
  1. Making Games With Java- Introduction
  2. Getting Everything you need for Java
  3. Introduction to Java Programming
  4. Basic data types and Variables in java
  5. Basic Data types and Variables in java - Continued...
  6. User Input using Scanner class in java
  7. Conditional Logic in java
  8. Loops in java
  9. Arrays in java
  10. Wrapper classes in java
  11. Methods in java
  12. What are Object Oriented Programming Practices
What is Java Inheritance?
Inheritance in Java is the process where one object or class acquires all the states (or properties) and behaviour of another object/class. The class whose states and behaviours is being acquired is known as the parent class or superclass, while the one that inherits is called the subclass.
With inheritance, we can create new classes built on existing classes; allowing you to reuse methods and variables from our parent class, add new ones, and arrange our data in a hierarchal order.
Inheritance in Java represents what is known as a parent-child relationship.



What do we use Inheritance in Java?

A major reason for inheritance in Java is for code reusability, as the subclass inherits everything from its parent class, including all variables and methods.
The extends keyword
The extends keyword is used to indicate that the class being created is inheriting properties from an existing class.
An Inheritance example
The following image shows an example of inheritance in Java, whereas a class named Puppy, inherits the properties of our parent class Dog.


 package animal;


 public class Dog {

  public void dogStats(String name, int age){
   System.out.println("His name is " + name);
   System.out.println("He is " + age + " year(s) old");
  }
  
 }

 class Puppy extends Dog{

  ppublic void pupStats(String name, int age){
   System.out.println("His name is " + name);
   System.out.println("He is " + age + " week(s) old");
  }
  
 }
Main Class looks like below:

 public static void main(String[] args){
  
  Puppy first = new Puppy();
  first.dogStats("Doug",1);
  first.pupStats("Junior",3);
  
  
 }
Result :
        His Name is Doug
 He is 1 year(s) old
 His name is Junior
 He is 3 week(s) old
Explaining the Inheritance example
In our given program, a Dog class is created, with the dogStats method, which takes in String and int variables and return the set print statements. When the Puppy class is created, which inherits from the Dog class, a copy of the parent’s contenst is created within it. This is why we can access the members of the Dog class using the Puppy class.
Trying to access the Puppy class with the Dog class will give an error.
The superclass reference variable can hold the subclass object, but using it will only access the members of the superclass. To access both, you’ll have to create reference variables to the subclass.
Note that the subclass inherits all the members from the super class. Members refers to the fields, methods and nested classes. Constructors are not members, and so cannot be inherited. They can be invoked from the subclass, though.

The IS-A Relationship

Also known as the parent-child relationship, an IS-A relationship simply means that an object is a type of another object. This form of relationship is achieved in inheritance:

Based on the example above, we can say the following:
  • The Animal class is the parent of the Mammal and Reptile classes.
  • The Dog class is the child or subclass of the Mammal class
  • The Lizard class is the child or subclass of the Reptile class
  • In an IS-A relationship, we may as well say:
  • Mammal IS-A Animal
  • Reptile IS-A Animal
  • Dog Is-A Mammal
  • Lizard IS-A Animal
  • The HAS-A Relationship
  • Unlike the IS-A relationship, this is based mainly on usage, or behaviour, or basically what an object has. This helps to reduce bugs and duplication of code.


For example:
Basically, we’re saying that the Dog HAS-A Tail. By having a class dedicated solely to the tail, we don’t have to put all the code for the tail in the Dog class, allowing us to reuse if for other classes that may require a tail; like a Cat class for example.

In OOP, the users do not have to worry about which object is actually doing the work. So the Dog class will hide implementation details from its users. Therefore, when the Dog class is to perform an operation, it will either do it by itself or ask another class to do it.
Types of Inheritance

There are about four type of Inheritance in Java:
  • Single Inheritance: This takes place when a parent class had only one sub class.
  • Multi-Level Inheritance: Here, the parent class has one subclass, and that subclass has its own subclass.
  • Hierarchical Inheritance: In this form of inheritance, the parent class can have two or more subclasses.
  • Multiple Inheritance: The opposite of Hierarchical Inheritance, this is achieved when a class has two or more parent classes. This is not supported in Java, though, so you cannot do this:
class Mammal extends Dog, Cat{

}       
                                             
A class can implement one or more interfaces, eliminating the need to perform that form of inheritance.


Next tutorial, we shall deal with Method Overriding.
Hope you like this article , Please share it with your friends and colleagues.

Also Read :




Thanks for reading
Noeik 



0 comments:

Post a Comment