SDE实践1

interface vs. abstract class

A class must be declared abstract when it has one or more abstract methods. A method is declared abstract when it has a method heading, but no body - which means that an abstract method has no implementation code inside like normal methods do.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
abstract class Figure {
public abstract float getArea();
public void print() {
System.out.println("This is a Figure");
}
}

class Circle extends Figure {
private float radius;
public float getArea() {
return (3.14f * (radius * radius));
}
}

class Rectangle extends Figure {
private float length, width;
public float getArea() {
return length * width;
}
}

An interface differs from an abstract class has only abstract methods.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
interface CanBark {
public void bark();
}

class Husky implements CanBark {
public void bark() {
System.out.println("wuuuuu!");
}
}

class Wolf implements CanBark {
@Override
public void bark() {
System.out.println("ooooooouu!");
}
}

class Cat implements CanBark {
@Override
public void bark() {
System.out.println("meow");
}
}

public class Main {
public static void main(String[] args) {

List<CanBark> myTeam = new LinkedList<>();
Husky myHusky = new Husky();
Wolf myWolf = new Wolf();
Cat myCat = new Cat();
myTeam.add(myHusky);
myTeam.add(myWolf);
myTeam.add(myCat);
for (CanBark player : myTeam) {
player.bark();
}
}
}

/*
wuuuuu!
ooooooouu!
meow
*/

Difference:

  1. Java does not allow multiple inheritance. In Java, a class can only derive from one class, whether it's abstract or not. However, a class can implement multiple interfaces.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    public class Main {
    public static void main(String[] args) {

    A a = new A();
    a.printOut(); // "hey, it's class A"
    B b = new B();
    b.printOut(); // “hey, it's class B”
    C c = new C();
    c.printOut(); // ???
    }
    }

    class A {
    public void printOut() {
    System.out.println("hey, it's class A");
    }
    }

    class B {
    public void printOut() {
    System.out.println("hey, it's class B");
    }
    }

    // if we allow multiple inheritance
    class C extends A, B {
    }

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    public class Main {
    public static void main(String[] args) {

    C c = new C();
    c.printOut();
    }
    }

    interface A {
    public void printOut();
    }

    interface B {
    public void printOut();
    }

    class C implements A, B {
    @Override
    public void printOut() {
    System.out.println("hey, it's class C");
    }
    }

  2. An abstract class may provide some methods with definitions - so an abstract class can have non-abstract methods with actual implementation details. An abstract class can also have constructors and instance variables as well. An interface, however, can not provide any method definitions - it can only provide method headings. Any class that implements the interface is responsible for providing the method definition/implementation.

  3. abstract: is a ; interface: has a function

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    public abstract class Door {
    abstract void open();
    abstract void close();
    }

    public interface Alarm {
    void alarm();
    }

    public class AlarmDoor extends Door implements Alarm {
    void open() {...}
    void close() { }
    void alarm() { }
    }

Interview Question: When will you use abstract class vs. interface?

An abstract class is good if you think you will plan on using inheritance since it provides a common base class implementation to derived classes.

An abstract class is also good if you want to be able to declare non-public members. In an interface, all methods must be public. If you think you will need to add methods in the future, then an abstract class is a better choice.

Because if you add new method headings to an interface, then all of the classes that already implement that interface will have to be changed to implement the new methods.

Interfaces are a good choice when you think that the API will not change for a while.

Interfaces are also good when you want to have something similar to multiple inheritance, since you can implement multiple interfaces.

Access Modifier

Definition

  • public - everyone can access

  • private - only myself can access (but only at class level, other objects of the same class can access as well)

  • protected - only my children and same package can access

  • default - only the same package can access

Use the possibly strictest access modifier!

Modifier Class Package Subclass World
public Y Y Y Y
protected Y Y Y N
no modifier Y Y N N
private Y N N N

Exceptions

An Exception indicates conditions that a reasonable application might want to catch.An Error indicates serious problems that a reasonable application should not try to catch: StackOverflowError, Both Error and Exceptions extend from Throwable.

image-20200129153755879
image-20200129153755879
捧个钱场?