In depth parsing of abstract classes in Java programming

  • 2020-04-01 04:19:29
  • OfStack

Java programs use abstract classes to implement abstract concepts of nature. The purpose of an abstract class is to organize many related classes together, providing a common class, the abstract class, from which the concrete classes it organizes are derived as subclasses. An abstract class characterizes the public behavior and is passed to its derived classes through an inheritance mechanism. Methods defined in an abstract class are called abstract methods, and these methods have only the declaration of the method header, and instead of the method body definition, a semicolon defines the interface form of the member method, with no concrete operations. Only a redefinition of an abstract member method by a derived class actually implements the operation associated with that derived class.

After each subclass inherits the abstract method of the parent class, it is redefined with different statements and method bodies to form several methods with the same name, the same return value and the same parameter list, with the same purpose but different implementations. The purpose of defining an abstract method in an abstract class is to implement an interface where all subclasses render a method with the same name. An abstract class is a collection of public properties for all 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 take advantage of these common properties to improve the efficiency of developing and maintaining programs. The restrictions on abstract classes and methods are as follows:

(1) the class that is usually modified by the abstract modifier is called abstract class. Member methods that are decorated with the abstract modifier are called abstract methods.
(2) abstract classes can have zero or more abstract methods, or can contain non-abstract methods.
(3) there can be no abstract methods in an abstract class, but the class with an abstract method must be an abstract class.
(4) for an abstract method, only its method name and its type are specified in the abstract class, and its implementation code is not written.
(5) the abstract class can be subclassed, and all the abstract methods defined in the abstract class must be implemented in the subclass derived from the abstract class.
(6) abstract class cannot create objects, the creation of objects by the work of the abstract class derived from the subclass to achieve.
(7) if the parent class has the abstract method with the same name, then the subclass cannot have the abstract method with the same name.
(8) abstract cannot modify the same class with final.
(9) abstract cannot modify the same method with private, static, final or native.

The Java language states that when a class contains abstract methods, the class must be declared an abstract class.

When the subclass inherits the parent class, if there are abstract methods in the parent class, and the subclass thinks it can implement all the abstract methods of the parent class, then the subclass must implement all the abstract methods of the parent class, such as:



class Dog extends Animal {
  
  public String furColor;

  public Dog(String n, String c) {
    super(n);//Calls the constructor of the parent class Animal
    this.furColor = c;
  }

  @Override
  public void enjoy() {
    System.out.println(" The dog barks ....");
  }

}

This parent class in the abstract method, if the subclass feel unable to implement, then declare the subclass as an abstract class, such as:



abstract class Cat extends Animal {

  
  public String eyeColor;

  public Cat(String n, String c) {
    super(n);//Calls the constructor of the parent class Animal
    this.eyeColor = c;
  }
}

Inherited from a class abstraction is here a subclass of Cat Animal, nature also inherited the Animal class declared abstract methods enjoy (), but the subclass the Cat feel to implement this enjoy () method is not appropriate, so it get its own statement into an abstract class, then, who will go to realize this enjoy method of abstract, who inherited the subclasses, who went to implement the abstract methods enjoy (). Such as:



class BlueCat extends Cat {

  public BlueCat(String n, String c) {
    super(n, c);
  }

  
  @Override
  public void enjoy() {
    System.out.println(" Blue cat called ...");
  }
  
}

The complete test code is as follows:


package javastudy.summary;


abstract class Animal {

  public String name;

  public Animal(String name) {
    this.name = name;
  }
  
  
  public abstract void enjoy(); 
  
}


abstract class Cat extends Animal {

  
  public String eyeColor;

  public Cat(String n, String c) {
    super(n);//Calls the constructor of the parent class Animal
    this.eyeColor = c;
  }
}


class BlueCat extends Cat {

  public BlueCat(String n, String c) {
    super(n, c);
  }

  
  @Override
  public void enjoy() {
    System.out.println(" Blue cat called ...");
  }
  
}


class Dog extends Animal {
  
  public String furColor;

  public Dog(String n, String c) {
    super(n);//Calls the constructor of the parent class Animal
    this.furColor = c;
  }

  @Override
  public void enjoy() {
    System.out.println(" The dog barks ....");
  }

}

public class TestAbstract {

  
  public static void main(String[] args) {

    
    //Cat c = new Cat("Catname","blue");
    Dog d = new Dog("dogname","black");
    d.enjoy();//Call your implemented enjoy method
    
    BlueCat c = new BlueCat("BlueCatname","blue");
    c.enjoy();//Call your implemented enjoy method
  }
}


Related articles: