java object 20_ abstract class (abstract)

1. abstract class overview

When writing a class, we tend to class defines the methods that are used to describe the function of the class specific implementation, these methods have specific method body.

But sometimes, just know that a parent subclass should include how to approach, but not exactly know how the sub-class implements these methods. For example, a graphics class should have a method to find the circumference, but different algorithms find the circumference of the pattern is not the same. Then how to do it?

When analyzing things, we found common content, there have drawn up. There is such a special case, the method is the same function declarations, but different methods of body functions. Then the time can be extracted, but only extraction method statement, not subject extraction method. Then this method is an abstract method.

For example: Description Instructor behavior: work.

Description assistant behavior: work.

Description of teacher behavior: work.

There are similarities between the lecturers, tutors, class, may be extracted upwards. They belong to extract common types: employees. As the instructor, teaching assistant, teacher has to work function, but they are not the same as the content of the specific work. At this point in describing the staff we found some functions can not be described, then these are not specific features, you need to identify it in the class, modified by java keywords abstract (abstract).

The statement did not realize method only, with abstract to modify the method is an abstract method. Abstract method must be defined in an abstract class, the class also be modified with abstract.

2. The use of abstract classes

Defines an abstract class: permission modifier abstract class name of the class {}

Defines an abstract method: permission modifier abstract return type method name (parameter list);

[Example] abstract class defines the abstract method &

// 抽象类,员工类
abstract class Employee {
	// 抽象方法,用abstract修饰,只有声明没有实现
	public abstract void work();
}
// 讲师类
class Teacher extends Employee {
	@Override
	public void work() { // 实现抽象方法
		System.out.println("老师讲java课");
	}
}
// 助教类
class Assistant extends Employee {
	@Override
	public void work() { // 实现抽象方法
		System.out.println("助教协助同学解决bug");
	}
}

3. Features abstract class

Abstract class features:

  1. Abstract class called modified with an abstract class.

  2. An abstract class can not be instantiated, because only an abstract method declaration can not call this method with the object.

  3. Abstract methods can define member variables and static variables.

  4. An abstract class can have a constructor, assigns them the convenience abstract class subclass create the object.

  5. An abstract class can be defined in the conventional method, can also define an abstract method (method may not abstract).

  6. Abstract parent class must be a need Subclasses override abstract methods of the parent class, then the instances of use.

Abstract method is characterized by:

  1. Abstract modification using a method called abstract methods.

  2. Abstract method only method declaration, there is no method body.

  3. Subclass or implement abstract methods of the parent class or sub-class is an abstract class, or fail to compile.

  4. Abstract method can only exist in the abstract class or interface, it can not exist in an ordinary class.

Note: abstract and keywords can not be private, final, static keyword coexist!

ps: For the latest free documentation and instructional videos, please add QQ group (627,407,545) receive.

Published 55 original articles · won praise 0 · Views 788

Guess you like

Origin blog.csdn.net/zhoujunfeng121/article/details/104619453