Monday, 24 August 2015

Java Tutorial : Inheritance Has A Relationship (Car)

🚀 Master Java Programming Today!

Join the Ram N Java community for the simplest coding tutorials.

SUBSCRIBE TO CHANNEL

Understanding the "HAS-A" Relationship in Java

In the world of Java, building complex systems requires us to connect different classes. While Inheritance (IS-A) is popular, the HAS-A Relationship (also known as Composition or Aggregation) is often more flexible and powerful for real-world scenarios.

What Exactly is a HAS-A Relationship?

A HAS-A relationship occurs when one class uses an instance of another class as a member variable. Instead of inheriting properties, the class "owns" or "has" those properties through another object.

Example: A Car has an Engine. The Car doesn't "become" an Engine (which would be inheritance); it simply contains one to function.

Key Components in the Video

In this tutorial, we break down the logic using two main entities:

  • The Car Class: This acts as the main container.
  • The Engine/Specific Parts: These are separate classes that define specific behaviors.

Why Should You Use HAS-A?

  1. Code Reusability: You can use the same Engine class for a Car, a Truck, or a Boat.
  2. Better Flexibility: You can easily change the parts of a class without rewriting the entire inheritance tree.
  3. Simplicity: It keeps your classes focused on one specific job (Single Responsibility Principle).

Recommended Tutorials for You


Click here to watch in Youtube :
https://www.youtube.com/watch?v=lQlu0kel2x8&list=UUhwKlOVR041tngjerWxVccw

Click the below Image to Enlarge
Java Tutorial : Inheritance Has A Relationship (Car)
Java Tutorial : Inheritance Has A Relationship (Car)
Car.java
public class Car
{

    private String color;
    private int maxSpeed;

    public Car(String color, int maxSpeed)
    {
        super();
        this.color = color;
        this.maxSpeed = maxSpeed;
    }

    public String getColor()
    {
        return color;
    }

    public void setColor(String color)
    {
        this.color = color;
    }

    public int getMaxSpeed()
    {
        return maxSpeed;
    }

    public void setMaxSpeed(int maxSpeed)
    {
        this.maxSpeed = maxSpeed;
    }

    public void carInfo()
    {

        System.out.println("Car Color= " + color + ", Max Speed= " + maxSpeed);

    }
}
Engine.java
public class Engine
{
    public void start()
    {

        System.out.println("Engine Started:");

    }

    public void stop()
    {

        System.out.println("Engine Stopped:");

    }
}
MarutiSwift.java
/**
 * 
 * MarutiSwift is specific type of Car which extends Car class means MarutiSwift
 * IS-A Car.
 * 
 * MarutiSwift extends Car and thus inherits all properties and methods from Car
 * (except final and static).
 * 
 * MarutiSwift can also define all its specific functionality.
 */
public class MarutiSwift extends Car
{
    /*
     * MarutiSwift HAS-A Engine
     */
    private Engine engine;

    public MarutiSwift(String color, int maxSpeed, Engine engine)
    {
        super(color, maxSpeed);
        this.engine = engine;
    }

    public void startMarutiSwift()
    {
        engine.start();
    }

}
RelationshipDemo.java
/**
 * RelationsDemo class is making object of MarutiSwift class and initialized it.
 * Though MarutiSwift class does not have setColor(), setMaxSpeed() and carInfo()
 * methods still we can use it due to IS-A relationship of MarutiSwift class with Car
 * class.
 */

public class RelationshipDemo
{

    public static void main(String[] args)
    {

        Engine engine = new Engine();
        MarutiSwift marutiSwift = new MarutiSwift("Red", 200, engine);
        marutiSwift.carInfo();
        marutiSwift.startMarutiSwift();
    }
}

Output
Car Color= Red, Max Speed= 200
Engine Started:
To Download InheritanceDemo-HAS-A-CarApp Project Click the below link
https://sites.google.com/site/javaee4321/java/InheritanceDemo-HAS-A-CarApp.zip?attredirects=0&d=1

See also:

  • All JavaEE Viedos Playlist
  • All JavaEE Viedos
  • All JAVA EE Links
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java Collection Framework Tutorial
  • JAVA Tutorial
  • No comments:

    Post a Comment

    Tutorials