wellnwill Marathahalli Bangalore

Class Code Reusability


The meaning of reusability is write once and use anywhere.

We can reuse the class code in the following ways

  • Association (has a/part of relationship).
  • Inheritance (is-a relationship)

Association

Association represent ‘has a’ or ‘part of’ relationship between two classes or two objects. Association can be represented in the following two ways.

Composition represents ‘strong has a’ relationship between two classes or two objects.

Ex- Bike has engine reference i.e. there is a strong relationship between Bike and engine because there is no use of bike without engine.


Composite object

An object is known as composite if it contains the address of another object.


Aggregation

Aggregation represents ‘weak has a’ relationship between two classes and objects.

class Address
{
   int houseNumber;
   String city;
   String state;
   String country;
   
   Address(int houseNumber, String city, String state, String country)
   {
       this.houseNumber=houseNumber;
       this.city =city;
       this.state =state;
       this.country =country;
   }
}
class Person
{
   int adhaarNum;
   String personName;
   Address ad; 
   
   Person(int adhaarNum, String personName, Address ad)
   {
       this.adhaarNum=adhaarNum;
       this.personName=personName;
       this.ad=ad;
   }
   
void displayDetails() 
 {
  System.out.println("Adaar number: "+adhaarNum);
  System.out.println("person name :"+personName);
  System.out.println("houseno:"+    ad.houseNumber);
  System.out.println("person city :"+ad.city);
  System.out.println(ad.state);
  System.out.println(ad.country);
   }
}

class Test
{
   public static void main(String args[])
   {
       Address ad1 = new Address(26, "Bangalore", "Karnatka", "India");
       Person p1 = new Person(555, "Ram",ad1);
       p1.displayDetails();
   }
}
Output-555,Ram,26,Bangalore,Karnakata,india








Inheritance

  • Acquiring members from one class to another class is known as inheritance.
  • With the help of inheritance concept we can create a new class by using the properties and methods of existing class.
  • The existing class from which the members are acquiring is known as super class or base class or parent class.
  • The newly defined class to which members are acquired is known as subclass or derived class.

Advantage of inheritance

  • To reuse the code.
  • To achieve runtime polymorphism.
Syntax --

class  extends 
{

}
class Parent
{
int age=50;
static String name="Parent";

void saving()
{
 System.out.println("Parent's saved money");
}

void lifeStyle()
{
 System.out.println("eat, sleep, work");
}

}
class Child extends Parent
{
int age=21;
static String name="child";

void keepLearning()
{
 System.out.println("keep Learning");
}
}

class Test192
{
 public static void main(String args[])
 {
  Parent p1=new Parent();
  p1.lifeStyle();
  Child c1=new Child();
  c1.saving();
  c1.lifeStyle();
  c1.keepLearning();
  System.out.println(p1.age + p1.name);
  System.out.println(c1.age + c1.name);
  System.out.println(c1.age + Child.name);
  } 
}
Output-eat, sleep, work
Parent’s saved money
eat, sleep, work
keep learning
1 Parent
2 child
2 child






  • We can access parent class members as well as child class members by using child class object.
  • Child class members cannot be accessed from parent class object.
  • Static members are also inherited to child classes including main() method. While execution of child class if child class doesn’t have main() method then parent class main() method will be executed.
  • We cannot create child class for final classes.
class Parent1
{
void saving()
{
 System.out.println("Parent's saved money");
}
public static void main(String args[]){
  Parent1 p1=new Parent1();
  Child1 c1=new Child1();
  c1.saving();
  c1.lifeStyle();
}
}
class Child1 extends Parent1
{
void lifeStyle()
{
 System.out.println("eat, sleep, work, play, party");
}
void keepLearning()
{
 System.out.println("keep Learning");
}
}
//Execution either by Child1 class or Parent class
Output-Parent’s saved money
       Eat,sleep,work,play,party


class Parent
{
static String name="Parent";
void saving()
{
 System.out.println("Parent's saved money");
}
void lifeStyle()
{
 System.out.println("eat, sleep, work");
}
public static void main(String args[])
{
System.out.println("parent class main()");
}
}
class Child extends Parent
{
static String name="child";

void keepLearning()
{
 System.out.println("keep Learning");
}
}
Output-Parent class main()







class Parent
{
static String name="Parent";

void saving()
{
 System.out.println("Parent's saved money");
}
void lifeStyle()
{
 System.out.println("eat, sleep, work");
}
public static void main(String args[])
{
System.out.println("parent class main()");
}
}

class Child extends Parent
{
static String name="child";

void keepLearning()
{
 System.out.println("keep Learning");
}

public static void main(String args[])
{
System.out.println("child class main()");
}
} 
Output-child class main()

In java, Object class is the super class of all the java classes. When a class inherits any parent class then Object class becomes indirect super class.


Members which are not inherited to sub class

Constructors, Blocks and private members.


Footer with Map

Top