Java Aggregation

Aggregation can be said as a relation among two classes that is best described as a “has-a” and “whole/part” relationship. It is a more specialized version of the association relationship. Now what is association? In Java, when you think of a class having that has member of different type, then there an association can be formed. In a Java class, where there lies an entity reference, it becomes an aggression. Let us take a situation, Student object contains many information such as roll, name, email_add etc. It contains one more object named rank, which contains its own information such as subj_Name, marks, board_name, exam-type etc. as given below.
class Student{
  int roll;
  String name;
  Rank ranking;
}
In this case, Student has an entity reference ranking, so relationship is Student HAS-A ranking Aggregation is also used for code reusability. Aggregation is a form of Association where the relation of Association can be considered the containing class ‘owning’ the contained class. The lifetime of that relationship cannot be defined. ‘Owning’ can be determined as a single-direction Association.

Why Programmers use Aggression in Java?

As told earlier, for code reusability. Let us consider a case where we have used two classes, student class and another id class along with staffs and teacher; in order to maintain Student id, teachers’ id, and staff id, programmers do not need to use the same code again and again. Just use the reference of id class while defining each of these classes. This can be written as:
Student Has-A id (Has-a relationship between student and id)
Staff Has-A id (Has-a relationship between staff and id)
teachers Has-A id (Has-a relationship between teachers and id)

What is Method Overriding in Java?

Declaring a method in subclass which already exists there in parent class is known as method overriding. When a class is inheriting a method from a super class of its own, then there is a option of overriding the method provided it is not declared as final. The advantage of using overriding is ability to classify a behavior that’s specific to the child class and the child class can implement a parent class method based on its necessity. There are certain rules that a programmer should follow in order to implement over riding. These are:
  • In java, a method can only be written in child class and not in same class.
  • Argument list should be exactly the same as that of the overridden method of that class.
  • Instance methods can also be overridden if they are inherited by the child class.
  • A constructor cannot be overridden.
  • Final – declared methods cannot be overridden.
  • Any method that is static cannot be used to override.
  • The return type must have to be the same or a subtype of the return type declared in the original overridden method in the parent class.
  • If a method cannot be inherited then it cannot be overridden.
  • A child class within the same package as the instance’s parent class can override any parent class method that is not declared private or final.
  • A child class in a different package can only override the non-final methods declared as public or protected.

Here is a Java Program to demonstrate how over riding takes place within a program

Example:

class college{
public void move(){
 System.out.println("College is open");
 }
  }
   class univ extends college{
   public void move(){
   System.out.println("University is open too");
 }
  }
  public class stud{
    public static void main(String args[]){
    college a = new college();
    college b = new univ();
    a.move();
    b.move();
   }
}

No comments:

Post a Comment