当前位置:网站首页>What is an abstract class? How to define abstract classes?

What is an abstract class? How to define abstract classes?

2022-06-23 16:39:00 User 7353950

When defining a class , It is often necessary to define some member methods to describe the behavior characteristics of the class , But sometimes the implementation of these methods is uncertain . for example , In defining Animal Class time ,shout( ) This method is used to describe animal calls , But the sounds of different animals are different , So in shout( ) The method can not accurately describe the animal cry . For the situation described above ,Java Abstract methods are provided to meet this need . The abstract method is to use abstract Keyword decorated member method , Abstract methods do not need to implement the method body when they are defined . The definition format of the abstract method is as follows :

abstract void Method name  ( Parameters );

When a class contains abstract methods , This class must be abstract . Abstract classes are the same as abstract methods , You have to use abstract Keyword modification . The definition format of the abstract class is as follows :

abstract class  Abstract class name {

 Access permission return value type   Method name ( Parameters ){

return [ Return value ] ;

 Access right abstract Return value type abstract method name ( Parameters ) ;

// Abstract method , There is no method 

}

From the above format, you can find , Abstract classes have more abstract methods than ordinary classes , The composition of other places is basically the same as that of ordinary classes . The definition rules of abstract classes are as follows . (1) A class containing more than one abstract method must be an abstract class . (2) Both abstract classes and abstract methods use abstract Keyword declaration . (3) Abstract methods only need to be declared but not implemented . (4) If a class inherits an abstract class , Then the subclass must implement all abstract methods in the abstract class . Let's learn how to use abstract classes through a case , Such as file 4-10 Shown . file 4-10 Example10.java

 // Defining abstract classes Animal

   abstract class Animal(

  // Define abstract methods shout ()

   abstract void shout () ;
}

 // Definition Dog Class inherits abstract class Animal

class Dog extends Animal {

  // Implement abstract methods shout ()

  void shout ()System.out.println (" Wang Wang ..... ;
     
}}

 // Define test classes 

 public class Example10 {public static void main (String[] args) {Dog dog=new Dog();         // establish Dog class 

dog. shout() ;           //  call dog Object's shout () Method 

 }
 
}

In the file 4-10 in , The first 2~5 Lines of code declare an abstract class Animal, And in Animal Class declares an abstract method shout ( ); The first 9~11 Lines of code in subclasses Dog Implementing a parent class in Anima Abstract method of shout( ); The first 17 Line code calls... Through instantiated objects of subclasses shout( ) Method . Be careful : Use abstract Abstract methods decorated with keywords cannot use private modification , Because abstract methods must be implemented by subclasses , If used private Statement , The subclass cannot implement the method .

原网站

版权声明
本文为[User 7353950]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/174/202206231553182568.html

随机推荐