Merge pull request #3725 from MalaguptaBaeldung/master
Code - Guide to Inheritance in Java
This commit is contained in:
commit
487864a2f2
|
@ -0,0 +1,43 @@
|
|||
package com.baeldung.inheritance;
|
||||
|
||||
public class ArmoredCar extends Car implements Floatable, Flyable{
|
||||
private int bulletProofWindows;
|
||||
private String model;
|
||||
|
||||
public void remoteStartCar() {
|
||||
// this vehicle can be started by using a remote control
|
||||
}
|
||||
|
||||
public String registerModel() {
|
||||
return model;
|
||||
}
|
||||
|
||||
public String getAValue() {
|
||||
return super.model; // returns value of model defined in base class Car
|
||||
// return this.model; // will return value of model defined in ArmoredCar
|
||||
// return model; // will return value of model defined in ArmoredCar
|
||||
}
|
||||
|
||||
public static String msg() {
|
||||
// return super.msg(); // this won't compile.
|
||||
return "ArmoredCar";
|
||||
}
|
||||
|
||||
@Override
|
||||
public void floatOnWater() {
|
||||
System.out.println("I can float!");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fly() {
|
||||
System.out.println("I can fly!");
|
||||
}
|
||||
|
||||
public void aMethod() {
|
||||
// System.out.println(duration); // Won't compile
|
||||
System.out.println(Floatable.duration); // outputs 10
|
||||
System.out.println(Flyable.duration); // outputs 20
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
package com.baeldung.inheritance;
|
||||
|
||||
public class BMW extends Car {
|
||||
public BMW() {
|
||||
super(5, "BMW");
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return model;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,32 @@
|
|||
package com.baeldung.inheritance;
|
||||
|
||||
public class Car {
|
||||
private final int DEFAULT_WHEEL_COUNT = 5;
|
||||
private final String DEFAULT_MODEL = "Basic";
|
||||
|
||||
protected int wheels;
|
||||
protected String model;
|
||||
|
||||
public Car() {
|
||||
this.wheels = DEFAULT_WHEEL_COUNT;
|
||||
this.model = DEFAULT_MODEL;
|
||||
}
|
||||
|
||||
public Car(int wheels, String model) {
|
||||
this.wheels = wheels;
|
||||
this.model = model;
|
||||
}
|
||||
|
||||
public void start() {
|
||||
// Check essential parts
|
||||
// If okay, start.
|
||||
}
|
||||
public static int count = 10;
|
||||
public static String msg() {
|
||||
return "Car";
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return model;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,15 @@
|
|||
package com.baeldung.inheritance;
|
||||
|
||||
public class Employee {
|
||||
private String name;
|
||||
private Car car;
|
||||
|
||||
public Employee(String name, Car car) {
|
||||
this.name = name;
|
||||
this.car = car;
|
||||
}
|
||||
|
||||
public Car getCar() {
|
||||
return car;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,10 @@
|
|||
package com.baeldung.inheritance;
|
||||
|
||||
public interface Floatable {
|
||||
int duration = 10;
|
||||
void floatOnWater();
|
||||
|
||||
default void repair() {
|
||||
System.out.println("Repairing Floatable object");
|
||||
}
|
||||
}
|
|
@ -0,0 +1,13 @@
|
|||
package com.baeldung.inheritance;
|
||||
|
||||
public interface Flyable {
|
||||
int duration = 10;
|
||||
void fly();
|
||||
|
||||
/*
|
||||
* Commented
|
||||
*/
|
||||
//default void repair() {
|
||||
// System.out.println("Repairing Flyable object");
|
||||
//}
|
||||
}
|
|
@ -0,0 +1,18 @@
|
|||
package com.baeldung.inheritance;
|
||||
|
||||
public class SpaceCar extends Car implements SpaceTraveller {
|
||||
@Override
|
||||
public void floatOnWater() {
|
||||
System.out.println("SpaceCar floating!");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fly() {
|
||||
System.out.println("SpaceCar flying!");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void remoteControl() {
|
||||
System.out.println("SpaceCar being controlled remotely!");
|
||||
}
|
||||
}
|
|
@ -0,0 +1,6 @@
|
|||
package com.baeldung.inheritance;
|
||||
|
||||
public interface SpaceTraveller extends Floatable, Flyable {
|
||||
int duration = 10;
|
||||
void remoteControl();
|
||||
}
|
|
@ -0,0 +1,46 @@
|
|||
package com.baeldung.inheritance;
|
||||
|
||||
import com.baeldung.inheritance.*;
|
||||
|
||||
import junit.framework.Test;
|
||||
import junit.framework.TestCase;
|
||||
import junit.framework.TestSuite;
|
||||
|
||||
public class AppTest extends TestCase {
|
||||
|
||||
public AppTest(String testName) {
|
||||
super( testName );
|
||||
}
|
||||
|
||||
public static Test suite() {
|
||||
return new TestSuite(AppTest.class);
|
||||
}
|
||||
|
||||
@SuppressWarnings("static-access")
|
||||
public void testStaticMethodUsingBaseClassVariable() {
|
||||
Car first = new ArmoredCar();
|
||||
assertEquals("Car", first.msg());
|
||||
}
|
||||
|
||||
@SuppressWarnings("static-access")
|
||||
public void testStaticMethodUsingDerivedClassVariable() {
|
||||
ArmoredCar second = new ArmoredCar();
|
||||
assertEquals("ArmoredCar", second.msg());
|
||||
}
|
||||
|
||||
public void testAssignArmoredCarToCar() {
|
||||
Employee e1 = new Employee("Shreya", new ArmoredCar());
|
||||
assertNotNull(e1.getCar());
|
||||
}
|
||||
|
||||
public void testAssignSpaceCarToCar() {
|
||||
Employee e2 = new Employee("Paul", new SpaceCar());
|
||||
assertNotNull(e2.getCar());
|
||||
}
|
||||
|
||||
public void testBMWToCar() {
|
||||
Employee e3 = new Employee("Pavni", new BMW());
|
||||
assertNotNull(e3.getCar());
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue