The difference between abstract classes and abstract methods in Java

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 modifying a class with abstract, this class is called an abstract class, and the method is called an abstract method when it is used to modify a method with abstract. For example:

Abstract class:
Abstract class Animal {//using abstract to define a Animal as an abstract class}

Abstract method:

public abstract void enjoy (); To define an abstract method "enjoy" with abstract

2 classes containing abstract methods must be declared as abstract classes, abstract classes must be inherited, and abstract methods 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:

Abstract method public abstract void enjoy ();//This abstract method does not need to implement function//general method public void enjoy () {System.out.print ("Scream"); And the general approach is necessary to implement certain functions}

Example A: polymorphic programs with no abstract classes

Class Person {public    void F () {        System.out.println ("parent Class");}    } Class Students extends person {public    void F () {        System.out.println ("Student Class");}    } Class Teacher extends person {public    void F () {        System.out.println ("Teacher Class");}    } public class Test3 {public    static void Main (string[] args) {person        s = new Students ();        Person T = new Teacher ();        S.F ();        T.f ();    }}

Example B: polymorphic programs for abstract classes

Abstract class Person {public    abstract void f ();} Class Students extends person {public    void F () {        System.out.println ("Student Class");}    } Class Teacher extends person {public    void F () {        System.out.println ("Teacher Class");}    } public class Test3 {public    static void Main (string[] args) {person        s = new Students ();        Person T = new Teacher ();        S.F ();        T.f ();    }}

The difference between abstract classes and abstract methods in Java

Related Article

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.