Home  >  Article  >  Java  >  Java basics - abstract classes and interfaces

Java basics - abstract classes and interfaces

巴扎黑
巴扎黑Original
2017-06-26 11:31:071331browse

The reason why abstract classes and interfaces are put together for note-taking is because they are difficult to distinguish and independent of each other. After learning the three major characteristics of Java programming (encapsulation, inheritance, and polymorphism), my biggest gain is that I slowly understood the advantages of object-oriented programming such as the Java language, which can both maintain its own independence and Constantly evolving and developing. If encapsulation and inheritance are the basis for realizing Java polymorphism, then I personally think that abstract classes and interfaces provide very good support for polymorphism.

1. Abstract class

Formally speaking, a class modified with abstract is an abstract class. Abstract classes are incomplete and can only be used as base classes and cannot be instantiated (cannot be new).

In layman's terms, an abstract class does not do anything by itself. It requires others to implement its specifications. It defines a set of abstract methods, and the specific expressions of this set of abstract methods can be implemented by derived classes. The implementation of abstract classes must use inheritance, otherwise it will be meaningless if you create it.

You must pay attention to the following points when using abstract classes:

1. An abstract class is just a reference and cannot be instantiated. The work of instantiation must be left to its subclasses;

2. Abstract methods must be overridden by subclasses;

3. In a class, as long as there is a method defined as an abstract method (modified by abstract), then this class must It is defined as an abstract class (the class name must also be modified by abstract);

4. The abstract class may or may not contain specific methods;

1 public abstract class ChouX {2     public abstract void method1();3 4     public void method2() {5 6     }7 }

5 .The abstract method in the subclass cannot have the same name as the abstract method in the parent class;

6.Abstract cannot be used to modify the same method in parallel with private, static, fanal or native.

Code example:

//抽象类public abstract class Animal {public abstract void shout();
}
1 //猫子类2 public class Cat extends Animal {3     public void shout() {4         System.out.println("喵~~");5     }6 }
//Dog子类public class Dog extends Animal {public void shout() {
        System.out.println("汪汪汪。。。");
    }
}

//测试类public class Test {public static void main(String[] args) {
        Animal a1 = new Dog();
        Animal a2 = new Cat();
        a1.shout();
        a2.shout();
    }
}

Output :

2. Interface

For object-oriented programming, abstraction is a major feature of it. In Java, OOP abstraction can be reflected in two forms: abstract classes and interfaces.

Interface (interface), in software engineering, generally refers to methods or functions that are called by others.

Format:

[public] interface InterfaceName{

}
<span style="color: #0000ff;"><span style="color: #99cc00;">//例子</span><br>interface</span><span style="color: #000000;"> Door{</span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">abstract</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> open();</span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">abstract</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> close();
}</span>

To make a class follow a specific set of interfaces, you need to use the implements keyword.

Format:

class ClassName implements Interface1, Interface2, Interface3....{} (allows a class to follow multiple specific interfaces)

3. The difference between abstract classes and interfaces

1. Differences at the syntax level

First of all, abstract classes can provide details of member method implementation, while only public abstract methods can exist in interfaces;

Secondly, member variables in abstract classes can be of various types, while members in interfaces can only be of public static final type;

Furthermore, interfaces cannot contain static code blocks and static methods, while abstract classes can;

Finally, a class can only inherit one abstract class, but can inherit multiple interfaces.

2. The difference at the design level

First of all, an abstract class is an abstraction of a thing, that is, an abstraction of a class, while an interface is an abstraction of behavior.

Furthermore, the abstract class serves as the parent class of many subclasses, and it is a template design. The interface is a behavioral specification.

Finally, in an abstract class, if you want to change a method, you can implement it directly in the abstract class, and the subclass does not need to make changes. If this happens to an interface, all classes linked to this interface need to be changed.

Summary: Having said so much, the principle is actually quite simple once you understand it. For abstract classes, for example, you define an abstract class to be the parent class. It's possible that you didn't fully describe a class, or that it was upgraded a few years later. There are always reservations. At this time, you don't have to go to war to overthrow the entire parent class. You only need to fill in the specific changes in the subclass. It’s up to you to decide what you want to change or not, and when to change it. Wouldn't this increase flexibility and reusability? The difference between abstract classes and interfaces is just like fish and boats can swim in the water, so they are not the same class. Don't even think about putting fish and boats into the same class, but they both have this "swimming" action, so we set this "swimming" action into an interface, which can be used by fish and boats. , you can use it in the future when you want to describe anything that can "swim".

There is a very promising case on the Internet:

You can think about it carefully to understand the difference between abstract classes and interfaces.

1 //接口2 interface Alram{3   void alarm();4 }
1 //抽象类2 abstract class Door{3   void open();4   void close();5 }
1 //报警门最后的设置2 class AlarmDoor extends Door implements Alarm{3   void open(){4   }5   void close(){6   }7   void alarm(){8   }9 }

The above is the detailed content of Java basics - abstract classes and interfaces. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn