has a relationship java code example
Example 1: has a relationship in java
‘Has a ‘ relationship is also known as “composition or Aggregation”.
As in inheritance we have ‘extends’ keyword we don’t have any keyword to
implement ‘Has a’ relationship in java. The main advantage of ‘Has-A‘
relationship in java code reusability. For has a relationship we use new keyword
Ex: Car has an engine. We cannot say Car in an engine.
Example 2: is a relationship in java
‘is a’ relationship is also known as inheritance. We can implement
‘is a’ relationship or inheritance in java using extends keyword.
The advantage of inheritance or is a relationship is reusability of code
instead of duplicating the code.
Ex :
Motor cycle is a vehicle
Car is a vehicle
Both, car and motorcycle extends vehicle.
Example 3: is a and has a relationship in java
class Room {
Table table = new Table();
}
Example 4: is a and has a relationship in java
class Apple extends Fruit {
}
Example 5: is a relationsiop in java
public class Actress extends Person { public String readScript(String movie) { return "Reading the script of " + movie; }