Java abstract class
Declare a method in a class that has no implementation body and is an "empty" method
Such a method is called an abstract method and uses the modifier "abstract"
When a class has an abstract method, it must be declared as an abstract class
Step 1: Abstract Class
Add an abstract method attack to Hero and declare Hero abstract.
APHero,ADHero,ADAPHero are subclasses of Hero and inherit the properties and methods of Hero.
However, their attack methods are different, so after inheriting the Hero class, these subclasses must provide different attack method implementations.
package charactor; public abstract class Hero { String name; float hp; float armor; int moveSpeed; public static void main(String[] args) { } // Abstract method attack // Subclasses of Hero are required to implement the attack method public abstract void attack(); } package charactor; public class ADHero extends Hero implements AD { public void physicAttack() { System.out.println("Physical Attack"); } @Override public void attack() { physicAttack(); } } package charactor; public class APHero extends Hero implements AP { @Override public void magicAttack() { System.out.println("Perform magical attacks"); } @Override public void attack() { magicAttack(); } } package charactor; public class ADAPHero extends Hero implements AD, AP { @Override public void attack() { System.out.println("Now that you can do physical attacks, you can also do magic attacks"); } public void magicAttack() { System.out.println("Perform magical attacks"); } public void physicAttack() { System.out.println("Physical Attack"); } }
Step 2: An abstract class can have no abstract methods
Hero classes can be declared abstract without providing abstract methods
Once a class is declared abstract, it cannot be instantiated directly
package charactor; public abstract class Hero { String name; float hp; float armor; int moveSpeed; public static void main(String[] args) { //Although there is no abstract method, once declared as an abstract class, it cannot be instantiated directly Hero h= new Hero(); } }
Step 3: Differences between abstract classes and interfaces
Difference 1:
Subclasses can inherit only one abstract class, not more than one
Subclasses implement multiple interfaces
Difference 2:
Abstract classes can be defined
public,protected,package,private
Static and non-static properties
Final and non-final attributes
But properties declared in an interface can only be
public
static state
final
Even if there is no explicit declaration
Note: Both abstract classes and interfaces can have entity methods.Entity methods in interfaces, called default methods
package charactor; public interface AP { public static final int resistPhysic = 100; //resistMagic even if it is not explicitly declared public static final //But still defaults to public static final int resistMagic = 0; public void magicAttack(); }
Practice: abstract class
Some items disappear after use, such as blood bottles
Some items will continue to exist after they are used, such as weapons
Design an abstract method for the Item class
public abstract boolean disposable()
Different subclasses will return different values when disposable is implemented.
LifePotion, for example, returns true because it disappears.
Weapon,Armor returns false, because it won't disappear