How thorough understanding of Java abstract class abstract class Why use under what circumstances an abstract class

  How thorough understanding of Java abstract class abstract class Why use under what circumstances an abstract class 

  Now, in the end what is an abstract class, sometimes obviously a general class can be solved, why have to abstract the whole class, loaded to force it

  With this I have doubts, check a lot of information, read a lot of source code, write a lot of code, the following is their own understanding

 First, a thorough understanding of Java abstract class 

  When we can not understand a thing, I have to pursue his origin: Everything Is an Object

  In object-oriented concept, the World Wide depicted by the class, but if a class does not contain enough information to describe a specific business objects, such a class is an abstract class. (Reference https://www.runoob.com/java/java-abstraction.html)

  Whether you seen how much the interpretation of the abstract class in the other channels are not explained in line with the fundamental!

  For chestnuts

  Allows you to design a simple student elective system

  Basic needs: student enrollment, teachers to teach, each teacher can only accept a limited all students, each student can choose a teacher.

  Eventually abstract objects which two areas: student, teacher. When we use to describe the class of students, teachers, and they have in common it is people (Person): has a name, age, occupation and other characteristics, there are classes (learning, teaching all belong to the class) and other acts;

  Now, we think the light of these characteristics, behavior, you can complete description of the object can be operated in elective system (student or teacher) do? Obviously not! Students also need: selected course, other features of the selected teachers and other teachers also want to: limit the number of students, other features such as the name of the student, together with these things, to a complete description of the operation of an object available!

  The following code, Person class as an abstract class, goToClass method is a method subclass must override, indicating that the subclass is to learn, or class

public abstract class Person {
    protected String name;
    protected int age;
    protected int profession;

    public abstract void goToClass();
}
public class Student extends Person {
    private int selLesson;
    private int selTeacher;

    @Override
    public void goToClass() {
        System.out.println("go to studying");
    }
}
public class Teacher extends Person {
    private int studentNumLimit;
    private List<String> students;

    @Override
    public void goToClass() {
        System.out.println("give lessons");
    }
}

 

Second, why use abstract classes under what circumstances the use of abstract classes 

  1, the root cause: not a complete description of a class of things

  2, there is a feature abstract class, an abstract method which must be written (non-abstract subclasses) in the subclass , so that, when the process of our parent class, subclass must implement rewriting, abstract classes. As the above goToClass () method, students, teachers to the classroom is completely different purpose, it is necessary to achieve their own.

  3, compared with the interface , interface method must be implemented by all classes implement the interface method is relatively long, will be trouble, but the abstract class has no such restrictions . For example jdk inside AbstractList, is the parent ArrayList class, which are all methods, you only need to rewrite their needs

  4, easy to understand , sometimes, this design makes it easier to understand the level and so on. Personally think that all the design principles are dead, in order to improve the actual operation of the code, to sacrifice part of the design principle is possible

 

Guess you like

Origin www.cnblogs.com/littlecarry/p/11939809.html