[Notes] from scratch to learn Java keyword abstract

We can focus on the author's account, focus on learning Java from scratch notes anthology. We can also learn from a catalog visit the author's blog garden blog. The film documents the learning and the sharing of information on employment-based programmers dark horse class video, and record notes and their own views. Welcome to learn and discuss.

[Notes] from scratch to learn Java directory

What is an abstract?

In Java, abstract concepts and methods commonly used in the class
abstract method: If you want to design such a class that contains a special member of the method, the specific implementation of the method is determined by its subclasses, then you can be declared in the parent class this method is an abstract method. Abstract method only contains a method name, and no method body.
Abstract classes: the object-oriented concept, all objects are described by type, but conversely, not all of the classes are used to describe an object, if a class does not contain enough information to describe a specific objects, such a class is an abstract class.

Keywords: abstract (for modification methods and classes)

for example

public class AbstractTest {
	public static void main(String[] args) {
		Programmer p = new Programmer();
		p.name = "刘备";
		p.work();
		
		Manager m = new Manager();
		m.name = "张飞";
		m.work();
		m.bouns();		
	}
}

abstract class Employee{
    //程序员和经理的共同属性	
    String name;
	int id;
	double pay;
	
    //抽象类没有方法体
	public abstract void work();
}

class Programmer extends Employee{

	@Override
    //对抽象方法的重写
	public void work() {
		System.out.println(name+"的工作内容是写代码");
		
	}
	
}

class Manager extends Employee{

	@Override
    //对抽象方法的重写
	public void work() {
		System.out.println(name+"的工作内容是管理程序员");
		
	}
	
	public void bouns() {
		System.out.println("获得奖金");
	}
	
}

It can be seen programmers and managers have a lot in common, such as name and office and other properties of this method, all write a parent class to improve code reusability. But apparently the work of programmers and managers are not the same, it is not possible for the parent class work()method to write, so I do not write the method body, then rewrite abstract methods of the parent class by the child class, this is abstraction.

Abstract class features:

Abstract methods in an abstract class which can only
abstract classes and abstract methods must be modified abstract
abstract class can not create object (can not be instantiated)
abstract class can have non-abstract way
the relationship between abstract classes and Continuation is also
a Continuation of the class the abstract class or override all the abstract methods, or he himself is an abstract class

It features abstract class members:

  • Member variables
    can have member variables
    can have a constant
  • Member method
    can have abstract methods
    can have non-abstract method
  • Constructor
    can have constructor, the need for an abstract class member variables are initialized

You can not coexist abstract keyword abstract and which keywords?

Abstract method, there is no method body, must be rewritten to use, so the method can not be overridden is no abstract sense.
Keyword is used to modify the final class, member variables, member methods, modified class can not be inherited, member variables can not be modified, members of the method can not be rewritten, so abstract and final mutually exclusive.
Private modification of the parent class member variables, member methods can not be accessed by subclasses, so abstract and coexistence does not make sense.

Is there a constructor abstract class?

Although abstract classes can not be instantiated, but there are still a member variable, needs to be initialized, all constructors.
Constructor java abstract class and normal class constructor method, are used to initialize the class, but an abstract class constructor can not be called directly because the abstract class can not be achieved instance, but once an ordinary class inherits the abstract class can also be constructed call the constructor function of the abstract class.

Can an abstract class without abstract methods?

You may, if one does not want to be instantiated method, can use an abstract class without abstract methods, such as an IO stream writer and reader classes.

Guess you like

Origin www.cnblogs.com/zllk/p/12656805.html