What is IS-A and HAS-A relationship in JAVA?

IS-A Relationship:

IS-A relationship is java represents Inheritance. It indicates that, "Class A is of Class B type". 
ex: Banana is a fruit, Car is a vehicle.
Inheritance is unidirectional as banana is a fruit , but all fruits are not bananas. 
        It is a key point to note that you can easily identify the IS-A relationship. Wherever you see an extends keyword or implements keyword in a class declaration, then this class is said to have IS-A relationship.

HAS-A Relationship:

Has-A means an instance of one class “has a” reference to an instance of another class or another instance of same class.There is no specific keyword to implement HAS-A relationship but mostly we are depended upon “new” keyword.
HAS-A relationship is  also called as "Composition". Composition(HAS-A) simply mean the use of instance variables that are references to other objects. For example Maruti has Engine, or House has Bathroom.

Summary:

  • IS-A relationship based on Inheritance, which can be of two types Class Inheritance or Interface Inheritance.
  • Has-a relationship is composition relationship which is a productive way of code reuse.

What is Inheritance in Java?

Inheritance is one of the features of Object Oriented Programming(OOPs). It is a mechanism in which an object acquires all the properties of its parent object. Inheritance allows one class to use variables and methods of other class.The derived class is called subclass and the base class is called as super-class. The derived class can have its own variables and methods.

Inheritance represents the IS-A relationship, also known as parent-child relationship.
Inheritance is a compile-time mechanism. A super-class can have any number of sub classes. But a subclass can have only one super-class. This is because Java does not support multiple inheritance.

Syntax:

class Subclass-name extends Superclass-name  
{  
   //methods and fields  
}  

The extends keyword indicates that you are making a new class that derives from an existing class. The meaning of "extends" is to increase the functionality.

Types of inheritance in java:

Java has 3 types of inheritance
  1. Single
  2. Multilevel
  3. Hierarchical













What is Java Programming Language?

Java is a general-purpose high-level computer programming language. Java is concurrent, class-based, object-oriented language. Java supports WORA(write once, run anywhere), the compiled java byte code can run on all platforms that supports java without the need for recompilation.Java applications are typically compiled to bytecode that can run on any Java virtual machine (JVM) regardless of computer architecture.Java is the popular language in use for client-server web applications.