Java Note 6__ abstract class/interface/polymorphic/instanceof keyword, parent design rule

Source: Internet
Author: User

/*** Abstract class: Many classes with the same characteristics and behavior can be abstracted as an abstract class * 1. Abstract classes can have no abstract methods, and classes with abstract methods must be abstract classes * 2. Abstract classes that are not abstract classes must implement abstract methods "can be NULL implementations" * 3. Abstract classes can have methods and properties * 4. Abstract classes cannot be instantiated * 5. Abstract classes cannot be declared as final * 6. Abstract classes can have a constructor method "does not represent an instantiated object"*/ Public classMain { Public Static voidMain (string[] args) {Goddess M1=Newgoddess (); M1.setname ("Dining");                M1.say (); Ugly m2=NewUgly (); M2.setname ("Fish");;    M2.say (); }  }Abstract classwoman{//woman as abstract class    PrivateString name;  PublicWoman () {System.out.println ("I am the constructor of an abstract class. "); }     Public voidsetName (String name) { This. Name =name; }     PublicString GetName () {returnname; }     Public Abstract voidSay ();//abstract method (declaration only, not implemented)}classGoddessextendswoman{//implementing an abstract method of the parent class     Public voidsay () {System.out.println ("I am the goddess, my name is:" +getName ()); }}classUglyextendswoman{ Public voidsay () {System.out.println ("I am an ugly girl, my name is:" +getName ()); }}

/*** Interface: A set of behavior specifications, definitions, not implemented * Interface Definition Format: * Interface interface Name {* Global constant; * abstract method; *} * 1. A class can implement multiple interfaces * 2. Abstract class implementation interface can not implement method * 3 The access rights for all methods in the interface must be public * 4. The properties defined in the interface are constants*///define an interfaceInterfacehit{ Public Static FinalString NAME = "D";//properties defined in an interface are constants public static final can be omitted     Public voidCry ();//Abstract method Public abstract (can be omitted)}Interfaceieat{ Public voideat ();}classGoddessImplementshit,ieat{//implementing an interface must implement all the methods in the interface     Public voidcry () {System.out.println ("It's killing me."); }     Public voideat () {System.out.println ("A little bite from a small stutter."); }}classGirlImplementshit{ Public voidcry () {System.out.println ("Your Sister!" The pain of the mother ~ "); }}Abstract classPersonImplementshit{}

 Public classMain { Public Static voidMain (string[] args) {//The parent class is large, and the sub-class is small "upward transformation: Reference to the parent class to the subclass object"person S1 =NewMan (); S1.say (); //If there is no say method in the person class, you cannot callS1.talk ();//Call the Talk method in the man class, not the talk method in the person class, and if there is no talk method in the man class , call the Talk method in the person classPerson S2 =Newwoman ();                        S2.say (); //"Transformation down: Converting a parent class instance to a subclass instance (cast)"Man W1 =(man) s1;        W1.say (); //Mans w2 = (man) s2;    }  }/*** Polymorphism: 1. Overloading and overriding of methods 2. The polymorphism of the object*/Abstract classperson{String name; Abstract  Public voidsay ();  Public voidTalk () {System.out.println ("Hi I ' m a person."); }}classMansextendsperson{ Public voidsay () {System.out.println ("I ' m a man."); }     Public voidTalk () {System.out.println ("Hi I ' m a man."); }}classWomanextendsperson{ Public voidsay () {System.out.println ("I ' m a woman."); }}

/*** instanceof Keyword: Used to determine whether an object is an instance of a class, returns a Boolean value*/ Public classMain { Public Static voidMain (string[] args) {Man M1=NewMan ();        Say (M1); Woman m2=NewWoman ();    Say (m2); }     Public Static voidsay (person p) {P.say (); if(pinstanceofWoman) {Woman W=(Woman) p;        W.getangry (); }    }}classperson{ Public voidsay () {System.out.println ("I ' m a person."); }}classMansextendsperson{ Public voidsay () {System.out.println ("I ' m a man."); }}classWomanextendsperson{ Public voidsay () {System.out.println ("I ' m a woman."); }     Public voidgetangry () {System.out.println ("I ' m angrying ..."); }}/*** Parent Design Law * Can be instanceof keyword, we can easily check the object type, but if a parent class is too many subclasses, this kind of judgment is very cumbersome * How to design the parent class? * 1. The parent class is typically designed as an abstract class or interface, where the interface is preferred, and the abstract class * 2 is considered if the interface is not satisfied. A specific class does not inherit another concrete class as much as possible, and the benefit is that there is no need to check whether the object is a parent class object*/

/*** Abstract Applications (Griffin Palace Recruitment) * Template method pattern: Defines the skeleton of an algorithm in an operation, and delays the implementation of some mutable parts into subclasses. * Template method pattern allows subclasses to redefine some specific steps of the algorithm without altering the structure of an algorithm*/ Public classMain { Public Static voidMain (string[] args) {linjiugong LP=NewShemale ("Fish7");    Lp.action (); }}Abstract classlinjiugong{ Public voidAction () {//Recruitment Activities        if(competition ()) {System.out.println ("Congratulations, Welcome to Linjiugong!"); }Else{System.out.println ("Sorry ..."); }    }     Public Abstract BooleanCompetition ();//Abstract Methods}classShemaleextendslinjiugong{PrivateString name; Shemale (String name) { This. Name =name; }     Public BooleanCompetition () {//How to achieve the gameSystem.out.println ("Game start ....") I ' m gonna win! "); Random R=NewRandom (); returnR.nextboolean (); }}

Java Note 6__ abstract class/interface/polymorphic/instanceof keyword, parent design rule

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.