Java's abstract modifier && Interface interface Usage && differences between abstract classes and interface

Source: Internet
Author: User
Tags define abstract modifiers

The abstract modifier can modify classes and methods.

(1) The abstract modifier class, which makes this class an abstract class, will not generate an object instance, but can be used as the type declared by the object variable (see later instance), which is the compile-time type. Abstract classes are equivalent to a class of semi-finished products, which require subclasses to inherit and overwrite the abstract methods.

(2) Abstract modification method will make this method an abstract method, that is, only the declaration is not implemented, the subclass must inherit the implementation.

(3) Place of attention:

A: A class with an abstract method must be an abstract class. But abstract classes are not necessarily abstract methods, but they can all be concrete methods. The abstract modifier must be placed before the class name when decorating the class. The abstract modification method is a method that requires its subclasses (implementations) to invoke a subclass to overwrite (implement) the subclasses in a polymorphic way, unless the subclass itself is an abstract class.

B: The parent class is an abstract class, in which there is an abstract method, then the subclass inherits the parent class and implements (overwrites) all the abstract methods in the parent class, and the subclass has the ability to create an instance of the object, or the subclass must be an abstract class. The simple example below has an abstract class

Abstract classe{ Public Abstract voidShow (); }    classFextendse{ Public voidShow () {System.out.print ("Test all FFFF \ n"); }  }    classGextendse{ Public voidShow () {System.out.print ("Test all GGGG \ n"); }  }   Public classMain { Public Static voidMain (string[] args)throwsinterruptedexception {E P=NewF ();          P.show (); E Q=NewG ();      Q.show (); }  }  

There will be polymorphic phenomena. The result of the execution is the console output

Test all FFFF
Test all GGGG

=============================================================================================================== ==============

Extends is inherited from the parent class, as long as that class is not declared final. Multiple inheritance is not supported in Java, but it can be implemented with interfaces, which will use the implements. Inheritance can inherit only one class, but implements can implement multiple interfaces, separated by commas, such as class A extends B implements C,d,e.
Differences with extends: extends is inherited from a class, inherited can use the parent class method can also override the parent class method, implements is to implement multiple interfaces, the method of the interface must be overridden to use. Be aware of the following points:

A, constants and abstract methods are generally defined in an interface. Abstract classes can contain abstract methods or non-abstract methods, but classes with abstract methods must be abstract classes. Abstract methods cannot have method bodies.

B, Interface (interface), methods can only define abstract methods and default is public, constants are public static final decorated (with or without these modifiers, methods and constants have this property by default).

C, a class can implement multiple unrelated interfaces (this is different from inheritance).

D, the interface can inherit other interfaces and add new attributes and abstract methods.

E, you must add the public modifier to the method that implements the interface in the class.

Example:

Interfacew1{//Defining Interfaces     Public Final Static inti = 3; voidstart (); voidrun (); voidStop (); }    InterfaceW2extendsw1{//interfaces can inherit and add new property methods     Public Final Static intj = 4;//Modifiers for constants    voidOpenmonth (); voidUpanddown (); voidgoin (); }    classTtImplementsw2{ Public voidStart () {//Implementation of the interface will inherit the variables of the interface, the method of implementing the interface plus publicSYSTEM.OUT.PRINTLN ("----start ()----"); }       Public voidrun () {System.out.println ("----run ()----"); }       Public voidStop () {System.out.println ("----Stop ()----"); }       Public voidOpenmonth () {System.out.println ("----openmonth ()----"); }       Public voidUpanddown () {System.out.println ("----upanddown ()----"); }       Public voidgoin () {System.out.println ("----goin ()----"); }  }     Public classMain { Public Static voidMain (string[] args) {//TODO auto-generated Method StubW1 TT =NewTT ();//implementing an object that points to the parent class of an interface referenceSystem.out.println (TT.I);//class name. Static VariablesSystem.out.println (TT.I);//instance. Static VariablesSystem.out.println (W1.I);//interface name. Static variableTt.start (); W2 ee=NewTT ();               System.out.println (TT.J); //class name. Static VariablesSystem.out.println (EE.J);//instance. Static VariablesSystem.out.println (W2.J);//interface name. Static variableEe.start (); }  }  

Execution Result:

3
3
3
----Start ()----
4
4
4
----Start ()----

Here are some points to note: (1) static variables (equivalent to constants) can be used directly with the class name. Static variable name, interface is also a class, so the interface name. Static variable names are available, and (2) interface references. Static variables are the embodiment of polymorphism.

=============================================================================================================== ===========

There is a need to talk about the differences between abstract classes and interface, and abstract class and interface are two mechanisms that support the definition of abstract classes. It is the existence of these two mechanisms that gives Java a powerful object-oriented capability, and the difference is as follows:

(1) Same point
A, both are abstract classes and cannot be instantiated.
B,interface implementations and ABSTRCT class subclasses must implement an abstract method that has already been declared.

(2) different points
A,interface realization, to use implements, and abstract class implementation, to use extends.
B, a class may implement multiple interface, but a class can inherit only one abstract class.
C,interface emphasizes the implementation of a particular feature, while the abstract class emphasizes its affiliation.
D, although the subclasses of the interface implementation class and the ABSTRCT class must implement the corresponding abstract methods, the implementations are in different forms. Each method in the interface is an abstract method, which is only declared (declaration, no method body) and must be implemented. The subclass of abstract class can be implemented selectively.
This choice of abstract class has two meanings: first, not all methods in Abastract class are abstract, only those whose crown has abstract are abstracted, and subclasses must be implemented. For those that do not have an abstract method, the method body must be defined in the ABSTRCT class. The second is that when the subclass of abstract class inherits it, it can either inherit directly from the non-abstract method or overwrite it, but the abstract method can be implemented by choice, or it can be implemented by declaring its method abstract, without implementing it and leaving it to its subclasses, but this class must also be declared as an abstract class. Abstract classes, and of course, cannot be instantiated.

E,interface are completely abstract, can only declare methods, and can only declare pulic methods, cannot declare private and protected methods, cannot define method bodies, and cannot declare instance variables.

Java's abstract modifier && Interface interface Usage && differences between abstract classes and interface

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.