Development want to learn a foreign language, to master its language features, often can achieve twice the result with half the effort study, since the word come into contact with Java, must have been taught is Java is an object-oriented programming, should remember everything can object, all the features are based on an object to expand and secure, direct drive.

A. The abstract

Everything in life can be abstracted into an object, something with the same characteristics can be abstracted into an object. For example, if you’ve ever played LOL, King of Glory knows that you have to choose a hero to enter the game. Each hero has his own health, blue and skill, and this attribute (health, blue) and trait (casting skill) can be abstracted into an object. All you need to do is list their common characteristics, and then you have the object class. You don’t need to know the details of the object at this point. When we use the hero object, we can implement it at a higher level.

abstract class Hero {
   int redValue;/ / health
   int blueValue;/ / blue
   abstract void releasekills(a);/ / skills
}
Copy the code

It makes a lot of sense to say that this object, given different health, blue energy and abilities, represents different heroes. Remember, anything can be an object. In actual development work, it is important to abstract a particular business so that we can better understand the business and development.

2. Enclosed

As the name implies, implementation details are hidden from the user who uses this object. Again, when we play a game and press QWER to release a skill, we call the release skill method. However, we don’t know how the skill is released or implemented, only the result of the release. So why use encapsulation, the benefits of encapsulation?

  • Security, hiding details from users;
  • Reduce the complexity of the program, only need to call the encapsulated method, do not need to know the specific implementation;
  • For the encapsulation method, we only care about the result, not the process, so that we can debug our program better.
  • Reusability and maintainability

3. Inheritance

When we abstract, we’ve already abstracted out a hero class, and when we need to use a concrete hero, we use inheritance. Subclasses inherit from their parent using the extends keyword. Subclasses inherit from their parent’s methods and properties (PS: We use the above abstract class Hero to write a child of the tank Hero superclass. The subclass is an incremental relationship to the superclass, and we can define other properties and methods based on the superclass, so that we can reuse the superclass code very well.

public class TankHero extends Hero{// Tank hero
	int redValue = 500;/ / health
	int blueValue = 300;/ / blue
	@Override
	public void releasekills(a){
          System.out.println("Regenerate 20 health per second.");
	}
	public void increaseArmor(a){
          System.out.println("Armor value increased by 100"); }}public class ApHero extends Hero{// The wizard hero
	int redValue = 400;/ / health
	int blueValue = 300;/ / blue
	@Override
	public void releasekills(a){
          System.out.println("Spell power increased by 100"); }}Copy the code

Why single inheritance, multiple implementations in Java?

Take, for example, what we did above. Define two classes, ApHero, TankHero, where there is a concrete Hero, Noxas, even if the mage is a tank, if he inherits both of these two classes, and the two parents inherit from the abstract Hero class, The crow method releasekills() inherits from ApHero or TankHero, so there is a problem. Java only inherits from TankHero. When the single inheritance can not meet our needs, we can implement the interface to meet, because the interface can be multiple implementation, implementation of the interface needs to rewrite the method, call or call the implementation class method, so it does not matter.

4. Polymorphism

Polymorphism refers to the parent class reference to the subclass object, the same operation on different subclass object to produce different effects, for example, each hero can release skills, but the shape of each skill is different, which is also a representation of polymorphism.

1. Polymorphic representation

Hero hero = new TankHero();
Hero hero = new ApHero();
Copy the code

There are three necessary conditions for polymorphism to exist: class inheritance or interface implementation, subclass overriding methods of the parent class, and reference of the parent class to objects of the subclass

2. Compile-time and run-time polymorphism

  • Compile-time polymorphism (static binding)

Method overloads are all compile-time polymorphisms. Java can determine at compile time which of the overloaded methods to execute, based on the data type, number, and order of the actual arguments. Method overrides a static binding that is compile-time polymorphic when references to the current object point to the current object.

    public static void main(String[] args){
        ApHero apHero = new ApHero();
        TankHero tankHero = new TankHero();
        // Static binding for method overload
        gainEffect(apHero);
        gainEffect(tankHero);
        System.out.println("-- -- -- -- -- -- -- -- -- -- -");
        // Static binding overridden (overridden Hero releasekills method)
        apHero.releasekills();
        tankHero.releasekills();
    }

    public static void gainEffect(ApHero apHero){
        System.out.println("Mage gets blue BUFF.");
    }
    public static void gainEffect(TankHero tankHero){
        System.out.println("Tanks get red BUFF");
    }

    /** Results: * Mage gets blue BUFF * Tank gets red BUFF * ----------- * Spell power increases by 100 * regeneration of 20 health per second */
Copy the code
  • Runtime polymorphism (dynamic binding)

Method override when a parent class reference points to a subclass object, which is run time polymorphic.

public class test {

public static void main(String[] args){
	ApHero apHero = new Syndra();
	apHero.releasekills();
	System.out.println("Sindra's Blue Quantity:"+apHero.blueValue);
}
	/** Result: * Release E skill weak retreat * Sindra's mana: 300 */
}

public class ApHero extends Hero{// The wizard hero
	int redValue = 400;/ / health
	int blueValue = 300;/ / blue
	@Override
	public void releasekills(a){
	  System.out.println("Spell power increased by 100"); }}public class Syndra extends ApHero{/ / Della
	int redValue = 400;/ / health
	int blueValue = 500;/ / blue
	@Override
	public void releasekills(a){
	  System.out.println("Release E skill weak retreat");
	}
	public void releaseFlash(a){
	  System.out.println("Careless. There's no flash."); }}Copy the code

Release skill is to call the overwritten method to release sindra’s skill, but notice that the output of sindra is 300, which is the parent class of the blue quantity. Why is that? The parent or subclass of the original property does not depend on the type of object we created, but on the type of variable we defined. Also note that superclass references cannot call methods unique to subclass objects. In the example above, the apHero reference cannot call Sindra’s releaseFlash() method, which is subclass specific and cannot be called.

3. Advantages and disadvantages of using polymorphism

  • Advantages:

    Easy to replace, high scalability, high flexibility, reduce type coupling degree

  • Disadvantages:

    Cannot use subclass-specific functionality

Thank you to see here, feel helpful to you, welcome to pay attention to the public number element autumn cloud, I am a little comrade like to study Java source code, hope to progress with you, welcome to come to me to play, secretly tell you to pay attention to the public number element autumn cloud, background reply e-book, receive rich Java e-book information oh!