Inheritance

INHERITENCE

0  0 2016-04-22 09:22:38

Inheritance in java is a working in which one object acquires all the properties and behaviours of parent object. Different kinds of objects often have a certain value in Object-oriented programming allows classes to inherit commonly used state and behaviour from other classes. In the Java programming language, each class is allowed to have direct superclass. Also each superclass has the potential for an unlimited number of subclasses.

The idea behind inheritance in java is that you can create new class or classes that are built upon existing classes. When you inherited from an existing class, you can use methods and fields also re-use these methods and fields of parent class, and you can add new methods and fields also.

Inheritance represents the IS-A relationship, also known as parent and child relationship.

 

The syntax for creating a subclass is simple. At the beginning of your class declaration, use the extends keyword, followed by the name of the class to inherit from:

Syntax of Java Inheritance

class Subclass-name extends Superclass-name  

{  

   //methods and fields  

      }  

 

 

class B extends A {

 

    // new fields and methods defining

}

This gives ‘B’ all the same fields and methods as A, yet allows its code to focus exclusively on the features that make it unique. This makes code for your subclasses easy to read. However, you must care to properly document the behaviour and state that each superclass defines, since that code will not appear in the source file of each subclass.

Why use inheritance in java

  • For Method Overriding, not overloading (achieved runtime polymorphism).
  • For Code Reusability.

Extends keyword indicates that you are making a new class that derives from an existing class or parent class.

In the terminology of Java, a class that is inheriting is calling a super class. The new class is call child class or a subclass.

Understanding the simple example of inheritance



 

As per displayed in the above fig., Programmer or developer is a subclass and Employee is the superclass. Relationship between employee and programmer classes is Programmer IS-A Employee. It’s mean that Programmer is a type of Employee.   

Types of inheritance in java

On the basis of class, there can be three types of inheritance in java: single inheritance, multilevel inheritance and hierarchical inheritance. In java programming multiple inheritance and hybrid inheritance is supported through interface only.



 


 

Comments (0)

Post Your Comments

Headquarter



Hong Kong Office



Find Us On

Website Development Company
SEO
Digital Marketing
website development company in Hong Kong
website designer in pune