Java classes and Object basics-abstract classes and interfaces

Source: Internet
Author: User

In the Java class system, there are two more special abstract bodies-abstract classes and interfaces. Abstract bodies cannot have specific objects (and interfaces are not even classes), but they are two very important concepts in the Java design world, and many good design patterns are based on these two concepts, although we may use less in application development, but in the development of real large project architectures, They are used to determine the overall framework of the whole project and are essential. Here are some basic syntax knowledge of abstract classes and interfaces for memo.

First, what is abstract class? What is an interface?

First of all, the above also said that abstract classes and interfaces are not directly instantiated, it is often only in the declaration of reference when the implementation of polymorphism.

1, abstract class.

Abstract class, the specific declaration syntax is as follows:

This is an abstract class that is declared by abstract class abstractclass{//abstract classes can have non-abstract methods public void Method1 () {//This is a non-abstract method, In subclasses (when subclasses are non-abstract classes) you can directly use SYSTEM.OUT.PRINTLN ("I am in an abstract class, but I am a non-abstract method");} The following is an abstract method, which has no method entity, only declares public abstract void method2 ();//If the subclass is not an abstract class, you must implement the method//private abstract void meethd3 (); Methods in an abstract class cannot be modified by private}

  

It is obvious that abstract classes can have non-abstract methods inside them (even if all methods are non-abstract and grammatical rules), and if subclasses are not abstract classes, they must implement all abstract methods and cannot have an abstract method declaration within them. In addition, the method of abstract class can not be declared as private; in fact, this is also very good understanding: Abstract method should be able to access the quilt class, if set to private, then the subclass can not access, then it has no meaning.

2, interface.

interface, which is not a Java class, it simply declares the abstract method required for that interface (interface). Specific syntax knowledge can be seen in the following code description:

This is an interface that is declared by the Interface keyword interface interfacetest{//This is an interface method, it is similar to abstract methods of abstraction class, there is no concrete implementation, only the declaration part of the method (public can omit) public void Method1 ();//The following notation is also possible, however, the method of the interface defaults to public and abstract and must be public and abstractabstract void Method2 ();//The following method declares a problem/ /private void Method3 ();//Note method adornments must be public or default}

Obviously: The syntax of an interface has the following requirements: The default modifier is public and abstract, and must be decorated only by these two modifiers (of course, this refers to permission adornments and whether they are abstract), which is also well understood: interfaces would have to be implemented, and interfaces would have been intended for polymorphic operation, Only if the external code can access the method in the interface can realize polymorphism, the interface is meaningful.

Ii. specific uses of abstract classes and interfaces

1, polymorphic and abstract class, interface. First of all, abstract classes and interfaces are designed to achieve good code decoupling in the organization of the concept, their ultimate goal is to ensure the scalability of the program, so that the program block in different external environment (incoming object parameters), the same method shows a variety of different behavior characteristics. See the following code specifically:

Polymorphic Description code for abstract classes and interfaces//This is an animal abstract class, there is no real animal this entity, but it is like the cat, dog and other animals abstract class Animal{public abstract eat ();p ublic abstract void Sleep ();} This is the interface of the flying behavior, because not all animals in animals will fly, so consider to separate it into an interface, the need for the implementation of the interface of the animal can be interface flyaction{void fly ();} This is a dog class, it inherits the animal class dog extends animal{public void eat () {System.out.println ("I am the Eat method of dog, I like to eat while pulling");} public void Sleep () {System.out.println ("I am the Sleep method of the dog, I like to go to sleep while drooling");}} This is a bird, it inherits the Animal class, but also implements the Flyaction interface class Bird extends Animal implements Flyaction{public void Fly () { System.out.println ("I am a bird, I can fly, and fly very fast"); public void Eat () {System.out.println ("I am a bird, I eat very little");} public void Sleep () {System.out.println ("I am a bird, I sleep in a cool posture");}} This is a plane, it can fly, but it is not an animal class airplane implements Flyaction{public void Fly () {System.out.println ("I am a plane, I fly high and Fast");}} The following is the test class public class test{flyaction somethingcanfly = null; Animal Animal = null;public void Setsomethingcanfly (Flyaction somethingcanfly) {this.somethingcanfly = Somethingcanfly;} public void Setanimal (Animal Animal) {this.animal = Animal;}//This is a specific application class, through the operation of the interface can manipulate the method of the corresponding object, the implementation of the polymorphic public void Fly () {somethingcanfly.fly ();} This is a specific application class, through the operation of the abstract parent class operation method, can easily implement the polymorphic public void Eat () {animal.eat ();} public static void Main (string[] args) {abstractinterface test = new Abstractinterface (); Test.setanimal (new Bird ());// Here if the transfer comes in is the aircraft object, then the non-way change is the way the plane flies test.fly ();//Here is the bird fly, specifically how to fly is the last word passed in the parameters control, interface to achieve polymorphic test.setanimal (new Dog ()); If this is the object of the bird, then the way to eat is the way the birds eat, the abstract class to achieve polymorphic test.eat ();//This is animal to eat, the specific food is passed in by the object parameters, the abstract class implemented polymorphic}}

Observing the code, in fact, abstract classes and interfaces embody the idea that by manipulating abstract references, you can show different methods of behavior in the concrete execution process, which is the concrete manifestation of polymorphism.

More polymorphism in the knowledge, after the design pattern of the finishing will mention, here will probably be so much.

2, interface or abstract class (inherit or Implement Interface)?

The above code estimates that you can see that the interface and abstract classes are all possible to achieve polymorphism, their role is largely through the abstract reference operation of the actual different objects, many times in the explicit, perhaps two ways can achieve the purpose, then how to choose?

In fact, it is easy to think that Java does not support multiple inheritance, but you can use the interface to achieve similar multi-inheritance effects. Therefore, abstract class, it can be reused code, but can not inherit more, and the interface is often assumed to be similar to the multi-inheritance mission, but it is obvious that all of its methods are abstract, code does not achieve the effect of reuse. So, the abstract class or interface, or to return to the schema design: If the subclass and the corresponding abstract class has a dependency (dog is an animal), then inherit it, because inheritance requires that all methods of the parent class are useful to the subclass, but if it seems like the above example: airplanes and birds will fly, But they are two fundamentally different things, but they all have a common feature: fly This behavior, so it is reasonable to use the interface, because, many times the interface acts like an additional function or behavior concept.

Well, more specific to interface-oriented and abstract programming in the design mode of the concrete summary of it!

Java classes and Object basics-abstract classes and interfaces

Related Article

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.