generated from nus-cs2030/base-template
-
Notifications
You must be signed in to change notification settings - Fork 4
Class VS Abstract Class VS Interface
tzelynn edited this page Oct 11, 2021
·
2 revisions
interface ThreeDShape {
int getVolume();
}
// remember to define different classes/interfaces/abstract classes in different .java files
abstract class ColouredRect {
private final String colour;
private final int height;
private final int width;
protected ColouredRect(String colour, int height, int width) {
this.colour = colour;
this.height = height;
this.width = width;
}
int getArea() {
return this.height * this.width;
}
abstract boolean floatable();
@Override
public String toString() {
return String.format("%d by %d %s shape", this.height,
this.width, this.colour);
}
}
class ColouredCuboid extends ColouredRect implements ThreeDShape {
private final String name;
private final int depth;
private static final int DENSITY_OF_WATER = 1;
ColouredCuboid(String colour, int height, int width, int depth, String name) {
super(colour, height, width);
this.name = name;
this.depth = depth;
}
@Override
public int getVolume() {
return this.getArea() * this.depth;
}
@Override
boolean floatable() {
return this.getArea() / this.getVolume() < DENSITY_OF_WATER;
}
@Override
public String toString() {
String output = String.format("%s is a ", this.name);
output += super.toString();
output += String.format(" with depth %s", this.depth);
return output;
}
}
class ColouredSquare extends ColouredRect {
ColouredSquare(String colour, int height, int width) {
super(colour, height, width);
}
@Override
boolean floatable() {
return true;
}
boolean isUnitSquare() {
return this.getArea() == 1;
}
}
- Interface cannot implement any functions within its definition (all functions are abstract) but abstract class can have both implemented and abstract functions, while concrete class cannot have abstract functions (must implement all functions)
- see
getVolume()
VSgetArea()
andfloatable()
above
- see
- In a concrete class that extends an abstract class and implements an interface, functions from interface must be
public
but functions from abstract class do not need to bepublic
- see
getVolume()
VSfloatable()
above
- see
- In a concrete class that extends an abstract class and implements an interface, all abstract methods must be implemented in concrete class
- see
getVolume()
in ColouredCuboid andfloatable()
in ColouredCuboid and ColouredSquare
- see
- Concrete class can use functions from abstract parent class without overriding if they have already been implemented in parent class
- see
getArea()
above
- see
- Concrete class can implement its own functions not from the parent class it extends or interface it implements
- see
isUnitSquare()
above
- see
Peer Learning
Guides
Setting Up Checkstyle
Setting Up Java
Setting Up MacVim
Setting Up Stu
Setting Up Unix For Mac
Setting Up Unix For Windows
Setting Up Vim
Setting up SSH Config
SSH Without Password
Copying Files From PE Nodes
Using tmux
CS2030 Contents
Lecture 1 SummaryLecture 2 Summary
Access Modifiers
Lecture 3 Summary (Polymorphism)
Compile Time Type VS Runtime Type
Abstraction, Encapsulation, Inheritance, and Polymorphism
SOLID Principles
Class VS Abstract Class VS Interface
Comparable VS Comparator
Generic Types T
HashMap
Raw Types with Generic
Lambda expression
PECS (Producer Extends Consumer Super)
Optional
Streams
Parallel Streams
Monad
Functors and Monads with Category Theory