Java object-oriented abstract class, interface

Source: Internet
Author: User
Tags scream

Abstract class:

Classes that contain abstract methods are declared abstract classes
Abstract methods are implemented by subclasses.

Classes that contain abstract methods must be declared as abstract classes

Abstract class Quilt class inheritance, subclasses (if not abstract classes) must override all abstract methods in the abstract class

Abstract method:

Methods that are declared but not implemented

Abstract classes cannot be instantiated
Subclasses can not implement, by the next level of subclasses to implement, the current class is declared abstract class, inherited this method is declared as an abstract method
The permission modifier for the abstract method must be public

Abstract

//Human Public classPerson {PrivateString name; //custom Attribute Reference type Pets    PrivatePet Pet;  PublicPerson () {} PublicPerson (String name, pet pet) { This. Name =name;  This. Pet =Pet; }     Public voidPethappy () {pet.scream (); }         PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; }     PublicPet Getpet () {returnPet; }     Public voidSetpet (Pet pet) { This. Pet =Pet; }}
//Parent class abstract class of pet class Public Abstract classPet {PrivateString pname; //Abstract classes can have construction methods     PublicPet () {} PublicPet (String pname) { This. pname =pname; }    //Abstract methods cannot have method bodies.     Public Abstract voidScream ();  PublicString Getpname () {returnpname; }     Public voidsetpname (String pname) { This. pname =pname; }}
//Sub-category Tiger class Public classTigerextendsPet { PublicString fur;//the color of the fur     PublicTiger (string name, String fur) {Super(name);  This. Fur =fur; }     PublicTiger () {Super(); } @Override Public voidScream () {System.out.println ("It's a tiger's voice!"); }     PublicString Getfur () {returnfur; }     Public voidSetfur (String fur) { This. Fur =fur; }}
//Sub-category Rabbit class Public classRabbitextendsPet { PublicString fur;//the color of the fur     PublicRabbit (string name, String fur) {Super(name);  This. Fur =fur; }     PublicRabbit () {Super(); } @Override Public voidScream () {System.out.println ("Rabbit will sell Moe!"); }     PublicString Getfur () {returnfur; }     Public voidSetfur (String fur) { This. Fur =fur; }}
// Main Method  Public class Main {    publicstaticvoid  main (string[] args) {        new Tiger ("qqqq", "www");         New Rabbit ();                 New Person ("Xiao Ming", T1)        ; New Person ("Little Red", R1);                P1.pethappy ();        P2.pethappy ();    }}

Interface:

A special abstract class, all of which are composed of global constants and common abstract methods.

Implementation relationships between classes and classes

The implementation of the interface must also be through subclasses, using the keyword implements, and the interface can be implemented more

A class can inherit and implement interfaces at the same time

An excuse cannot integrate an abstract class, but can inherit multiple interfaces at the same time through the extends keyword, implementing multiple inheritance of the interface

 Public Interface InterFaceTest1 {    publicstaticfinal String name = "Hanqi";     = "Tianqi";     // interface-defined properties default public static final     Public void TestMethod ();}

A class to implement a method in which an interface must implement

Inheritance: Multiple inheritance is not supported in Java, a class can inherit only one

Interfaces: Interfaces can implement multiple

// Interface 1  Public Interface Inter {    int age=22;   the constants in the interface are global constants, which are the default public, static, and final non-changing publicly    abstract void Tell ();}
// Interface 2  Public Interface Inter2 {    publicabstractvoid  Say ();}
// Interface 3  Public Interface extends inter,inter2{    // Interface Inheritance interface     // An interface cannot inherit an abstract class, but can inherit multiple interfaces. Implementing multiple inheritance of Interfaces }
// Abstract class  Public Abstract class CHOUXC {    publicabstractvoid  write ();}
//sub-class Public  classShixianextendsChouxcImplementsinter,inter2{ Public voidTell () {System.out.println ("This is the abstract method of the interface Inter implemented by the class."); }     Public voidsay () {System.out.println ("This is the abstract method in the interface Inter2 implemented by the class"); }     Public voidwrite () {System.err.println ("This is an abstract method in a class-inherited abstract class"); }}
// Main Method  Public class InterTest1 {    publicstaticvoid  main (string[] args) {        //  TODO auto-generated method stub        Shixian s=new  Shixian ();        S.tell ();        S.say ();        System.out.println (inter.age);        System.out.println (s.age);    }}

Java object-oriented abstract class, interface

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.