What are abstract methods in Java and how to use them?

Insert picture description here
Abstraction method...
it looks very abstract when I hear it
, and images of various abstract paintings emerge in my mind,
shrinking in the bed and retreating.

but!
Abstraction in Java is not that difficult. What is an
Insert picture description here
abstract method?
In layman's terms, I
just don’t know how to do it.
Don’t faint,
let me give you a little chestnut.

We create a class, what class? Patient category A
patient is someone who is
sick, what should a sick person do?
You have to take medicine,
Insert picture description here
so
taking medicine is one way for patients

but!
What medicine should I use?
Should he eat Kaisailu?
Should I drink Fengyoujing?

We’re not sure.
It depends on where the patient is.

So we don’t know how to execute this method, it’s called an abstract method

The concept is clear.
How do we define abstract methods?
In fact, it is a simple
common method without curly braces, and an abstract keyword before it, ending with a semicolon. For
example:

public abstract void tackMedicine();

After writing it up, you will be surprised to find that an error was reported! !
why?
Because abstract methods can only be used in abstract classes,
your class definition needs to be changed a little bit: also add an abstract and
it will be this Yazi after writing ↓

public abstract class Patients{
    
    
	public abstract void tackMedicine();
}

ps: There is no problem at all to define ordinary methods in abstract classes~

Now that the abstract class and abstract method are defined, how should we use it?
If you directly violently new an object of this class as before, you will be reported
as pp because our abstract class cannot directly define its object.

How should we use it?
We can create a new subclass to inherit our abstract parent class
like this:

public class ColdPtients extends Patients{
    
    

}

ColdPtients means cold patients, not cold patients

After writing, you will be surprised to find: another error! ! !
Insert picture description here
Stop it!
Don't hit it, don't hit it, you will be stupid if you hit the child again!
We are just the last step~

We need to write the abstract method in the parent class again in the subclass, and remove the abstract keyword , because our subclass is already concrete and no longer need abstract methods.

public class ColdPtients extends Patients{
    
    
	public void tackMedicine(){
    
    
		System.out.println("来点儿清凉油");
	}
}

Alright, we can now start to create subclass objects for use:

public static void main(String[] args){
    
    
	ColdPatients coldPatient = new ColdPatients();
}

Done ~
recap: Let's create a patient an abstract class, which is written in this abstract methods medication, then specific sub-class inherits the parent class, and abstract method abstract parent class removed for use, we Successfully opened a bottle of cooling oil to people who caught a cold.

I am Xiao He in the bed,
I wish you all a happy life!

Guess you like

Origin blog.csdn.net/weixin_51517370/article/details/115264737
Recommended