Skip to content

Commit 487864a

Browse files
authored
Merge pull request eugenp#3725 from MalaguptaBaeldung/master
Code - Guide to Inheritance in Java
2 parents ba97c27 + e789eaa commit 487864a

9 files changed

Lines changed: 195 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+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.baeldung.inheritance;
2+
3+
import com.baeldung.inheritance.*;
4+
5+
import junit.framework.Test;
6+
import junit.framework.TestCase;
7+
import junit.framework.TestSuite;
8+
9+
public class AppTest extends TestCase {
10+
11+
public AppTest(String testName) {
12+
super( testName );
13+
}
14+
15+
public static Test suite() {
16+
return new TestSuite(AppTest.class);
17+
}
18+
19+
@SuppressWarnings("static-access")
20+
public void testStaticMethodUsingBaseClassVariable() {
21+
Car first = new ArmoredCar();
22+
assertEquals("Car", first.msg());
23+
}
24+
25+
@SuppressWarnings("static-access")
26+
public void testStaticMethodUsingDerivedClassVariable() {
27+
ArmoredCar second = new ArmoredCar();
28+
assertEquals("ArmoredCar", second.msg());
29+
}
30+
31+
public void testAssignArmoredCarToCar() {
32+
Employee e1 = new Employee("Shreya", new ArmoredCar());
33+
assertNotNull(e1.getCar());
34+
}
35+
36+
public void testAssignSpaceCarToCar() {
37+
Employee e2 = new Employee("Paul", new SpaceCar());
38+
assertNotNull(e2.getCar());
39+
}
40+
41+
public void testBMWToCar() {
42+
Employee e3 = new Employee("Pavni", new BMW());
43+
assertNotNull(e3.getCar());
44+
}
45+
46+
}

0 commit comments

Comments
 (0)