Inheritance in JAVA

Inheritance in Java is a mechanism in which one object  obtains all the characteristics and behaviors of a parent object. It is an important part of OOPs (Object Oriented programming system).

The idea behind inheritance in Java is that you can create new classes  that are dependent upon existing classes. When you inherit from an existing class, then you are able to reuse methods and attributes of the parent class. Moreover, you can add new methods  and attributes in your current class too.

Inheritance represents the IS-A relationship which is also known as a parent-child relationship.

Child Class:
The class that extends the features of another class is known as child class, sub class or derived class.

Parent Class:
The class whose properties and functionalities are used(inherited) by another class is known as parent class, super class or Base class.

Why use inheritance in java

Reusibility of the code - It allows us to reuse of the code. it improves reusability in  java application.The biggest advantage of Inheritance is that the code that is already written  in parent class need not be rewritten in the child class.

The syntax of Java Inheritance

  1. class Childclass-name extends Parentclass-name  
  2. {  
  3.    //methods and fields  
  4. }  

Types of inheritance in java

On the basis of class, there can be three types of inheritance in java: single, multilevel and hierarchical.

Single Inheritance Example

When a class inherits another class, it is known as a single inheritance.

Multilevel Inheritance Example

When there is a chain of inheritance, it is known as multilevel inheritance.

Hierarchical Inheritance Example

When two or more classes inherits a single class, it is known as hierarchical inheritance.

 

 
Object is an instance of a class while class is a blueprint of an object. An object represents the class and consists of properties and behavior.
A class is a group of objects which have common properties. It is a template or blueprint from which objects are created. It is a logical entity. It can't be physical. A class in Java can contain: Fields,Methods, Constructors,Blocks, Nested class and interface
A method is a block of code, which only runs when it is called. You can pass data, known as parameters, into a method. Methods are used to perform certain actions, and they are known as functions.
An attribute is another term for a field. It's typically a public constant or a public variable that can be accessed directly. In this particular case, the array in Java is actually an object and you are accessing the public constant value that represents the length of the array.