directory

concept

Abstract classes and ordinary classes

Abstract classes and interfaces

Third, summary

concept

Abstract classes and concrete classes are relative concepts. “Abstract” is a concept in the logic of being thought, while “concrete” is a tangible object. In simple terms, “man” is more abstract than “man”, “animal” is more abstract than “man”, and “creature” is more abstract than “animal”.

Abstract concept is produced by concrete concept in accordance with its “general”, the specific concept of the personality of the expulsion, concentrated description of its general, will produce an abstract concept. Abstract thinking is a kind of ability produced by the advanced stage of human thinking. For example, when children are immature in thinking, they can only grasp specific concepts, and they often encounter difficulties in learning pronouns “you, I, he”, because pronouns are highly abstract.

In short, the denotation of abstract concept is large, but the connotation is small; The denotation of the specific concept is small, but the connotation is large.

Abstract classes and ordinary classes

(1) Ordinary classes can be instantiated; An abstract class cannot be instantiated because it exists in a concept and is not concrete.

(2) Both ordinary and abstract classes can be inherited, but if the abstract class is inherited, the subclass must override the inherited methods, unless the class is also abstract.

(3) Example demonstration:

/** * Public class Pet {public void play(){public void play(){ Method is empty in the}} -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - public class extends Pet Cat {/ / this is a subclass, Public void play(){system.out.println ("1, cat tree "); }} -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- the public class Dog extends Pet {/ / this is a subclass, Public void play(){system.out.println ("2, dog bite bone "); }} -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- the public class Test {public static void main (String [] args) {/ / this is a Test class, Pet p1=new Cat(); Pet p2=new Dog(); Pet p2=new Dog(); p1.play(); p2.play(); }}Copy the code

Output result:

1. Cats climb trees

The dog gnaws a bone

Q:

Public void play(); public void play(); If the subclass doesn’t change, it overrides the parent method.

Is an abstract method complete without a method body?

That I ordinary method has method body, I empty do not write content not to get, do not follow abstract method a kind? Don’t

An abstract class can’t be instantiated yet, and I don’t need to new it! What’s the point of having an abstract class when a normal class can do it?

As I said before, I can leave ordinary class methods blank to achieve the same effect as an abstract class method without a method body. Why create an abstract class when both methods can achieve the same output? Is it more comfortable than ordinary class?

With the great?

Or more convenient?

Or do you have to enforce the implementation of abstract methods in order to force others to use them?

A:

In order to enforce that you can’t instantiate, and enforce that subclasses have to implement methods it’s not a matter of whether you forget, you say you don’t go to new it’s ok, that’s true. So you have another question, why do you have access control? Why should there be a distinction between private and public? I can all public, shouldn’t access, I don’t access the line ah? On the contrary, the private members have to write a bunch of set and get functions, which is a lot of trouble. When I write my own small programs, I will also be lazy to write all public functions. However, when the project is large and the code is much larger, this kind of strict structure is very important. Not to mention there will be many people cooperate together to write a program, even if still write you a person, also won’t have to forget, then the compiler is not an error, the vast yards offshore which find errors to object oriented at the end of the day is convenient for you to think, easy extension, easy maintenance and management, extortionary say there’s no need to, don’t need all object-oriented, C what do ah, The operation efficiency is also high.

Abstract classes and interfaces

1, concept,

Interfaces are abstractions; abstract classes are abstractions.

An abstract class is a representation of what this object is. Interfaces are all about what this object can do. For example, for men and women, the abstract class is human, while for cats and dogs, the abstract class is pet. Humans can eat, pets can also eat, but the two should not be confused, because there are essential differences. The “eating” is an action, you can define “eating” as an interface, and then let the two classes implement its methods.

Therefore, in high-level languages, a class can only inherit one class or abstract class, just as a person can not be animal and plant at the same time, but can implement multiple interfaces, such as eating interface, breathing interface, etc.

2. Usage

A. Abstract classes and interfaces are used to abstract concrete objects, but interfaces have the highest level of abstraction;

B. Abstract classes can have concrete methods and properties, and interfaces can only have abstract methods and final constants;

C. Abstract classes are mainly used to abstract categories and interfaces are mainly used to abstract functions;

D. There are no implementations in the abstract class, which must be overridden by derived classes. All methods in the interface must be unimplemented;

E. When an abstract class implements an interface, the methods of the interface may or may not be implemented in the abstract class, whereas a common implementation interface must implement all interface methods.

3. Direction of use

  • When you focus on the essence of a thing, use abstract classes;
  • When you focus on an operation, use interfaces.

Third, summary

Abstract classes are much more powerful than interfaces, but they are expensive to define. Because high-level languages (and by practical design) can only inherit one class per class. In this class, you must inherit or write all the commonalities of all its subclasses. Although the interface is much less functional, it is only a description of an action. And you can implement multiple interfaces simultaneously in a single class. The difficulty will be reduced at the design stage.

The last

Original is not easy, burning hair output content, if there is a lost harvest, a praise to encourage it! There is also a little surprise, to help you sort out some technical e-books, follow the public account “AIO Life” reply “1024” ~

PS: There are Easter eggs in the official account