Java abstract classes and interfaces

Source: Internet
Author: User

First, abstract class

1.abstract Modifier class: abstract class

    • Cannot be instantiated
    • Abstract classes have constructors (all classes have constructors)
    • The class in which the abstract method resides must be an abstract class
    • There can be no abstract method in an abstract class

2.abstract Retouching Method: Abstract method

    • Format: No method body, including {}. such as: Public abstract void Eat ();
    • Abstract methods only retain the functionality of the method, and the specific execution is given to subclasses of the inherited abstract class, which is overridden by subclasses.
    • If a subclass inherits an abstract class and overrides all the abstract methods, this class is an "entity class" that can be instantiated
    • If a subclass inherits an abstract class and does not override all abstract methods, meaning that there are still abstract methods in this class, then this class must be declared abstract!

Note: Abstract cannot be used to decorate properties, constructors, private, final, static

 PackageCom.yyx.pratice; Public classJavapratice { Public Static voidMain (string[] args) {Student stu=NewStudent (); /*Error person person=new person (); */        NewPerson () {//Anonymous Class@OverridevoidSayword () {System.out.println ("I am an anonymous class");        }}.sayword ();;    Stu.sayword (); }}Abstract classPerson {Abstract voidSayword ();  Public voidReadbook () {System.out.println ("People can read."); }     PublicPerson () {System.out.println ("Abstract classes have constructors and cannot be instantiated by new, but when subclasses create objects, I create instances first"); }}classStudentextendsPerson {@OverridevoidSayword () {System.out.println ("I am a child of man, and I will speak."); }}

Operation Result:

Abstract classes have constructors and cannot be instantiated by new, but when subclasses create objects I create instances first
Abstract classes have constructors and cannot be instantiated by new, but when subclasses create objects I create instances first
I am an anonymous class
I am a child of man, I will speak

Second, the interface

Interface (interface) is a concept that is parallel to a class

    • An interface can be seen as a special abstract class. is a set of constants and abstract methods that cannot contain variables, general methods
    • Interface is not a constructor
    • An interface defines a function. This function can be implemented by the class (implements)
    • A class that implements an interface must override all of its abstract methods before it can be instantiated. If all the abstract methods are not overridden, then this class is still an abstract class
    • A class can implement multiple interfaces. ----Inheritance of classes in Java is single-inheritance
    • Interfaces and interfaces are also inherited relationships, and multiple inheritance can be implemented
 PackageCom.yyx.pratice; Public classTestInterface { Public Static voidMain (string[] args) {/** polymorphic, cannot call interface No, implement class some methods*/CC CPerson=NewPerson (); DD Dperson=NewPerson ();        Print (CPerson);        Printspeak (Dperson);    System.out.println (Cperson.age); }     Public Static voidprint (CC c) {c.walk ();        C.eat ();    C.study (); }     Public Static voidPrintspeak (DD d) {d.speak (); }}InterfaceAA {voidwalk ();}InterfaceBB {voideat ();}InterfaceDD {voidspeak ();}InterfaceCcextendsAA, BB {//interface supports multiple inheritance    Final intAge = 22; voidstudy ();}classPersonImplementsCC, DD {@Override Public voidWalk () {System.out.println ("People can walk."); } @Override Public voideat () {System.out.println ("People will eat."); } @Override Public voidStudy () {SYSTEM.OUT.PRINTLN ("People will learn"); } @Override Public voidspeak () {System.out.println ("People can talk."); }     Public voidswim () {System.out.println ("People can swim."); }}

Java abstract classes and interfaces

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.