The difference between abstract classes and abstract methods

Source: Internet
Author: User

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. 1 when you use abstract to modify a class, this class is called an abstract class, and when you use abstract to modify a method, the method is called an abstract method. For example: 1abstract class Animal {//Use abstract to define a Animal as abstract class 2}3 and 4public abstract void enjoy (); To define an abstract method using abstract "Enjoy" 2 the class containing the abstract method must be declared as an abstract class, the abstract class must be inherited, and the abstract method must be overridden. 3 abstract classes cannot be instantiated. 4 abstract methods need to be declared without the need to implement certain functions. such as: 1public abstract void enjoy ();//This abstract method does not need to implement the function 2 3public void enjoy () {4system.out.print ("scream");//And the general method is necessary to implement some functions 5} Example A: Polymorphic program without abstract class 01class person {System.out.println-public void F () {}05}06 ("parent class"); 07class Students Extend s person {The public void F () {System.out.println ("Student Class");}11}12 13class Teacher extends person {+ PU Blic void F () {System.out.println ("Teacher Class"),}17}18 19public class Test3 {public static void main (String [] args) {The person s = new Students (); the person t = new Teacher (); s.f (); T.f (); 25}26 } Example B: Polymorphic program of abstract class 01abstract class Person {Lic abstract void f (); 03}04 05class Students extends person {The public void F () {System.out.println ("Student Class"); 08 }09}10 11class Teacher extends person {a public void F () {System.out.println ("Teacher Class");}15}16 17publi C class Test3 {public static void main (string[] args) {The person s = new Students (); the person t = new Teacher (); s.f (); T.f (); 23}24}

The difference between abstract classes and abstract methods

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.