Friday, April 28, 2017

Relationship In Java (Inheritance and Aggregation)

Relations in Java

Inheritance (IS-A Relation )

The process where one class derived from other classes, there by inherits the fields and methods of other classes. This process in java is called Inheritance.  It is also called IS-A relation between the classes.

A class that is derived from another class is called Sub Class or child Class or derived Class and the Class from which class is derived is called Super Class or parent Class or base class.


Types of Inheritance:

  1. Single inheritance:  if a class derived from another class only it is called single inheritance.E.g. suppose class B derived from class A only

  2.  Multiple inheritance: - if a class is derived from more than one class than this type of inheritance is called multiple inheritance.E.g. Suppose Class B derived from class A , Class C and Class D .




  3. Multi Level Inheritance:  if a class is derived from another class and its super class is also derived from some other class this type of inheritance is called multi level inheritance.E.g. Class B derived from Class A and Class A derived from Class X. it mean class B is not the direct child of class X


  4. Cyclic inheritance: if a Class derived from another class and its super class derived from its child. This type of inheritance is called cyclic inheritance.
    E.g Class B derived from Class A and Class A Derived From Class B.


    If a class derived its self, then it is also called Cyclic Inheritance. E.g. Class A derived from Class A

Inheritance in Java

In java by using extends keyword we can implement inheritance or established IS-A relation between the Classes. Java don’t support multiple and cyclic inheritance.
The main advantages of IS-A relation is reusability of a code.

A sub class inherits the entire member (field, methods and nested inner classes) from its super class except private member. Constructors are not the members, so they are not inherited by sub class But Super class constructor must be invoked from subclass for initializing the super class data members.
Whatever the field and methods parent class have by default available to child class and we can call access them by using child reference BUT whatever the methods and field child Class have not available to the parent class and by using parent reference we can’t access child specified members.

Parent reference can be used to hold the child class object but by using this reference we can only access parent specified member from child object. And we can’t access child specified member by using parent reference even though it refer child object.
Child class reference can’t hold parent class object.
Private member can’t be access by the child class but private field or variables can be access indirectly by child class using methods and inner classes declared in parent class.

The most common method which is applicable for all child class we have to define it in parent class and the specific method which are applicable for particular child only we define those method in child class.
The object class act as root class for all java classes in java API.
Every class directly or indirectly a child of Object class i.e. Object class is the only class in java which don’t have any parent class.

If our class extends some other Class than our class is not the direct child of Object class but it is indirect child of Object class. Hence, if our class doesn’t extends any class than only our class is a Direct Child of Object Class.
The most common methods which are applicable for all java Object is define inside a Object Class of java which is present inside java.lang package.

Reason for why java do not support multiple inheritance at class level:- There is chance  of ambiguity problem. Suppose both super class contain one method with same name and child invoke the method than which super class method is invoked there is ambiguity problem therefore java doesn’t support multiple inheritance at class level.

Disadvantage of Inheritance:

  1. Sub class object can get the functionality only once. We can’t inherit a data member twice.
  2. Sub class Object gets All functionality of super Class even though it required or not.
  3. There should be tight coupling between the classes mean if we make change in one class effect the other class.
  4. One class can extend at most one class only. If a class need functionality of two different classes than it’s not possible to extends both class.


Association (Has-A relation)

In Object oriented Programming, One Object is related two other object to use the functionality and services provided by the other object. This relation between two objects is called Association or Has-A relation.
The object which using the functionality of other object is called Container Object and the object whose functionality is used are called Contained Object.

Association is of two types:

  1. Aggregation: if there is chance of existing contained object without the existence of container object i.e.  if container object is weakly associated with contained object that this type of association is called aggregation.
    E.g. relation between University and professor Object
  2. Composition:  without existing container object if there is no chance of existing contained object than this association is called composition. Here the container and contained object are strongly associated to each other.
    E.g. Relation between University and its department Objects. 
Note: In Composition Container Object directly holds the contained object where as in aggregation Container Object Only Holds the reference of contained object class.


        


No comments:

Post a Comment