What is Code Reusability in java ?

Real life example like our laptop battery is discharge the charge it and reuse it.

Like Gas cylinder if empty then we refill it again .To save cost same thing coding.


Reusability


1) Association (has-A)

i)composition(Strong)

ii) Aggregation(Weak)


2) Inheritance (is-A)


Without relation reusability is not possible.

Benefits of reusability in code is 1 already developed program used in another program .To avoid ambiguity in code


Car is a vehicle and bike is also vehicle and when you got the relation is (is-a) then always prefer for inheritance. If relation is (has –a) then go for association. Car has a (engine,music system). When between 2 classes relation is (has-a) then use association.

But when we remove the engine then car become useless then its example of composition. Music system has a relation with car but its an aggregation without music system we can have car.

If u want [0-100] % reusability then go for inheritance

If partially then go for association.


In Coding we create constructor for strong and methods for weak thing

Like we have an example of human ,human have hair heart ,eyes human

Can live without hair but not without heart.


class Hand{

void hand() {

System.out.println(“Hand is weak”);

}

class Heart{

Heart()

{

System.out.println("Heart is Strong");

}

}

class Human{

Hand hand; // aggregation in java

Heart heart;

Human(){

Hand=new Hand(); //composition in java

new Heart(); //association in java

}

public static void main(String args[])

{

Human h = new Human();

h.hand.hand(); //OGNL(object graph navigation language) in struts

}}




Inheritance simply means child inherits behavior and properties from parent.


Benefits : Code Reusablility

Avoid code duplicacy

Reduce Complexity

Runtime Polymorphism

Method overriding

Data Hiding


When you use private keyword in Parent class and you wanna access in child class then you will get compile time Error.



Whenever we create any class in java then its imporible to create without inheritance because implicitly we are inheriting Object Class

Implicitly . means every class extending Object class and this is Super class for every class.



class Parent{

int x=10;

static int y=20;

void show()

{

System.out.println("Show Parent");

}

static void display()

{

System.out.println("Show Parent");

}


Then question is non static data how can we access inside any static method. Now you can access similar in child class as well.


class Child extends Parent

{

public static void main(String args[])

{

Child c =new Child ();

System.out.println("Show Child x:"+new Child ().x);

System.out.println("Show Child x:"+c.x);

}

}



}

class Child extends Parent

{

private static int y=4;

static int z=5;

Child()

{

System.out.println("Show Child x:"+x);

show();

display();

hiChild();

}

void hiChild()

{

System.out.println("Show Child x:"+x);

show();

display();

}

static void helloChild(){

System.out.println("Hello Child");

}

public static void main(String args[])

{

Child c =new Child ();

Parent p =new Child(); //A good Programming

// Its a bad Programming Parent p =new Parent();

System.out.println("Show Child x:"+new Child ().x);

System.out.println("Show Child x:"+c.x);

//show();but not in static method

//display();but not in static method

//hiChild();but not in static method

System.out.println("Show Child x:"+new Child ().x);

//c.show();

//c.display();

//c.hiChild();

//System.out.println("Show Child x:"+p.y);

//p.hiChild();

//p.helloChild();

}

}

When in above program Parent want to acces it child data or method then we will get an error.

//System.out.println("Show Child x:"+p.y);

//p.hiChild();

//p.helloChild();



Child()

{

display();

}

void hiChild()

{

System.out.println("Show Child x:"+x);

display();

}

public static void main(String args[])

{

Child c =new Child ();

Parent p =new Child(); //A good Programming

c.display();

display();

new Child().display();

Parent.display();

p.display();

Child.display();

}

}




#javaassignmenthelp #java #java

Contact Us

Tel: (+91) 0120  4118730  

Time :   10 : 00  AM -  08 : 00 PM IST 

Registered address: G-69, Sector 63, 

 Noida - 201301, India

We Provide Services Across The different countries

USA    Australia   Canada   UK    UAE    Singapore   New Zealand    Malasia   India   Ireland   Germany

CodersArts is a Product by Sofstack Technology Solutions Pvt. Ltd.

  • CodersArts | Linkedin
  • Instagram