Common knowledge points for abstract classes and abstract methods:
(1) As an inherited class, the subclass must implement all abstract methods in the abstract class unless the subclass is also an abstract class.
That is to say, if the subclass is also an abstract class, the abstract methods in the parent class can be not implemented. But if there is a non-abstract class
Inherited from an abstract subclass, it is necessary to implement abstract subclasses, all abstract methods of abstract parent class of abstract subclasses, and calculate the new and old accounts together.
(2) Abstract classes cannot be modified with final.
(3) Abstract classes cannot be instantiated, which means that you cannot create them through the new keyword when you use them.
(4) Abstract classes can contain abstract methods and non-abstract methods. Abstract methods have no method body, that is, there is no concrete implementation.
It just defines what functions it has, but non-abstract methods contain method bodies.
eg:
Abstract parent class Person:
package com.lanhuigu.java;/** * Abstract class: cannot be modified with final. * can contain abstract methods and non-abstract methods. * The abstract method has no method body, but only defines functions and has no implementation; * The non-abstract method is a normal method. */public abstract class Person {/* * abstract methods */public abstract String eat();public abstract String sleep();/* * non-abstract methods */public String sport() {return "Life lies in movement";}}Non-abstract subclass Teacher:
package com.lanhuigu.java;/** * Inherited from the abstract class person, all abstract methods in the parent class must be implemented*/public class Teacher extends Person {@Override public String eat() {// TODO Auto-generated method stub return "Teacher eats";}@Override public String sleep() {// TODO Auto-generated method stub return "Teacher sleeps";}}Non-abstract subclass Student:
package com.lanhuigu.java;/** * Inherited from the abstract class person, all abstract methods in the parent class must be implemented*/public class Student extends Person {@Override public String eat() {// TODO Auto-generated method stub return "Student eats";}@Override public String sleep() {// TODO Auto-generated method stub return "Student sleeps";}}Abstract subclass OthersPerson:
package com.lanhuigu.java; /** * The abstract class inherits from the abstract class, and you can do not need to implement the abstract method of its parent class*/ public abstract class OthersPerson extends Person { public abstract String call(); }The non-abstract subclass ExtendsOthersPerson inherits from the abstract subclass OthersPerson:
package com.lanhuigu.java;/** * You must implement the abstract parent class OthersPerson, as well as all abstract methods in OthersPerson parent class *, and the new and old accounts must be settled together. */public class ExtendsOthersPerson extends OthersPerson {@Override public String sleep() {// TODO Auto-generated method stub return "Sleep";}@Override public String eat() {// TODO Auto-generated method stub return "Eat";}@Override public String call() {// TODO Auto-generated method stub return "Call";}}Test code:
package com.lanhuigu.java;public class TestAbstract {public static void main(String[] args) {Teacher teacher = new Teacher();Student student = new Student();//OthersPerson othersPerson = new OthersPerson();//An error is reported, abstract class cannot be instantiated System.out.println(teacher.eat());System.out.println(teacher.sleep());System.out.println(teacher.sport());// Inherit the parent class non-abstract method System.out.println("============================================================ Inherit the parent class non-abstract method}} Console output:
Teachers eat, teacher’s life lies in exercise
===================
Students eat, students sleep, their lives lie in exercise
Summarize
The above is all the detailed explanation of Java programming abstract classes and methods, I hope it will be helpful to everyone. Interested friends can continue to refer to this site:
Detailed explanation of static class in java
Java compressed file tool class ZipUtil usage code example
Java Programming Understanding of the Issue of Rewriting Parent Class Methods in Subclasses
If there are any shortcomings, please leave a message to point it out. Thank you friends for your support for this site!