First, Introduction.
Interfaces and internal classes provide us with a more structured approach to separating interfaces from implementations.
There is not enough information to describe the class we can call it abstract class.
Second, the problem introduced.
We all know that the door has the function of opening and closing, as well as the function of locking. It is up to the designer to decide how to open and close the door. At this point we can set an abstract class.
1 Abstract class door{2 Abstract void open (); 3 Abstract void close (); 4 }
What if we add the alarm function to the door at this time? Of course we can not directly add the function of alarm in the original door class, because not all the doors have the function of alarm, so we can't create an abstract class like this. Currently we are comprised of two solutions.
The first type:
1 Abstract classdoor{2 Abstract voidopen ();3 Abstract voidclose ();4 }5 Abstract classAlarmextendsdoor{6 Abstract voidalarm ();7}
Inherit the original functionality and add your own unique functionality by defining an abstract class. Although this method can realize the three functions of the door, it increases the complexity of the code, is not easy to read, and it does not conform to the reasonable requirements of program design and object-oriented.
The second type:
1 Abstract class door{2 Abstract void open (); 3 Abstract void close (); 4 }5interface Alarm {6 void Alarm (); 7 }
By defining an interface to describe the function of the alarm, we can inherit the function of door through a class, and the function of alarm can be realized through the interface. Just in line with the concept of program design.
Third, the difference between abstract class and interface.
The basics of learning Java know that both abstract classes and interfaces must implement functionality through other classes that do not contain abstract methods. So what's the difference between them?
Features of abstract classes:
1. Abstract classes cannot be instantiated. It is possible to instantiate by inheriting subclasses.
2. Whenever a class that contains an abstract method must be defined as an abstract class.
3. Abstract classes can contain specific methods, and of course, they may not contain any abstract methods.
4. If you inherit from an abstract class and want to create an object of that new class, you must provide a method definition for all the abstract methods in the base class.
Features of the interface:
1.Interface Party All access rights are automatically declared as public.
2. The implementation method does not exist in the interface.
3. A non-abstract class that implements an interface must implement all the methods of that interface.
Thanking in Java--interface this keyword produces a completely abstract class that does not provide any concrete implementations at all. It allows the creator to determine the method name, the argument list, and the return type, but there is no method body. The interface provides only the form, without providing any specific implementation. An interface means: "All classes that implement that particular interface look like this." Interface is just the way it looks, but now I want to state how it works. "The interface is used to establish the protocol between classes. Interface is an extremely abstract class. To create an interface, you need to replace the class keyword with the interface keyword.
Difference:
1. member variables in an abstract class can be of various types, whereas member variables in an interface are only public static final types.
2. Interfaces cannot contain static code blocks and static methods, whereas abstract classes can have static code blocks and static methods.
3. A class can inherit only one abstract class, while a class may implement multiple interfaces.
4. Abstract classes are abstractions of classes (abstractions of things), and interfaces are abstractions of behavior (abstractions of methods).
Iv. Summary
1. A class can only have one parent class, but there may be multiple interfaces.
2. You can have your own member variables and non-abstract class methods in an abstract class, but there can only be static immutable member data in the interface, and all of its methods are abstract.
3. Abstract classes and interfaces reflect the design concept is different, the abstract class represents the "is-a" relationship, and the interface represents the "like-a" relationship.
Java essay: Talking about abstract classes and interfaces