Abstract class and abstract method in Java

A class modified with the keyword abstract is called an abstract class (abstract class) . Such as:

abstract  class A  {
      abstract int min(int x,int y);
}

The method modified with the keyword abstract is called the abstract method (abstract method) ,

For abstract methods, only declarations are allowed, implementations are not allowed , and final and abstract are not allowed to modify a method at the same time, for example:

abstract int min(int x,int y);

Understand the abstract class

( 1 ) Abstract classes can abstract important behavior standards , which are represented by abstract methods. That is, the abstract class encapsulates the behavior standards that the subclass must have.

( 2 ) The object declared by the abstract class can become the upper transformation object of the object of its subclass , calling the subclass overriding method, that is, reflecting the specific behavior given by the subclass according to the behavior standard in the abstract class.

abstract class  GirlFriend  //抽象类封装了两个行为标准
{
	abstract void speak();
	abstract void cooking();
}

class ChinaGirlFriend extends GirlFriend 
{
	void speak(){
		System.out.println("你好!");
	}
	void cooking(){
		System.out.println("水煮鱼!");
	}
}

class AmericanGirlFriend extends GirlFriend
{
	void speak(){
		System.out.println("hello !");
	}
	void cooking(){
		System.out.println("roast beef!");
	}
}

class Boy
{
	GirlFriend friend;
	void setGirlfriend(GirlFriend f){
		friend = f;
	}
	void showGirlFriend(){
		friend.speak();
		friend.cooking();
	}
}

public class Example5_11
{
	public static void main(String args[]){
		GirlFriend girl = new ChinaGirlFriend();  //girl是上转型对象
		Boy boy = new Boy();
		boy.setGirlfriend(girl);
		boy.showGirlFriend();

		girl = new AmericanGirlFriend();
		boy.setGirlfriend(girl);
		boy.showGirlFriend();
	}
}

 

abstract class 机动车 
{
	abstract void 启动();
	abstract void 加速();
	abstract void 刹车();
}

class 手动挡轿车 extends 机动车
{
	void 启动(){
		System.out.println("踏下离合器,换到一档。");
		System.out.println("然后慢慢抬起离合器。");
	}
	void 加速(){
		System.out.println("踩油门!");
	}
	void 刹车(){
		System.out.println("踏下离合器,踏下刹车板。");
		System.out.println("然后将挡位换到一档。");
	}
}

class 自动挡轿车 extends 机动车
{
	void 启动(){
		System.out.println("使用前进档。");
		System.out.println("然后轻踩油门。");
	}
	void 加速(){
		System.out.println("踩油门!");
	}
	void 刹车(){
		System.out.println("踏下刹车板。");
	}
}

public class Example5_12
{
	public static void main(String args[]){
		机动车 car = new 手动挡轿车();
		System.out.println("手动挡轿车的操作:");
		car.启动();
		car.加速();
		car.刹车();

		car = new 自动挡轿车();
		System.out.println("自动挡轿车的操作:");
		car.启动();
		car.加速();
		car.刹车();
	}
}

The three characteristics of abstract methods are as follows:

  1. Abstract method has no method body
  2. The abstract method must exist in the abstract class
  3. When the subclass rewrites the parent class, it must rewrite all the abstract methods of the parent class


Note: You cannot use the private modification when using the abstract keyword to decorate the abstract method, because the abstract method must be overridden by the subclass, and if the private declaration is used, the subclass cannot be overridden.

The definition and usage rules of abstract classes are as follows:

  1. Both abstract classes and abstract methods are declared using the abstract keyword.
  2. If a method is declared as abstract, then the class must also be declared as abstract. In an abstract class, there can be 0~n abstract methods and 0~n concrete methods.
  3. Abstract classes cannot be instantiated, that is, objects cannot be created using the new keyword.

 

Guess you like

Origin blog.csdn.net/qq_43629083/article/details/108692295