# Relationships between classes in JAVA

There are mainly two types of relationships between classes in JAVA

* Inheritance (IS-A)
    
* Association (HAS-A)
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1673949033555/3290b864-2445-466d-b526-8398b400f050.png align="center")

## Inheritance

In inheritance, the child class object carries the body of the parent class when initiated. This is achieved using the "extends" keyword.

```java
class Vehicle
{
    
}
class car extends Vehicle
{
    
}
```

## Association

Association is the relation between two separate classes established through objects. There are two forms of association

* Aggregation
    
* Composition
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1673949388326/a087878d-a68b-452b-a4af-84e749d44fcf.png align="center")

In Aggregation, both entries can survive individually, which means ending one entity will not affect the other.

For example, consider a car having a music player and an engine. The music player can work even without a car; this indicates aggregation, whereas an engine cannot work without a car this is an example of composition.

```java
class Vehicle
{
    
}
class car 
{
    Engine e=new Engine();
}
```

### Advantages

* Code reusability
    
* Cost cutting
    
* Reduce redundancy
