Abstract class and abstract method and interface differences, abstract class interface differences

Source: Internet
Author: User

Abstract class and abstract method and interface differences, abstract class interface differences
I. abstract classes

Abstract classes and specific classes are relative, not absolute. Abstract is a conceptual term, specifically a visible and touchable realistic object. The smaller the concept, the greater the abstraction degree, the larger the extension, and vice versa. Simply put, "man" is more abstract than "man", while "creature" is more abstract than "man", and "material" is more abstract than "creature.
Abstract concepts are generated by specific concepts based on their "commonalities". When many personalities of specific concepts are excluded and their commonalities are described in a centralized manner, an abstract concept is created. Abstract thinking is a kind of ability generated when human thinking reaches the advanced stage. For example, when a child's thinking is not mature, they can only grasp specific concepts, they often encounter difficulties when learning the pronoun "you, me, and him", because the pronoun has a high abstraction.
In short, abstract concepts have a large extension, a small connotation, a small extension of specific concepts, and a large connotation.


Ii. Abstract METHODS
An abstract method is a special method: it has only declarations, but not specific implementations. The Declaration format of the abstract method is:
abstract void fun();

Abstract methods must be modified with abstract keywords. If a class contains an abstract method, this class is called an abstract class. The abstract class must be modified with the abstract keyword before the class. Because abstract classes contain no specific implementation methods, you cannot use abstract classes to create objects.

Note the following: in JAVA programming ideology, the abstract class is defined as a class that contains abstract methods. However, if a class does not contain abstract methods, abstract classes are used for modification. That is to say, abstract classes do not necessarily contain abstract methods. I personally think this is a tough question, because if an abstract class does not contain any abstract methods, why should it be designed as an abstract class? So remember this concept for the moment. You don't have to go into the details.

[public] abstract class ClassName {    abstract void fun();}

It can be seen from this that the abstract class exists for inheritance. If you define an abstract class but do not inherit it, it is equivalent to creating this abstract class in vain, because you cannot use it to do anything. For a parent class, if a method of the class has no meaning in the parent class, it must be implemented according to the actual requirements of the subclass, then you can declare this method as the abstract method. In this case, this class becomes the abstract class.

Classes that contain abstract methods are called abstract classes, but they do not mean that abstract classes can only have abstract methods. They can also have member variables and common member methods like normal classes. Note: There are three main differences between abstract classes and common classes:

1) The abstract method must be public or protected (because if it is private, it cannot be inherited by the quilt class, And the subclass cannot implement this method). By default, the abstract method is public.

2) abstract classes cannot be used to create objects;

3) if a class inherits from an abstract class, the subclass must implement the abstract method of the parent class. If the subclass does not implement the abstract method of the parent class, the subclass must also be defined as the abstract class.

In other aspects, the abstract class is no different from the ordinary class.
 

Iii. Interfaces

An interface is called an interface. in software engineering, an interface refers to a method or function that can be called by others. From here, we can understand the original intention of the Java language designer, which is an abstraction of behavior. In Java, the form of an interface is as follows:

[public] interface InterfaceName { }

The interface can contain variables and methods. Note that the variables in the interface are implicitly specified as the public static final variable (and can only be the public static final variable). If you use private modification, a compilation error is returned ), the method is implicitly specified as a public abstract method and can only be a public abstract method (a compilation error is reported when other keywords such as private, protected, static, and final are used ), in addition, all methods in the interface cannot be implemented. That is to say, methods in the interface must be abstract methods. The difference between an interface and an abstract class can be seen from this. An interface is an extremely abstract type, which is more "abstract" than an abstract class and generally does not define variables in the interface.

The implements keyword is required for a class to follow a specific set of interfaces. The specific format is as follows:

class ClassName implements Interface1,Interface2,[....]{}

It can be seen that a class can follow multiple specific interfaces. If a non-abstract class follows an interface, all the methods in the interface must be implemented. Abstract classes that follow an interface do not implement abstract methods in the interface.


Iv. Differences between abstract classes and interfaces

Java provides and supports creating abstract classes and interfaces. Their implementations share the following things:

  • All methods in the interface are implicitly abstract. Abstract classes can contain both abstract and non-abstract methods.

  • Class can implement many interfaces, but can inherit only one abstract class

  • Class to implement an interface, it must implement all methods declared by the interface. However, classes do not implement all methods declared in abstract classes. In this case, classes must be declared abstract.

  • Abstract classes can implement interfaces without interface methods.

  • The variables declared in the Java interface are final by default. Abstract classes can contain non-final variables.

  • The member functions in the Java interface are public by default. Abstract class member functions can be private, protected, or public.

  • Interfaces are absolutely abstract and cannot be instantiated. Abstract classes cannot be instantiated, but can be called if they contain the main method.



Thank you for reading the Shanghai Java TRAINING Article. For more information and support, please clickJava training in ShanghaiThank you for your attention!


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.