抽象和接口(abstract vs interface)

Difference between Abstract Class and Interface in Java
https://www.geeksforgeeks.org/difference-between-abstract-class-and-interface-in-java/

  1. Like C++, an abstract class can contain constructors in Java. And a constructor of abstract class is called when an instance of a inherited class is created. For example, the following is a valid Java program.
abstract class Shape {
    int color;

    Shape() {
      // ("Shape Constructor Called");
        Log.e("--", "Shape:Shape Constructor Called " );
    }

    abstract void draw();
}

output:

Shape Constructor Called 
Rectangle: constructor--called
 draw: ---rectangle
  1. In Java, we can have an abstract class without any abstract method. This allows us to create classes that cannot be instantiated, but can only be inherited.
    java中抽象类可以有非抽象的方法,这时子类只可以集成。

  2. Abstract classes can also have final methods (methods that cannot be overridden). For example, the following program compiles and runs fine.
    抽象类中还可以有final 类的方法,不可以被override.

abstract class Shape {
    int color;

    Shape() {
      // ("Shape Constructor Called");
        Log.e("--", "Shape:Shape Constructor Called " );
    }
    void shapeUnAbstrac(){
        Log.e("--", "shapeUnAbstrac: --" );
    }

  final  void shapeFinalMethod(){
      Log.e("--", "shapeFinalMethod: ");
  }

    abstract void draw();
}

output:

11-15 11:15:32.555 357-357/? E/--: shapeUnAbstrac: --
11-15 11:15:32.555 357-357/? E/--: shapeFinalMethod: 

原文还有几个小彩蛋,哈哈,几个在上学时候很让人烦的“课后练习”或是“思考题”,但是,离开学校后在自学的路上越来越发现,学校里老师强制的:
预习-听课-练习-复习 真的是学习的好方法!!!
废话不说了,下面直接搬运来几个问题,真的,需要思考下,脑袋才不会锈掉。
Exercise:

  1. Is it possible to create abstract and final class in Java?
  2. Is it possible to have an abstract method in a final class?
  3. Is it possible to inherit from multiple abstract classes in Java?

问题1:不可以!!!
这个一看就知道完全不可以啊,因为 abstract 就是一个抽象类,它设计的初衷就是为了给子类继承的,然后实现具体的abstract方法。而final 类,顾名思义,就是最终的,是不可以被修改,更别说继承了,完全矛盾的2种逻辑。
问题2:不可以!!!
在java中,abstract()方法必须在abstract类中,一旦你在一个类中定义了抽象方法,那么这个类就自动是 abstract 类,但是题目说它是final类,那么问题又回到了1,即:一个类可以是abstract and final class. 所以,答案就显而易见了。

3.不可以!!!
直接编译错误,会飘红的说!!!
这是个和interface区别的经典问题之一。
java官方文档解释说“to void the issues of multiple inheritance of state,which is the ability to inherit fields from multiple classes.”(避免状态的多重继承问题,即从多个类继承fields的能力)
举例哈:
假设您能够定义一个扩展多个类的新类。通过实例化该类来创建对象时,该对象将继承所有类的超类中的字段。如果来自不同超类的方法或构造函数实例化相同的字段会怎样?哪个方法或构造函数优先?

interface IAnimals
{
    public string eat();
}

abstract class Animals implements IAnimals
{
    public abstract void run();
    public string eat(){ return "Animals eating"; } 
}

abstract class Animals1 implements IAnimals
{
    public abstract void run1();
    public string eat(){ return "Animals1 eating"; } 
}

class Dog extends Animals,Animals1
{
  public void run() {System.out.println("Run method");}
  public void run1() {System.out.println("Run1 method");}
}

interface
Why do we use interface ?

It is used to achieve total abstraction.
Since java does not support multiple inheritance in case of class, but by using interface it can achieve multiple inheritance .
It is also used to achieve loose coupling.
Interfaces are used to implement abstraction. So the question arises why use interfaces when we have abstract classes?
The reason is, abstract classes may contain non-final variables, whereas variables in interface are final, public and static.

为什么用interface?
重要一点:多继承。

抽象类和接口区别:

interface方式的实现中,Demo只能够有静态的不能被修改的数据成员(也就是必须是static final的,不过在interface中一般不定义数据成员),所有的成员方法都是abstract的。从某种意义上说,interface是一种特殊形式的abstract class。

一般性理解:
概念不同:抽象类是对根源的抽象,而接口是对动作的抽象。
抽象类是 is-a,接口是like-a ;抽象类表示:这个对象是什么?接口:这个对象能做什么?比如:汽车和火车(设为2个类)他们的抽象类是车,因为他们都是车;像汽车可以跑,火车也可以跑,那么你可以把车的性能定义为interface,里面有drive()、stop()等的共同行为。

A real world example:
Let’s consider the example of vehicles like bicycle, car, bike………,they have common functionalities. So we make an interface and put all these common functionalities. And lets Bicylce, Bike, car ….etc implement all these functionalities in their own class in their own way.

import java.io.*; 

interface Vehicle { 
    
    // all are the abstract methods. 
    void changeGear(int a); 
    void speedUp(int a); 
    void applyBrakes(int a); 
} 

class Bicycle implements Vehicle{ 
    
    int speed; 
    int gear; 
    
    // to change gear 
    @Override
    public void changeGear(int newGear){ 
        
        gear = newGear; 
    } 
    
    // to increase speed 
    @Override
    public void speedUp(int increment){ 
        
        speed = speed + increment; 
    } 
    
    // to decrease speed 
    @Override
    public void applyBrakes(int decrement){ 
        
        speed = speed - decrement; 
    } 
    
    public void printStates() { 
        System.out.println("speed: " + speed 
            + " gear: " + gear); 
    } 
} 

class Bike implements Vehicle { 
    
    int speed; 
    int gear; 
    
    // to change gear 
    @Override
    public void changeGear(int newGear){ 
        
        gear = newGear; 
    } 
    
    // to increase speed 
    @Override
    public void speedUp(int increment){ 
        
        speed = speed + increment; 
    } 
    
    // to decrease speed 
    @Override
    public void applyBrakes(int decrement){ 
        
        speed = speed - decrement; 
    } 
    
    public void printStates() { 
        System.out.println("speed: " + speed 
            + " gear: " + gear); 
    } 
    
} 
class GFG { 
    
    public static void main (String[] args) { 
    
        // creating an inatance of Bicycle 
        // doing some operations 
        Bicycle bicycle = new Bicycle(); 
        bicycle.changeGear(2); 
        bicycle.speedUp(3); 
        bicycle.applyBrakes(1); 
        
        System.out.println("Bicycle present state :"); 
        bicycle.printStates(); 
        
        // creating instance of bike. 
        Bike bike = new Bike(); 
        bike.changeGear(1); 
        bike.speedUp(4); 
        bike.applyBrakes(3); 
        
        System.out.println("Bike present state :"); 
        bike.printStates(); 
    } 
} 

你可能感兴趣的:(抽象和接口(abstract vs interface))