Before Inheritance-

Types of relationship in classes-
1- Is-a relationship-(INHERITANCE)
i.e- class bike, class pulser
Relationship- pulser is a bike

2- Has-a relationship-(ASSOCIATION)
i.e- class engine, class pulser
Relationship- pulser has a engine

INHERITANCE

Inheritance is a concept where a class inherits data members and methods from another class.

1- Types of inheritance
(i) Single level inheritance
inh1

(ii) Multi level inheritance
inh2
(iii)Multiple inheritance
inh3
(iv) Hierarchical inheritance
inh4
(v) Hybrid inheritance – Combination of any two inheritance

ASSOCIATION

Association is a concept where a class has reference of another class.

ass

2- Types of association
(i) Aggregation

agrgsn
(ii) Composition
compositn

code samples are here

 

1-single level inheritance example- Program to extend class ParentC on class 
ExampleSingleLevel

package inheritance11;

class ParentC{
String name="abc";
void show(){
System.out.println("hi");
}
}

public class ExampleSingleLevel extends ParentC {

public static void main(String[] args) {
ExampleSingleLevel e1=new ExampleSingleLevel();
System.out.println(e1.name);
e1.show();

}
}
 
 2- Multi level inheritance example- 


package inheritance11;

class P1{
int a=1;
void shw(){
System.out.println("abc");
}
}

class P2 extends P1{
String n="god";
void dsplay(){
System.out.println("abc");
}
}

public class ExampleMultiLevel extends P2 {

public static void main(String[] args) {
ExampleMultiLevel e1=new ExampleMultiLevel();
System.out.println(e1.n);
System.out.println(e1.a);
e1.shw();
e1.dsplay();
}

}

Association-
1- Aggregationa-
i-Class Engine-

package association12;

public class Engine {
private int engineCapacity;
private int engineSerialNumber;

public Engine(int engineCapacity, int engineSerialNumber) {
this.engineCapacity = engineCapacity;
this.engineSerialNumber = engineSerialNumber;
}

public int getEngineCapacity() {
return engineCapacity;
}

public int getEngineSerialNumber() {
return engineSerialNumber;
}
}
b- Class AggregateCar

package association12;

public class AggregateCar {
private String make;
private int year;
private Engine engine;

public AggregateCar(String make, int year, Engine engine) {
this.make = make;
this.year = year;
this.engine = engine;
}

public String getMake() {
return make;
}

public int getYear() {
return year;
}

public Engine getEngine() {
return engine;
}
}

 
c- Class Mainclass

package association12;

public class MainClass {

public static void main(String[] args) {
Engine engine=new Engine(10, 420);

AggregateCar agCar=new AggregateCar("Audi", 2016, engine);

}
}

 

2- Composition

a- Class Engine-

package association12;

public class Engine {
private int engineCapacity;
private int engineSerialNumber;

public Engine(int engineCapacity, int engineSerialNumber) {
this.engineCapacity = engineCapacity;
this.engineSerialNumber = engineSerialNumber;
}

public int getEngineCapacity() {
return engineCapacity;
}

public int getEngineSerialNumber() {
return engineSerialNumber;
}
}

b- Class CompositeCar-
package association12;
public class CompositeCar {
private String make;
private int year;
private Engine engine;

public CompositeCar(String make, int year, int engineCapacity, int engineSerialNumber) {
this.make=make;
this.year=year;
engine = new Engine(engineCapacity,engineSerialNumber);
}

public String getMake() {
return make;
}

public int getYear() {
return year;
}

public int getEngineSerialNumber() {
return engine.getEngineSerialNumber();
}

public int getEngineCapacity() {
return engine.getEngineCapacity();
}
}

c- Class MainClass
package association12;

public class MainClass {

public static void main(String[] args) {
Engine engine=new Engine(10, 420);

AggregateCar agCar=new AggregateCar("Audi", 2016, engine);

CompositeCar comCar=new CompositeCar("Ford", 2014, 20, 786);

}
}
Advertisements