Commit 8b0fa3ca authored by Suresh Kumar's avatar Suresh Kumar

Refactor Inheritance

parent c8b50e84
package aggregation.com; package aggregation.com;
public class Address { public class Address {
int House_no; int hno;
String city; String city;
String country; String country;
Address(int h, String c, String cntry){ Address(int h, String c, String cntry){
this.House_no=h; this.hno=h;
this.city=c; this.city=c;
this.country=cntry; this.country=cntry;
} }
......
...@@ -6,6 +6,6 @@ public static void main(String [] args){ ...@@ -6,6 +6,6 @@ public static void main(String [] args){
Address adOjb = new Address(150, "Umerkot", "Pakistan"); Address adOjb = new Address(150, "Umerkot", "Pakistan");
Employee emp1 = new Employee(1, "Suresh", adOjb); Employee emp1 = new Employee(1, "Suresh", adOjb);
emp1.Display(); emp1.display();
} }
} }
...@@ -3,21 +3,21 @@ package aggregation.com; ...@@ -3,21 +3,21 @@ package aggregation.com;
public class Employee { public class Employee {
int id; int id;
String name; String name;
Address Add; Address add;
Employee(int i, String n, Address a){ Employee(int i, String n, Address a){
this.id = i; this.id = i;
this.name=n; this.name=n;
this.Add=a; this.add=a;
} }
void Display(){ void display(){
System.out.println("Your Id Number is .................. (" + id + ")\n" + System.out.println("Your Id Number is .................. (" + id + ")\n" +
"Your Name is .........................(" + name + ")\n" + "Your Name is .........................(" + name + ")\n" +
"Your House _No Is ...........................(" + Add.House_no + ")\n" + "Your House _No Is ...........................(" + add.hno + ")\n" +
"Your City Name is................................(" + Add.city + ")\n" + "Your City Name is................................(" + add.city + ")\n" +
"your Country Name is..................................(" + Add.country + ") \n " ); "your Country Name is..................................(" + add.country + ") \n " );
System.out.print("------------------------Composition With Aggregation------------------------"); System.out.print("------------------------Composition With Aggregation------------------------");
......
package association.com; package association.com;
public class Addres { public class Addres {
int HouseNo; int hno;
int zip; int zip;
String city; String city;
String country; String country;
Addres(int HouseNo, int zip, String city, String country){ Addres(int hno, int zip, String city, String country){
this.HouseNo=HouseNo; this.hno=hno;
this.zip=zip; this.zip=zip;
this.city=city; this.city=city;
this.country= country; this.country= country;
......
...@@ -10,7 +10,7 @@ public class Person { ...@@ -10,7 +10,7 @@ public class Person {
void display(){ void display(){
System.out.println("Your Name Name is Mr........ (" + name + ")\n" + System.out.println("Your Name Name is Mr........ (" + name + ")\n" +
"Your House No is .........................(" + add.HouseNo + ")\n" + "Your House No is .........................(" + add.hno + ")\n" +
"Your Zip Code is ...........................(" + add.zip + ")\n" + "Your Zip Code is ...........................(" + add.zip + ")\n" +
"Your City Name is...........................(" + add.city + ")\n" + "Your City Name is...........................(" + add.city + ")\n" +
"your Country Name is..................................(" + add.country + ") \n " ); "your Country Name is..................................(" + add.country + ") \n " );
......
package composition.com; package composition.com;
public class Car { public class Car {
private final String name; private final String name;
private final Engine engine; private final Engine engine;
public Car(String name , Engine engine){ public Car(String name , Engine engine){
...@@ -11,9 +10,7 @@ public class Car { ...@@ -11,9 +10,7 @@ public class Car {
public String getName(){ public String getName(){
return name; return name;
} }
public Engine getEngine(){ public Engine getEngine(){
return engine; return engine;
} }
}
}
package composition.com; package composition.com;
public class Engine { public class Engine {
private String type ; private String type ;
private int hp; private int hp;
...@@ -7,18 +6,15 @@ Engine(String type , int hp){ ...@@ -7,18 +6,15 @@ Engine(String type , int hp){
this.type=type; this.type=type;
this.hp=hp; this.hp=hp;
} }
public void setType(String type) { public void setType(String type) {
this.type = type; this.type = type;
} }
public String getType() { public String getType() {
return type; return type;
} }
public void setHp(int hp) { public void setHp(int hp) {
this.hp = hp; this.hp = hp;
} }
public int getHp() { public int getHp() {
return hp; return hp;
} }
......
...@@ -10,20 +10,16 @@ class Parent{ ...@@ -10,20 +10,16 @@ class Parent{
System.out.println("Hi I am Child_1........"); System.out.println("Hi I am Child_1........");
} }
} }
class Child_2 extends Parent{ class Child_2 extends Parent{
void name_2(){ void name_2(){
System.out.println("Hi I am Child_2..........."); System.out.println("Hi I am Child_2...........");
} }
} }
public class Hierarchical { public class Hierarchical {
public static void main(String [] args){ public static void main(String [] args){
Child_2 ch = new Child_2(); Child_2 ch = new Child_2();
ch.eat(); ch.eat();
ch.name_2(); ch.name_2();
System.out.println("Hierarchical Inheritance ................"); System.out.println("Hierarchical Inheritance ................");
} }
} }
...@@ -6,7 +6,6 @@ class GrandFather{ ...@@ -6,7 +6,6 @@ class GrandFather{
class Father extends GrandFather{ class Father extends GrandFather{
void eat(){System.out.println("Eating...");} void eat(){System.out.println("Eating...");}
} }
class Son extends Father{ class Son extends Father{
void speak(){System.out.println("Speaking...");} void speak(){System.out.println("Speaking...");}
} }
...@@ -18,5 +17,4 @@ public class Multilevel{ ...@@ -18,5 +17,4 @@ public class Multilevel{
b.speak(); b.speak();
System.out.println("Hi This is Multi Level Inheritance"); System.out.println("Hi This is Multi Level Inheritance");
} }
} }
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment