Step 13:Oops- 1st Concept :INHERITANCE: 8th hour + code

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 Parent C on class ExampleSingleLevel

 package inheritance11;

class ParentC{

String name="paret class variable";

void show(){ System.out.println("Parent class method");

}

}

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{

String a="grandparent variable"; \

void shw(){

System.out.println("grandparent method");

}

}

class P2 extends P1{

String n="parent variable";

void dsplay(){

System.out.println("parent method");

}

}

public class ExampleMultiLevel extends P2 {

public static void main(String[] args) {

ExampleMultiLevel e1=new ExampleMultiLevel();

System.out.println(e1.a);

e1.shw();

System.out.println(e1.n);

e1.dsplay();

}

} 
Association-
1- Aggregation-
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);

}

} 

Run this Program ▶️

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);

}

} 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.