Type Of Relationships In Java

Type Of Relationships In Java :

  • Based on reusing the data members from one class to another class.
  • Is-a relationship
  • Has-a relationship
  • Uses-a relationship
  • Is-a relationship
  • One in which data members of one class are obtained into another class through the concept of inheritance.
Type Of Relationships In Java
class Base {
    public void m1() {
    }
}

class Derived extends Base {
    public void m2() {
    }
}

class Main {
    public static void main(String[] args) {
        Base b = new Base();
        b.m1(); // valid
        b.m2();
        /* Compile time error : cannot find symbol
                                 symbol : method m2()
                                 location : variable b of type Base */

        Derived d = new Derived();
        d.m1(); // valid
        d.m2(); // valid

        Base b = new Derived();
        b.m1(); // valid
        b.m2();
        /* Compile time error : cannot find symbol
                                symbol : method m2()
                                location : variable b of type Base */
        Derived d = new Base();
        /* Compile time error : incompatible types
                                required : Derived
                                found : Base */
    }
}
  • Has-a relationship
  • One in which an object of one class is created as a data member in another class. Mostly we use new keyword.
  • It is also known as “Composition or Aggregation”.

Disadvantage

Has – a relationship is that it increases dependencies between classes and create maintenance problems.

class A {
}

class B {
    A obj1 = new A();
}
  • In Has – a relationship, we create an object of class A in the context of class B. In the context of class B, physical memory space is created for the features of class A. In general, if multiple classes creates an object of class A then in the context of all the classes, memory space is created for data members of class A explicitly.
  • Uses-a relationship
  • One in which a method of one class is using an object of another class.
class A {
}

class B {
void m1() {
        A obj1 = new A();
    }
}
  • In this relationship as long as m1() method of class B will be the executing, object of class A will be available and when m1() method completes its execution then the object of class A will be destroyed.
  • Note
  • The default relationship in Java is ‘Is-a relationship’ because for each and every class in Java there exists an implicit predefined super class known as java.lang.Object.
  • System.out is the universal example for ‘Has-a relationship’ because out is an object of PrintStream class created as static data member in another predefined class called System. The relationship between System and PrintStream class is known as Has-a relationship.
  • Each and every execution logic method i.e. main() method of execution logic class is using an object of business logic class is known as Use-a relationship.

The flutter tutorial  is a website that bring you the latest and amazing resources of code. All the languages codes are included in this website. The languages like flutter, android, java,kotlin etc.with the help of this languages any user can develop the beautiful application

For more information about Flutter. visit www.fluttertutorial.in