The difference between abstract class and interface is frequently asked in interviews.
Characteristics of abstract class
1.Both abstract class and abstract method needs to be declared with 「abstract」
2.Using「new」to generate object on abstract class is not allowed.
3.Abstract class needs declaration but not necessary to be implemented.
4.The derived class needs to implement the abstract method in the base class.
Otherwise it's merely another abstract class.
abstract class Shape{
String name;
double length, width, heigh;
double radius;
abstract double area();
abstract double perimeter();
public Shape(double length, double width, String name){
this.length = length;
this.width = width;
this.name = name;
}
public Shape(String name, double width, double height){
this.height = height;
this.width = width;
this.name = name;
}
}
class Rectangular extends Shape{
public Rectangular(double length, double width, String name){
super(length, width, name);
}
double area(){
return length * width;
}
double perimeter(){
return (length + width) * 2;
}
}
class EqualTriangle extends Shape{
public EqualTrangle(String name, double width, double height){
super(name, width, height);
}
double area(){
return (width * height) / 2 ;
}
double perimeter(){
return 3 * width;
}
}
Characteristics of interface
1.No constructor in the interface.
2.All the data members need to be initiated
3.The parameters need to be declared as public and final,or static.
4.Abstract methods need to be public or abstract.
interface Shape {
public double Shape_PI = 3.14;
public double area();
public double perimeter();
}
class Circle implements Shape {
private double radius;
public Circle(double r) {
setRadius(r);
}
public Circle() {
this(10.0);
}
public double getRadius() {
return radius;
}
public void setRadius(double r) {
if (r > 0.0) {
radius = r;
}
else {
radius = 1;
}
}
public double area() {
return radius * radius * Shape_PI;
}
public double perimeter() {
return 2 * radius * Shape_PI;
}
}
class Rectangle implements Shape {
private double length;
private double width;
public Rectangle(double l, double w) {
setLength(l);
setWidth(w);
}
public Rectangle(double a) {
this(a, 10.0);
}
public Rectangle() {
this(10.0, 10.0);
}
public double getLength() {
return length;
}
public double getWidth() {
return width;
}
public void setLength(double l) {
if (l > 0) {
length = l;
}
else {
length = 1.0;
}
}
public void setWidth(double w) {
if (w > 0) {
width = w;
}
else {
width = 1.0;
}
}
public double area() {
return length * width;
}
public double perimeter() {
return 2 * (length + width);
}
}
Note to avoid confusion
1. Class in java can only inherit one base class.
(While in C++, multi-inheritance of class is supported but not recommended)
2. Interface can do multi-inheritance on interfaces.
3. Class can implements multiple interfaces.
When to use abstract class or interface ?
It's been widely discussed for long time about when to user abstract class or interface.As for me, I will intend to use abstract class and inheritance to describe the characteristic of the class.And I will use interface to define functions that can represent the capability or behavior of it. That's my principle,depending on the project,developers can have their own style or principles to use abstract class and interface flexibly.
0 意見:
張貼留言