Software Engineering java-Abstract class

Source: Internet
Author: User

We all know that in an object-oriented world, everything is an object, and all objects are described by classes, but not all classes are intended to describe objects. If a class does not have enough information to describe a specific object and needs other concrete classes to support it, then such a class is called an abstract class. For example New Animal (), we all know that this is an animal Animal object, but this Animal exactly what it looks like we do not know, it does not have a specific animal concept, so he is an abstract class, need a specific animal, such as dogs, cats to the specific description of it , we knew what it was like.

Abstract classes used to characterize abstract concepts cannot be instantiated in the object-oriented domain because abstract concepts have no corresponding specific concepts in the problem domain.

At the same time, abstract class embodies the idea of data abstraction and is a mechanism to realize polymorphism. It defines a set of abstract methods that are implemented by a derived class for the specific representation of this set of abstract methods. At the same time, abstract classes provide the concept of inheritance, the starting point is to inherit, otherwise it does not exist any meaning. Therefore, the definition of the abstract class must be used to inherit, at the same time in an abstract class as a node of the inheritance relationship hierarchy chain, the leaf node must be a specific implementation class. (I do not know if this understanding is wrong!!! Expert guidance ....)

There are a few things to keep in mind when using abstract classes:

1, abstract class can not be instantiated, the work of the instantiation should be left to its subclasses to complete, it only need to have a reference.

2. Abstract methods must be overridden by subclasses.

3. As long as an abstract class containing an abstract method, the method must be defined as an abstract class, whether or not there are other methods included.

4, the abstract class can contain specific methods, of course, can not contain abstract methods.

5. Abstract methods in subclasses cannot have the same name as the abstract methods of the parent class.

6. Abstract cannot be modified in the same class as final.

7. Abstract cannot be modified with private, static, final, or native the same method. 、

Instance:

Define an abstract animal class animal, which provides an abstract method called Cry (), a cat, a dog is a subclass of animal class, because cry () is an abstract method, so cat, dog must implement the Cry () method. As follows:

Public abstract class Animal {

public abstract void Cry ();

}

public class Cat extends animal{

public void Cry () {

System.out.println ("Cat's name: Meow meow ...");

}

}

public class Dog extends animal{

public void Cry () {

System.out.println ("Dog bark: Bark ...");

}

}

public class Test {

public static void Main (string[] args) {

Animal a1=new Cat ();

Animal a2=new Dog ();

A1. Cry ();

A2. Cry ();

}

}

It is useful to create abstract classes and abstract methods, because they can clarify the abstraction of a class and tell the user and the compiler how they intend to use them. Abstract classes are also useful refactorings because they enable us to easily move public methods up and down the inheritance hierarchy.

Software Engineering java-Abstract class

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.