All about JAVA inheritance, polymorphism, abstract classes, interfaces, interface-oriented programming __ios

Source: Internet
Author: User

Inheritance implements the reuse of code, the abstract class takes into account the purpose of code reuse and the realization of polymorphism, interface is a 100% abstract class, its existence is purely for polymorphism.

Polymorphic, the sample code is as follows:

Public abstract class Animal {int size; public abstract void makenoise (); public abstract void Sleep ();} public class Ca T extends Animal {public void makenoise () {System.out.println ("miao! Miao! ");}} public class Dog extends Animal {public void makenoise () {System.out.println ("wang! Wang! ");}}

These are 3 classes, Dog and Cat inherit from Animal, and a test class for these three classes is written below.

public class Testmain {public static void main (string[] args) {Testmain testmain = new Testmain (); Dog Dog = new Dog (); Cat cat = new Cat (); Testmain.runanimalmakenoise (dog); Dog reference variable is dog type, dog inherits from Animal Testmain.runanimalmakenoise (cat); Cat reference variable is cat type, cat inherits from Animal} public void Runanimalmakenoise (Animal Animal)//This method takes a reference variable of type Animal as an argument, but if you use polymorphism, We can pass in the subtype reference variable of any animal class as the parameter {animal.makenoise ();/////No matter what type of argument is actually passed in, as long as it inherits from Animal, it has makenoise this method. } }

After the run output is:

wang! wang!

Miao! Miao!

As you can see from the top test class, the Runanimalmakenoise method works even if you add some other inherited Animal analogies such as Duck. Polymorphic for an interface, if you define a reference variable of an interface type, the reference variable can be given to any object that implements this interface without having to control where the object itself actually inherits from.

Interface Programming: The interface is not only refers to the Java keyword interface, interface-oriented programming should be understood as a hyper-class programming, interface-oriented programming is the idea of the use of polymorphic features, you can write the program has a strong scalability and maintainability. Interface programming generally refers to the use of polymorphic programming in such places as instance variables of classes, parameters of class methods, return value types of class methods, and so on. Use the strategy patterns in design patterns for examples (refer to the head of the pattern):

A code fragment with no policy mode applied:

Public abstract class Duck {public abstract void Fly ();}

After the policy mode is applied, the code above becomes the following form.

         public abstract class Duck {Flybehavior flybehavior;// Flybehavior is an interface with only one fly method, where polymorphism is used so that flybehavior can be assigned//any class that implements the Fly method in the Flybehavior interface. This way we can change the flying behavior of the Ducks instead of//change the original code, just give the different classes that implement the Flybehavior interface to Flybehavior. public void Performfly () { ()}///Here the Performfly method is equivalent to the Fly method on the top without the policy mode code applied, but here we have delegated the fly//act to the Flybehavio R to execute. public void Setflybehavior (Flybehavior fb) {flybehavior = FB;}///The advantage of this method is that you can dynamically change the behavior of ducks in the process of operation, only to be different to achieve the//Flybehavi The class of the Fly method in the or interface is assigned to the Flybehavior reference variable. The public interface Flybehavior//This interface defines the behavior of the Ducks flying, followed by different classes to implement this interface, and/or implement different fly methods to assign to the Flybehavior reference variable in the Duck type object. {public void Fly ();} The public class Slowfly implements Flybehavior//implements the Flybehavior interface, defining a slow-flying behavior {public void Fly ()//Implementing an abstract fly method in the interface. {System.out.println ("I fly Slowly");} The public class Fastfly implements Flybehavior//Implementation Flybehavior interface, defines a fly fast behavior//Slowfly and Fastfly object can be assigned to a duck class or subclass// Flybehavior reference variables to change the behavior of ducks flying if ducks fly//The behavior needs to be changed simply by creating a class to implement a different fly method. {public void Fly ()//Implementing an abstract fly method in the interface. {System.out.println ("I fly Fast");} }   

Subclasses of the Duck class:

public class Specialduck extends Duck {public Specialduck () {this.flybehavior = new slowfly ();}}

Test class:

public class Testmain {public static void main (string[] args) {Specialduck sd = new Specialduck ();//default constructor in Specialduck The slow behavior of the fly is assigned to Flybehavior Sd.performfly (); Output "I fly slowly" fastfly ff = new Fastfly (); Create a fast-flying behavior instance sd.setflybehavior (FF); The fast-flying behavior instance is dynamically assigned to the Specialduck flybehavior instance variable when the program is running. Sd.performfly (); Output "I Fly Fast"}

Run after output:

I'm flying slow.
I'm flying fast.


         policy pattern defines a set of different algorithms for the same behavior, encapsulated separately, so that the algorithm can be changed independently of the class that uses the algorithm. Polymorphic use Soso.

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: 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.