Shape.java
public class Shape {
protected double width;
protected double height;
public Shape(double width, double height) {
this.width = width;
this.height = height;
}
//Get the area of the shape (override in the derived class)
public double getArea() {
return 0d;
}
//public abstract double getArea();
}
Triangle.java
public class Triangle extends Shape {
public Triangle(double width, double height) {
super(width, height);
}
//Get the area of a triangle
@Override
public double getArea() {
return this.width * this.height / 2;
}
}
Rectangle.java
public class Rectangle extends Shape {
public Rectangle(double width, double height) {
super(width, height);
}
//Get the area of a rectangle
@Override
public double getArea() {
return this.width * this.height;
}
}
public class PolymorphismBasic {
public static void main(String[] args) {
//Assign Triangle type object (upcast)
Shape tri = new Triangle(10, 50);
//Substitute Rectangle type object (upcast)
Shape rec = new Rectangle(10, 50);
System.out.println(tri.getArea()); //250.0
System.out.println(rec.getArea()); //500.0
}
}
abstract
qualifier to abstract method definition **;
abstract
modifier to the class block for classes containing abstract methods **Shape.java
public abstract class Shape {
protected double width;
protected double height;
public Shape(double width, double height) {
this.width = width;
this.height = height;
}
//You can't have content in the base class!
public abstract double getArea();
}
interface
instructionpublic interface Shape {
double getArea();
}
implements interface name
implements Shape, Hoge, Bar
extends MyParent implements Shape
//Shape interface implementation class
public class Rectangle implements Shape {
private double width;
private double height;
public Rectangle(double width, double height) {
this.width = width;
this.height = height;
}
@Override
public double getArea() {
return this.width * this.height;
}
}
public static final
interface MyApp{
String TITLE = "Order of the Phoenix";
double NUMBER = 5;
}
public interface Loggable {
default void log(String msg) {
System.out.println("Log: " + msg);
}
}
//Do not override the log method
public class LoggableImpl implements Loggable {
}
InterfaceDefault.java
public class InterfaceDefault {
public static void main(String[] args) {
var l = new LoggableImpl();
//The default method of the Loggable class is called
l.log("Harry Potter"); //Log:Harry Potter
}
}
//Do not override the log method
public class LoggableImpl implements Loggable {
@Override
public void log(String msg) {
Loggable.super.log(msg);
System.out.println("LogImpl: " + msg);
}
}
InterfaceDefault.java
public class InterfaceDefault {
public static void main(String[] args) {
var l = new LoggableImpl();
l.log("Harry Potter");
//Log:Harry Potter
//LogImpl:Harry Potter
}
}
public interface IHoge {
void foo();
//CharSequence foo(); //OK
}
public interface IHoge2 {
String foo();
}
public class HogeImpl implements IHoge, IHoge2 {
@Override
public String foo() { } //The return type is IHoge.foo()Not compatible with
}
public interface Hoge {
int DATA = 0;
}
public interface Hoge2 {
String DATA = "This is an apple.";
}
public class HogeImpl implements Hoge, Hoge2 {
public void foo() {
System.out.println(DATA); //error
}
}
super
public interface DHoge {
default void log(String msg) {
System.out.println("DHoge: " + msg);
}
}
public interface DHoge2 {
default void log(String msg) {
System.out.println("DHoge2: " + msg);
}
}
public class HogeImpl implements DHoge, DHoge2 {
@Override
public void log(String msg) {
DHoge.super.log(msg); //Error (duplicate default method log with String is DHoge2,(Inherited from DHoge)
//IHoge.super.log(msg); //Explicit reference is OK
}
}
XWriter
framework in the base classes of BufferWriter
, OutputStreamWriter
, PrintWriter
.Recommended Posts