Java4teachus

Monday, July 22, 2019

Types Of Relationships in java

Relationships in Java

The purpose of relationships in java is that how to reduce the features from one to another class. In java programming language we have 3 types of relationships. They are:
  1. Is-A relationship.
  2. Has-A Relationship.
  3. User-A Relationship.
➤ Is-A Relationship

In Is-A Relationship, one class is obtain the features of another class by using  inheritance concept with the help of extends keyword.

Example
Is-A-Relationship-in-java.jpeg

Is-A relationship always follows the logical memory management. The limitation of Is-A relationship is that unable to solve the Ambiguity problem which is available in multiple inheritance and it can be solved by "HAS-A" relationship.

program:


class Salary
{
float x= 20000.0f;
}
class Bonus extends Salary
{
 float y=2000.0f;
}
class EmployeSal
{
public static void main(String l[])
{
Bonus s=new Bonus();
System.out.println("Salary is:"+s.x);
System.out.println("Bonus is:"+s.y);
}
}

OutPut
Salary is : 20000.0
Bonus is : 2000.0

➤ HAS-A Relationship


In HAS-A Relationship an object of one class is created as a data member in the content of another class.

HAS-A-Relationship-in-java.jpeg
Here the relationship Between C1 & C2  is "HAS-A Relationship". It is always follows the physical memory management.

The advantages of HAS-A relationship is that to eliminate the ambiguity problems which is shown in the below example.

HAS-A-Relationship-Example-in-Java.jpeg
The above image has invalid code segment  and can be written as  the follows by applying "HAS-A" Relationship  as  valid code.
HAS-A-Relationship-example-in-java.jpeg
Program

class Employee
{
    float salary=20000.0f;
}
class Developer extends Employee
{
    float Bonus=2000.0f;
}
class  EmployeeDeveloper
{
    public static void main(String l[])
    {
        Employee e=new Employee();
        Developer d=new Developer();
        float a=e.salary+d.Bonus;
        System.out.println("Employee Salary :"+a);
    }
}


OutPut

HAS-A-Relationship-in-java.jpeg
➤ User-A Relationship

In User-A Relationship method of one class is using of another class the relationship between two classes.

USER-A-Relationship-in-java.jpeg
Program

class Employee

{
    float salary=20000.0f;
}
class  Salary extends Employee
{
    void show()
    {
        float bonus=2000.0f;
        float totalsal=salary+bonus;
        System.out.println("DevloperSalary :"+totalsal);
    }
}
class DevloperSalary
{
    public static void main(String l[])
    {
        Salary s=new Salary();
        s.show();
    }
}
Output
USER-A-Relationship-program-in-java.jpeg

Note 1: The default relationship in java "IS-A Relationship" because  for each every class in java their exist implicit predefined super class called   java.lang.Object.

 
Note 2: System.out  is a universal example for "HAS-A" Relationship. Here out is an object of PrintStream class and it is created as static data member class called System. The Relationship between the System PrintStream called  "HAS -A" Relationship.


Note 3: Each and every execution logic method (main()) of execution class is using logic of business logic class. Relationship between execution logic class and business logic class is called "USER-A".

2 comments:

  1. Oh my gosh! Programming looks so hard! I often want to learn how to program, but as soon as I see stuff like this I realize how overwhelming the process would be and I lose faith that I'd be able to do it! Good work in having mastered computer programming!

    Google Chrome Scroll Solution

    ReplyDelete
    Replies
    1. Thanks alot...
      Thanks for visiting my site ...

      Delete

Thanks for visiting my site..