Abstract method: A method that does not have a method body in a class is an abstract method.
Abstract classes: Classes that contain abstract methods are called abstract classes.
||||||||| Abstract classes cannot be instantiated (new)
Why use abstract methods?
(The following is a personal view)
Because in the idea of object-oriented programming, everything can be considered a class. For example: Every animal needs to rest, sleep (sleep), and different animals have different ways of sleeping, such as, the bat is hanging upside down in the tree sleep, the puppy is crawling to sleep, the snake is on the plate to sleep ... Wait a minute. It can be seen that each animal sleeps differently, but turns to think, and they do the same thing (sleep). Therefore, the concept of abstract classes is introduced in Java. That is, some methods in the parent class do not contain any logic (for example, the sleep method in the example above) and need to be overridden by subclasses (for example, bats are hung asleep ...). In this case, we should abstract these methods with common characteristics, and subclasses are responsible for the implementation details of these abstract methods. The parent class provides only the name and declaration of this method, and no body of the method. Therefore, the subclass is responsible for overriding this method. So that's why you should use abstract methods! (Personal opinion, I don't know, right?) )
============================================
The following code:
-----------------------------
Class Boar
{
abstract void sleep ();
}
-----------------------------
In this code, the class boar has an abstract method of sleep (), you can see that there is no method body behind this method, but with a ";" Sign end. Then this method is called the abstract method.
If you compile this code, you will be prompted with the following error:
----------------------------------------------------------------
Boar.java:1: Boar is not abstract and does not overwrite the abstract method in Boar sleep ()
Class Boar
^
1 Error
----------------------------------------------------------------
It means: Boar This class is not abstract. Why is it? Because the previous definition of the abstract class, it is said that the class containing the abstract method is called an abstract class. Therefore, the Boar class must also be abstract!
Therefore, the following code should be changed:
-----------------------------
Abstract class Boar
{
abstract void sleep ();
}
-----------------------------
If this is compiled, you will not be prompted for any errors.
============================================
Consider the following code:
-------------------------------------------------------------------
Abstract class Allsleep
{
abstract void sleep ();
}
Class Owl extends Allsleep
{
void Sleep ()
{
System.out.println ("I'm an owl, I'm sleeping on a tree");
}
}
Class Bat extends Allsleep
{
void Sleep ()
{
System.out.println ("I'm a bat, I'm hanging upside down in a tree");
}
}
public class Newsleep
{
public static void Main (string[] SL)
{
Owl Objowl=new Owl ();
Bat objbat=new Bat ();
Objowl.sleep ();
Objbat.sleep ();
}
}
-------------------------------------------------------------------
The compilation does not go wrong, and the result is printed:
----------------------------
I'm an owl, I'm standing in a tree, sleeping.
I'm a bat, I sleep upside down in a tree.
----------------------------
But if you change the code to read as follows:
-------------------------------------------------------------------
Abstract class Allsleep
{
abstract void sleep ();
}
Class Owl extends Allsleep
{
/*void Sleep ()
{
System.out.println ("I'm an owl, I'm sleeping on a tree");
}*/
}
Class Bat extends Allsleep
{
/*void Sleep ()
{
System.out.println ("I'm a bat, I'm hanging upside down in a tree");
}*/
}
public class Newsleep
{
public static void Main (string[] SL)
{
/*owl objowl=new Owl ();
Bat objbat=new Bat ();
Objowl.sleep ();
Objbat.sleep (); */
}
}
-------------------------------------------------------------------
The JVM will prompt the following when compiling:
--------------------------------------------------------------------------
Newsleep.java:5: Owl is not abstract and does not overwrite the abstract method in Allsleep sleep ()
Class Owl extends Allsleep
^
Newsleep.java:12:bat is not abstract and does not overwrite the abstract method in Allsleep sleep ()
Class Bat extends Allsleep
^
2 Error
--------------------------------------------------------------------------
The Owl and Bat classes do not implement the sleep () method in Allsleep, but only numb inheritance comes
So we get the conclusion that abstract methods in abstract classes must be implemented!
So in order to confirm this conclusion, write another piece of code, as follows:
---------------------------------------------
Abstract class Allsleep
{
void Eat ()
{
System.out.println ("I will Eat");
}
}
Class Owl extends Allsleep
{
}
Class Bat extends Allsleep
{
}
---------------------------------------------
From the above code we can see that there is no abstract method in the abstract class allsleep, and we do not implement the Eat () method in the Allsleep class in subclasses, so this confirms our conclusion that abstract methods in abstract classes must be implemented!
=====================================================
Anyway, what if a subclass doesn't implement an abstract method in the parent class?
Let's do an experiment:
-------------------------------
Abstract class Allsleep
{
abstract void sleep ();
}
Class Owl extends Allsleep
{
}
Class Bat extends Allsleep
{
}
-------------------------------
What will be the result of compiling this piece of code?
In fact, this experiment has been done, in the last example, the Owl class and the Bat class is masked, with this is an effect. That is to say: abstract methods in abstract classes must be implemented!
So what do we do if we don't want it to happen? Very simple, as long as the handle class is declared an abstract class is OK!
So we get the conclusion that if a subclass does not implement an abstract method in the parent class, the subclass becomes an abstract class!
Finally, to summarize the following:
----------------------------------------------------------------
Abstract method: A method that does not have a method body in a class is an abstract method.
Abstract classes: Classes that contain abstract methods are called abstract classes.
Abstract methods in abstract classes must be implemented!
If a subclass does not implement an abstract method in the parent class, the subclass becomes an abstract class!
The normal method in an abstract class can be implemented without having to implement it.
Abstract classes and abstract methods of Java