Java Foundation---Object-oriented (4) [polymorphic]

Source: Internet
Author: User


Polymorphic

1. Overview of polymorphism
The same object (transaction), which manifests itself in different states at different times.
Examples of Use:
Pig: It's an animal, it belongs to a mammal.

2. Conditions for polymorphism
(1). To have an inherited relationship.
(2). To have the override of the method.
In fact, there can be no, but if there is no method of rewriting, then this one is meaningless.
Animal c=new Cat ();
C.show ()
Animal D=new Dog ();
D.show ();
(3). To have a reference to the parent class point to the Subclass object
Parent f=new Child ();


3. Feature of member access in polymorphic
(1). Member variables
Compile look left, run look right
(2). Construction method
When you create a subclass object, you access the constructor of the parent class and initialize the data for the parent class.
(3). Member method (mainly due to methods overridden by method that causes subclasses to override methods of the parent class)
Compile look left, run look right
(4) static method
Compile look left, run look left

Code:
Parent Class Code:
public class Father {
public int num=10;

public void Show () {
System.out.println ("show father");
}

public static void function () {
System.out.println ("function father");
}
}

//Subclass code and test class
public class Student extends father{
public int num=200;
public int num2=400;

public void Show () {
System.out.println ("Student Show");
}

public void Method () {
System.out.println ("method student");
}

public static void function () {
System.out.println ("function student");
}


//polymorphic test
@SuppressWarnings ("static-access")
public static void Main (string[] args) {
Father ft= New Student ();
//access to the member variable of the parent class run look left
System.out.println (ft.num);
Can not find the symbol, compile the error, compile to see the right, looking for the parent class inside the variable, no error will be
//system.out.println (ft.num2);
Ft.show ();
//Cannot find the symbol, error
//ft.method ();
Ft.function ();

}
}
//Output results

Student show
function Father

4. Polymorphism Benefits
(1). Improved code maintainability (inheritance Guarantee)
(2). Improved code extensibility (guaranteed by polymorphism)
We write an animal class, and then use the cat and dog case to achieve the function we need, because these functions are similar (eating, sleeping, playing) And so on, so we unified in the tool class, we can work on all the animals to use,
and because at the time of the invocation of each object is not the same, so the call will be different, which can guarantee the extensibility of the code.

(3). Code:
Animal class
public class Animal {
Eat
public void Eat () {
System.out.println ("Animal Eat ()");
}
Go to bed
public void sleep () {
System.out.println ("Animal Sleep");
}
}
Dog class
public class Dog extends animal{
public void Eat () {
System.out.println ("Dog eats meat");
}
public void sleep () {
System.out.println ("The dog curled up to sleep");
}
}
Cat class
public class Cat extends animal{
public void Eat () {
System.out.println ("Cat eats fish");
}
public void sleep () {
System.out.println ("The cat sleeps on his stomach");
}
}
Tool class
Tool classes for animal operations
public class Animaltool {
Private Animaltool () {}

Call the function of the cat
public static void Usecat (Cat c) {
C.eat ();
C.sleep ();
}
Call dog function
public static void Usedog (dog d) {
D.eat ();
D.sleep ();
}
}


5. Transition issues in Polymorphism

(1). The drawbacks of polymorphism
Polymorphism is not able to use the unique features of subclasses. If you want to use features that are unique to subclasses, then you need to use transformations at this point in time.

(2). There are two major transformations: downward transformation and upward transformation
Down transformation: (subclass becomes parent)
Format: Fu f=new Zi ();
Transition up: (parent class becomes subclass)
Format: Zi z= (Zi) F

Code:
Parent Class Code
public class Father {
public void Show () {
System.out.println ("show father");
}
}
Sub-class code and test
public class Student extends father{

public void Show () {
System.out.println ("Student Show");
}

public void Method () {
System.out.println ("method student");
}
Polymorphic Testing
public static void Main (string[] args) {
Father f=new Student ();
F.show ();
F.method (); error
Student st= (Student) F;
St.show ();
St.method ();

}
}
Output results
Student Show
Student Show
Method Student


(3). Understanding of polymorphic problems (Confucius pretending to be father)
Class Confucius Father {
public int age=40;

public void Teach () {
System.out.println ("Confucius Father will be the I Ching");
}
}

Class Confucius extends Confucius Father {
public int age=20;

public void Teach () {
System.out.println ("Confucius will speak the Analects");
}

public void PlayGame () {
System.out.println ("Play League of Legends");
}

}

Recently, many people learn the I Ching, a lot of people ask Confucius Father to lecture, this day Confucius father was asked to go, but still someone to please,
At the same time, the price is good, left Confucius at home, Confucius A thought, I can not consider to go? Then he puts on his father's clothes, sticks his beard, and starts pretending to be his father, and then he's gone.
This is essentially a process of upward transformation.
Confucius Father K-Father =new Confucius (); Look outside is the Confucius father, the essence is Confucius
System.out.println (K-Father age);//40 because it is Confucius in the father, if not like, no one thinks he is his father, will not tell him to lecture
K. teach (); Confucius only speaks the Analects, and this process is the process of invoking member variables and methods in polymorphism.
K. PlayGame (); Although this one is the son itself, but this one is to pretend to be a father, so is not able to play games.


We're done, come home from work.
Take off Dad's disguise, and then change back to yourself.
In fact, this time is a downward transformation of a process, this time is to restore their own appearance
Confucius K= (Confucius) K-dad;
System.out.println (k.age);//20
K.teach ();//Speaking the Analects
K.playgame ();//play League of Legends

6. Multi-State exercise


(1). Different food culture in different places
//person class
public class person {
public void eat () {
System.out.println (' Person eat ");
}
}
//southerners
public class Southperson extends person{
public void Eat () {
System.out.println ("Stir fry, eat rice" );
}

public void bus () {
SYSTEM.OUT.PRINTLN ("Doing Business");
}
}
Northern people
public class Northperson extends person{
public void Eat () {
System.out.println ("stewed vegetables, eat steamed buns");
}

public void Yanjiu () {
SYSTEM.OUT.PRINTLN ("research");
}
}
Test class
public class Duotaitest {
public static void Main (string[] args) {
Southern People
Person P=new person ();
P.eat ();
System.out.println ("-------------");
Southperson sp=new Southperson ();
Sp.bus ();
Sp.eat ();
System.out.println ("-------------");
Northern people
P=new Northperson ();
P.eat ();
System.out.println ("-------------");
Northperson np= (Northperson) p;
Np.eat ();
Np.yanjiu ();
}
}
Test results
Person Eat
-------------
Business
Stir fry and eat rice
-------------
Stewed vegetables and steamed buns
-------------
Stewed vegetables and steamed buns
Study

(2). Interview questions: Read the program to write the results
Class a{
public void Show () {
Show2 ();
}

public void Show2 () {
System.out.println ("I");
}
}

Class B extends a{
public void Show2 () {
System.out.println ("Love");
}
}


Class C extends b{

public void Show () {
Show2 ();
}

public void Show2 () {
System.out.println ("You");
}

}

public class duotaitest{
public static void Main (string[] args) {
A a=new B ();
A.show ();

B b=new C ();
B.show ()

}

}

Output: Love You

Java Foundation---Object-oriented (4) [polymorphic]

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.