Skip to content

Commit dffe5cb

Browse files
Source code - Guide to inheritance in Java
1 parent a6d3ddd commit dffe5cb

8 files changed

Lines changed: 149 additions & 0 deletions

File tree

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.baeldung.inheritance;
2+
3+
public class ArmoredCar extends Car implements Floatable, Flyable{
4+
private int bulletProofWindows;
5+
private String model;
6+
7+
public void remoteStartCar() {
8+
// this vehicle can be started by using a remote control
9+
}
10+
11+
public String registerModel() {
12+
return model;
13+
}
14+
15+
public String getAValue() {
16+
return super.model; // returns value of model defined in base class Car
17+
// return this.model; // will return value of model defined in ArmoredCar
18+
// return model; // will return value of model defined in ArmoredCar
19+
}
20+
21+
public static String msg() {
22+
// return super.msg(); // this won't compile.
23+
return "ArmoredCar";
24+
}
25+
26+
@Override
27+
public void floatOnWater() {
28+
System.out.println("I can float!");
29+
}
30+
31+
@Override
32+
public void fly() {
33+
System.out.println("I can fly!");
34+
}
35+
36+
public void aMethod() {
37+
// System.out.println(duration); // Won't compile
38+
System.out.println(Floatable.duration); // outputs 10
39+
System.out.println(Flyable.duration); // outputs 20
40+
}
41+
42+
43+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.baeldung.inheritance;
2+
3+
public class BMW extends Car {
4+
public BMW() {
5+
super(5, "BMW");
6+
}
7+
8+
@Override
9+
public String toString() {
10+
return model;
11+
}
12+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.baeldung.inheritance;
2+
3+
public class Car {
4+
private final int DEFAULT_WHEEL_COUNT = 5;
5+
private final String DEFAULT_MODEL = "Basic";
6+
7+
protected int wheels;
8+
protected String model;
9+
10+
public Car() {
11+
this.wheels = DEFAULT_WHEEL_COUNT;
12+
this.model = DEFAULT_MODEL;
13+
}
14+
15+
public Car(int wheels, String model) {
16+
this.wheels = wheels;
17+
this.model = model;
18+
}
19+
20+
public void start() {
21+
// Check essential parts
22+
// If okay, start.
23+
}
24+
public static int count = 10;
25+
public static String msg() {
26+
return "Car";
27+
}
28+
29+
public String toString() {
30+
return model;
31+
}
32+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.baeldung.inheritance;
2+
3+
public class Employee {
4+
private String name;
5+
private Car car;
6+
7+
public Employee(String name, Car car) {
8+
this.name = name;
9+
this.car = car;
10+
}
11+
12+
public Car getCar() {
13+
return car;
14+
}
15+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.baeldung.inheritance;
2+
3+
public interface Floatable {
4+
int duration = 10;
5+
void floatOnWater();
6+
7+
default void repair() {
8+
System.out.println("Repairing Floatable object");
9+
}
10+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.baeldung.inheritance;
2+
3+
public interface Flyable {
4+
int duration = 10;
5+
void fly();
6+
7+
/*
8+
* Commented
9+
*/
10+
//default void repair() {
11+
// System.out.println("Repairing Flyable object");
12+
//}
13+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.baeldung.inheritance;
2+
3+
public class SpaceCar extends Car implements SpaceTraveller {
4+
@Override
5+
public void floatOnWater() {
6+
System.out.println("SpaceCar floating!");
7+
}
8+
9+
@Override
10+
public void fly() {
11+
System.out.println("SpaceCar flying!");
12+
}
13+
14+
@Override
15+
public void remoteControl() {
16+
System.out.println("SpaceCar being controlled remotely!");
17+
}
18+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.baeldung.inheritance;
2+
3+
public interface SpaceTraveller extends Floatable, Flyable {
4+
int duration = 10;
5+
void remoteControl();
6+
}

0 commit comments

Comments
 (0)