[Java] Polymorphism

What is polymorphism?

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 method

How to use

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

Interface definition

public interface Shape {
  double getArea();
}

Defineable member

Interface implementation

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

Interface member

Constant field

interface MyApp{
    String TITLE = "Order of the Phoenix";
    double NUMBER = 5;
}

static field (Java 8 or later)

default method (Java 8 or later)

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

Attention to multiple inheritance

Duplicate abstract method

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
}

Duplicate constant fields

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

Duplicate default method

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

Which to use, interface or abstract class?

Recommended Posts

[Java] Polymorphism
java (polymorphism)
Polymorphism
About Java Polymorphism super ()
java (merits of polymorphism)
Java
Java
Polymorphism
Java learning (0)
Studying Java ―― 3
[Java] array
Polymorphism quiz
Java protected
[Java] Annotation
[Java] Module
Java array
Studying Java ―― 9
Java scratch scratch
Java tips, tips
Java methods
Java method
java (constructor)
Java array
java (override)
java (method)
Java Day 2018
Java string
Java static
Java serialization
java beginner 4
JAVA paid
Studying Java ―― 4
Java (set)
[Java] compareTo
Studying Java -5
java reflexes
java (interface)
Java memorandum
☾ Java / Collection
Java array
[Java] Array
Studying Java # 0
Java review
java framework
Java features
[Java] Inheritance
FastScanner Java
About polymorphism
Java features
java beginner 3
Java memo
java (encapsulation)
Java inheritance
[Java] Overload
Java basics
Decompile Java
[Java] Annotation
java notes
java beginner
Java (add2)
JAVA (Map)