Source code - Guide to inheritance in Java

This commit is contained in:
MalaguptaBaeldung 2018-02-25 08:06:50 -08:00 committed by GitHub
parent a6d3ddd38f
commit dffe5cb81d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 149 additions and 0 deletions

View File

@ -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
}
}

View File

@ -0,0 +1,12 @@
package com.baeldung.inheritance;
public class BMW extends Car {
public BMW() {
super(5, "BMW");
}
@Override
public String toString() {
return model;
}
}

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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");
}
}

View File

@ -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");
//}
}

View File

@ -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!");
}
}

View File

@ -0,0 +1,6 @@
package com.baeldung.inheritance;
public interface SpaceTraveller extends Floatable, Flyable {
int duration = 10;
void remoteControl();
}