Thinking in java--abstract classes and interfaces

Source: Internet
Author: User

Remember the last time Ali two side asked the abstract class and interface problems, when the answer is not comprehensive. This time again learned, do a more detailed summary.
A Definition of abstract classes and interfaces
Abstract classes and interfaces are abstractions at a higher level in Java.
Abstract class: We define abstract classes and abstract methods with the abstract keyword, which refers to methods that only the definition does not implement, and specifies that if a class contains one or more abstract methods, it must be declared as an abstract class. Abstract classes can also contain ordinary methods, or even no abstract methods are common methods are also possible (this situation may occur when we do not want this class to create objects). An important feature of abstract classes is that they cannot be used to create objects and can only be inherited. For subclasses of an abstract class, if it is a normal class, then it is important to implement all the abstract methods in the abstract class, and if it is still an abstract class, it can not be implemented.
Interface: We define the interface with the interface keyword. Interfaces can be viewed as an extremely abstract class, because Java specifies that all methods in an interface can only have declarations and cannot have implementations. If you want to inherit an interface, we use the Implements keyword, and if a class inherits an interface, the class must implement all the methods in the interface, and of course the interfaces can inherit from each other. All of these methods must be public permissions for the interface, and you can explicitly declare this, but the default permissions are public.

An example: we defined a cycle base class, and bicycle,tricycle,unicycle three subclasses in the previous learning polymorphism. There is a common method in these classes wheels (), which returns the number of its wheels, but in fact, because the base class is not a specific cycle, his wheels () should not be defined (I have set his return value to 0), and we should not allow the cycle class to create an object (because of its ambiguous object meaning). So let's rewrite the code in the form of interfaces and abstract classes, respectively.

 PackageLKL;/// define a base class as an abstract class Public Abstract  class Cycle {    the Wheels () method in///cycle only declares that it is not implemented        Public Abstract int Wheels();} Public  class unicycle extends Cycle{        Public int Wheels(){return 1; }} Public  class BiCycle extends Cycle{      Public int Wheels(){return 2; }} Public  class tricycleextendsCycle{              Public int Wheels(){return 3; }} Public  class ridetest {          Public int Wheels(Cycle Cy) {returnCy.wheels (); } Public Static void Main(string[] args) {//cycle cy = new Cycle (); An error occurred attempting to create an abstract class objectRidetest RT =NewRidetest ();/// depending on the different sub-classes that are passed in, the behavior is called polymorphicSystem.out.println ("Unicylce ' s Wheels:"+rt.wheels (NewUnicycle ())); System.out.println ("Bicycle ' s Wheels:"+rt.wheels (NewBiCycle ())); System.out.println ("Tircycle ' s Wheels:"+rt.wheels (NewTricycle ())); }}
 PackageLKL;/// define cycle as an interface Public  interface Cycle {    the methods in///interface are always public permissions, even if you do not explicitly declare       intWheels ();//private int f (); Error attempting to declare a private permission method} Public  class unicycle implements Cycle{        Public int Wheels(){return 1; }} Public  class BiCycle implements Cycle{      Public int Wheels(){return 2; }} Public  class tricycleimplementsCycle{              Public int Wheels(){return 3; }} PackageLKL; Public  class ridetest {          Public int Wheels(Cycle Cy) {returnCy.wheels (); } Public Static void Main(string[] args) {Ridetest RT =NewRidetest ();/// depending on the different sub-classes that are passed in, the behavior is called polymorphicSystem.out.println ("Unicylce ' s Wheels:"+rt.wheels (NewUnicycle ())); System.out.println ("Bicycle ' s Wheels:"+rt.wheels (NewBiCycle ())); System.out.println ("Tircycle ' s Wheels:"+rt.wheels (NewTricycle ())); }}

As can be seen from the code above, the overall structure is similar, whether it is a common base class or an abstract base class or interface. There is also a very important phenomenon: the two structures are manifested in polymorphism, which means that whether it is an upward transformation into an interface or an abstract class, there is no effect on polymorphism.

Two. The difference between an abstract class and an interface.
Although interfaces and abstract classes look very similar, there are a number of different points between them.
(1). Abstract classes are still classes, so declare them with the class keyword and then use the extends keyword to inherit. The interface is declared with interface, implements inherits.
(2). The method permission in the interface must be public, the variable is a static final type constant, and the method permissions in the abstract class can be set by themselves, as are variables.
(3). All methods in an interface must be abstract, and abstract classes can be abstract or normal.
(4). The last and most important difference is that interfaces support multiple inheritance and abstract classes are not possible! One of the benefits of this is that we can transform a class up into multiple base classes.

Thinking in java--abstract classes and interfaces

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.