Deep parsing of abstract classes in Java programming _java

Source: Internet
Author: User
Tags modifier

Java programs use abstract classes to implement abstract concepts of nature. The function of an abstract class is to organize many related classes together, providing a common class, an abstract class, and the concrete classes that are organized by it will derive from it as subclasses of it. An abstract class depicts the characteristics of a public behavior and transmits it to its derived classes through an inheritance mechanism. The methods defined in an abstract class are called abstract methods, which have only the declaration of the method header, instead of the definition of the method body by a semicolon, which defines only the interface form of the member method, not the concrete operation. Only the redefinition of the abstract member method by the derived class really implements the operation associated with the derived class.

After each subclass inherits the abstract method of the parent class, it is redefined with different statements and method bodies, with the same number of names, the same return values, and the same argument list, with the same purpose but with a certain difference in the implementation of the method. The purpose of defining an abstract method in an abstract class is to implement an interface in which all subclasses render a method of the same name. An abstract class is a collection of public properties for all of its subclasses, and is a class that contains one or more abstract methods. One of the great advantages of using abstract classes is that you can make the most of these common properties to improve the efficiency of your development and maintenance programs. The restrictions for abstract classes and abstract methods are as follows:

(1) A class that is decorated with the abstract modifier is called an abstract class. A member method decorated with the abstract modifier is called an abstract method.
(2) An abstract class can have 0 or more abstract methods, or it can contain non-abstract methods.
(3) There can be no abstract method in an abstract class, but a class with an abstract method must be an abstract class.
(4) For abstract methods, specify only its method name and its type in an abstract class without writing its implementation code.
(5) Abstract classes can derive subclasses, and all abstract methods defined in an abstract class must be implemented in subclasses that derive from the abstract class.
(6) An abstract class cannot create an object, and the work of creating the object is implemented by subclasses derived from the abstract class.
(7) If an abstract method with the same name already exists in the parent class, there can be no more abstract methods with the same name in the subclass.
(8) Abstract cannot modify the same class as final.
(9) Abstract cannot modify the same method with private, static, final or native.

The Java language stipulates that when a class has an abstract method, the class must be declared as an abstract class.

When a subclass inherits a parent class, if the parent class has an abstract method and the subclass feels that it is possible to implement all the abstract methods of the parent class, then the subclass must implement all the abstract methods of the parent class, such as:

The/**
 * Subclass Dog inherits the abstract class Animal and implements the abstract method enjoy
 * @author gacl
 * */
class Dog extends Animal {
  /**
   * Dog class to add its own unique
   properties
  /public String furcolor;

  Public Dog (string n, string c) {
    super (n);//Calling the constructor method of the parent class animal
    this.furcolor = c;
  }

  @Override public
  Void Enjoy () {
    System.out.println ("dog called ...");
  }



The abstract method within the parent class, if it doesn't feel like it can be implemented, also declares the subclass as an abstract class, such as:

/**
 * Here the subclass cat inherits from the abstract class animal and naturally inherits the abstract method declared in the animal class enjoy (),
 * But the subclass cat feels it is not appropriate to implement this enjoy () method, So it declares itself as an abstract class,
 * So, who is going to implement this abstract enjoy method, who inherits the subclass, and who is going to implement this abstract method enjoy ().
 * @author GACL
 * * * */
abstract class Cat extends Animal {

  /**
   * Cat add its own unique attributes
   * * * Public
  String Eyecolor;

  Public Cat (string n, string c) {
    super (n);//Invoke the constructor method of the parent class animal
    this.eyecolor = c;
  }
}

The subclass cat inherits from the abstract class animal and naturally inherits the abstract method enjoy () declared in the animal class, but the subclass cat feels it is not appropriate to implement the enjoy () method, so it declares itself as an abstract class, then, Who is going to implement this abstract enjoy method, who inherits the subclass, who is going to implement this abstract method enjoy (). Such as:

The/**
 * Subclass BlueCat Inherits abstract class cat and implements an abstract method inherited from the parent cat enjoy
 * @author GACL * * * */
class BlueCat extends Cat { Public

  BlueCat (string n, string c) {
    super (n, c);
  }

  /**
   * Implements the abstract method enjoy
   /
  @Override public
  Void Enjoy () {
    System.out.println ("Blue cat called ...");
  }
  


The complete test code is as follows:

Package javastudy.summary; /** * Parent Class Animal * In the front of the class with abstract, that is to say: abstract class Animal * So the Animal class is an abstract class of the/abstract class Animal {p

  Ublic String name;
  Public Animal (String name) {this.name = name;
   /** * Abstract Method * Here only the definition of the method, there is no implementation of the method. 
  
* * Public abstract void enjoy (); /** * The subclass cat here inherits from the abstract class animal, nature also inherits the abstract method declared in the animal class enjoy (), * But the subclass cat feels it is not appropriate to implement the enjoy () method, so it declares itself as an abstract class, * then,
 Who is going to implement this abstract enjoy method, who inherits the subclass, who is going to implement this abstract method enjoy ().

  * @author GACL * * */abstract class Cat extends Animal {/** * cat add its own unique attributes/public String Eyecolor;
  Public Cat (string n, string c) {super (n);//Invoke the constructor method of the parent class animal this.eyecolor = C; }/** * Subclass BlueCat Inherits abstract class cat and implements an abstract method inherited from the parent cat enjoy * @author gacl * * */class BlueCat extends Cat {public BLUEC
  At (string n, string c) {super (n, c);
  /** * Implements the abstract method enjoy/@Override public void enjoy () {System.out.println ("Blue cat called ..."); }/** * Subclass dog Inherits the abstract class animal and implements the abstract method ENJOY * @author GACL * */class Dog extends Animal {/** * Dog class to add its own unique attributes/public String Furcolor;
  Public Dog (string n, string c) {super (n);//Calling the constructor method of the parent class animal this.furcolor = C;
  @Override public void Enjoy () {System.out.println ("dog called ...");  } public class Testabstract {/** * @param args/public static void main (string[] args) {/** *
     After you declare a cat class as an abstract class, you can no longer instantiate the cat class, because abstract classes are incomplete and lack arms and legs, so abstract classes cannot be instantiated.
    * *//cat C = new Cat ("CatName", "Blue");
    Dog d = new Dog ("Dogname", "Black");
    D.enjoy ()//calls its own implemented enjoy method BlueCat C = new BlueCat ("Bluecatname", "Blue");
 C.enjoy ()//calling the Enjoy method that you implemented}

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: info-contact@alibabacloud.com 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.