Java self-learning-interface and inheritance super

Java's super keyword

Step 1: Prepare a parent class that explicitly provides a parametric constructor

Prepare the parent class that explicitly provides a parametric-free constructor
When instantiating a Hero object, its constructor prints
"Hero's Construction Method“

package charactor;
 
import property.Item;
 
public class Hero {
        
    String name; //Full name
        
    float hp; //Blood volume
        
    float armor; //Armor
        
    int moveSpeed; //Moving speed
     
    public void useItem(Item i){
        System.out.println("hero use item");
        i.effect();
    }
     
    public Hero(){
        System.out.println("Hero Construction method ");
    }
     
    public static void main(String[] args) {
        new Hero();
    }
      
}

Step 2: Instantiate subclasses, and the constructor of the parent class must be invoked

Instantiate an ADHero(), whose constructor is called
The constructor of its parent class will also be called
And it's the parent constructor that calls first
Subclass constructors call the parent class's parameterized constructors by default

package charactor;
  
public class ADHero extends Hero implements AD{
  
    @Override
    public void physicAttack() {
        System.out.println("Physical attack");
    }
     
    public ADHero(){
         
        System.out.println("AD Hero Construction method");
    }
     
    public static void main(String[] args) {
 
        new ADHero();
         
    }
  
}

Step 3: The parent class explicitly provides two constructions

They are the parametric method and the parametric method respectively.

package charactor;
 
import property.Item;
 
public class Hero {
        
    String name; //Full name
        
    float hp; //Blood volume
        
    float armor; //Armor
        
    int moveSpeed; //Moving speed
     
    public void useItem(Item i){
        System.out.println("hero use item");
        i.effect();
    }   
     
    public Hero(){
        System.out.println("Hero Nonparametric construction method ");
    }
     
    public Hero(String name){
        System.out.println("Hero A construction method with one parameter ");
        this.name = name;
    }
     
    public static void main(String[] args) {
        new Hero();
    }
      
}

Step 4: Subclass explicitly calls parent class parametric construction method

Using keyword super to explicitly call the construction method of parent class with parameters

package charactor;
  
public class ADHero extends Hero implements AD{
  
    @Override
    public void physicAttack() {
        System.out.println("Physical attack");
    }
     
    public ADHero(String name){
        super(name);
        System.out.println("AD Hero Construction method");
    }
     
    public static void main(String[] args) {
        new ADHero("De levy");
    }
  
}

Step 5: Call parent class properties

Calling the moveSpeed property of the parent class through super
ADHero also provides the attribute moveSpeed

public int getMoveSpeed(){
   return this.moveSpeed;
}
 
public int getMoveSpeed2(){
   return super.moveSpeed;
}
package charactor;
  
public class ADHero extends Hero implements AD{
 
    int moveSpeed=400; //Moving speed
 
    @Override
    public void physicAttack() {
        System.out.println("Physical attack");
    }
     
    public int getMoveSpeed(){
        return this.moveSpeed;
    }
     
    public int getMoveSpeed2(){
        return super.moveSpeed;
    }
     
    public static void main(String[] args) {
        ADHero h= new ADHero();
         
        System.out.println(h.getMoveSpeed());
        System.out.println(h.getMoveSpeed2());
         
    }
  
}

Step 6: Call the parent class method

ADHero overrides the useItem method and calls the useItem method of the parent class through super in useItem

package charactor;
 
import property.Item;
import property.LifePotion;
 
public class ADHero extends Hero implements AD {
 
    int moveSpeed = 400; // Moving speed
 
    @Override
    public void physicAttack() {
        System.out.println("Physical attack");
    }
 
    public int getMoveSpeed() {
        return this.moveSpeed;
    }
 
    public int getMoveSpeed2() {
        return super.moveSpeed;
    }
 
    // Rewrite useItem and call the userItem method of the parent class
    public void useItem(Item i) {
        System.out.println("adhero use item");
        super.useItem(i);
    }
 
    public static void main(String[] args) {
        ADHero h = new ADHero();
 
        LifePotion lp = new LifePotion();
 
    }
 
}

Practice: super

The parent Hero provides a constructive approach with parameters:

public Hero(String name){
  this.name = name;
}

However, no parametric construction method is provided.
How should subclasses be handled?

package charactor;
   
public class Hero {
    public String name;
    protected float hp;
   
    public Hero(String name){
        this.name = name;
    }
     
//    Deliberately not providing a parametric method of construction
//    public Hero(){
//     
//    }
     
    public static void main(String[] args) {
     
    }
       
}

Keywords: Java Attribute

Added by vmarellano on Thu, 03 Oct 2019 09:33:54 +0300