Java up and down transformation (with concrete examples)

Source: Internet
Author: User

Java up and down transformation (with concrete examples)

Stay up late on the Java up and down transformation of the sample, very easy to understand Oh ~ ~ ~ ~

I. Upward transformation

Package COM.SHEEPMU; Class Animal {public void Eat () {System.out.println ("eating ..." of the parent Class);}} Class Bird extends animal{@Overridepublic void Eat () {System.out.println ("subclass overrides the parent class  eatting ...");} public void Fly () {System.out.println ("subclass new method  flying ...");}} public class Sys{public static void Main (string[] args) {Animal b=new Bird ();//Upward transformation b.eat ();//  b.fly (); B points to the subclass object. But at this time the subclass as the upward cost is lost and the parent class is different from the fly () method sleep (new Male ()), Sleep (new Female ()), and//the incoming parameter is subclass-----!

! }public static void Sleep (Human h)//The parameter of the method is the parent class------!. { h.sleep (); }}

Package COM.SHEEPMU; public class Human {public void sleep () {SYSTEM.OUT.PRINTLN ("Parent Human   Sleep:");}} Class Male extends human{@Overridepublic void sleep () {System.out.println ("man sleep ...");} Class Female extends Human {@Overridepublic void sleep () {System.out.println ("Woman sleep ...");}                        
Output:

Subclass overrides the eatting of the parent class ...
Man sleep.
Woman sleep.


Specific explanations:

1. The realization of upward transformation

Animal b=new Bird ();//Upward transformation
B.eat (); The Eat () method of the subclass is called
B.fly (); Error!!!!! -------B points to a subclass object, but at this point the subclass is lost as the cost of upward transformation and the fly () method differs from the parent class------
2. Why not direct Bird b=new Bird (); B.eat ()?

This does not reflect the object-oriented abstract programming idea AH. Reduces the extensibility of the code.

3. What are the advantages of upward transformation?

Sleep (New Male ());//The number of parameters passed in when calling a method is a subclass
Sleep (new Female ());

public static void Sleep (Human h)//The parameter of the method is the parent class

{

H.sleep ();

}

If the above code is to use the upward transformation, if not the upward transformation. So how many subclasses have to write here how many different ways to sleep ~~~~~~


Two. Downward transition

Package COM.SHEEPMU; Class Fruit  {public void MyName () {System.out.println ("I am the parent  fruit ...");}} Class Apple extends fruit{@Overridepublic void MyName () {System.out.println ("I am a subclass of  Apple ...");} public void Mymore () {System.out.println ("I am your little Apple ~~~~~~");}} public class sys{public static void Main (string[] args) {Fruit a=new Apple ();//Upward transformation a.myname (); Apple Aa= (Apple) A; Downward transformation, compilation and execution are not error (correct) aa.myname ();//the Sub-class Aa.mymore () is called when the transition is down;;  Fruit f=new Fruit ();        Apple aaa= (Apple) F; -Insecure---downward transformation, compile error-free but will execute will be wrong  aaa.myname ();  Aaa.mymore (); }}
Output:

I am a sub-class apple ...
I am a sub-class apple ...
I'm your little Apple ~~~~~~.
Exception in thread "main" java.lang.ClassCastException: Com.sheepmu.Fruit cannot is cast to Com.sheepmu.Apple
At Com.sheepmu.Sys.main (sys.java:30)


Specific explanations:

1. Correct downward transformation

Fruit a=new Apple (); Upward transformation
A.myname ();
Apple Aa= (Apple) A; Down transformation, compile and execute without error (correct)
Aa.myname ();
Aa.mymore ();

A points to the object of a subclass, so an instance of a subclass AA can also point to a AH ~ ~

After the downward transformation because all points to the child class object, so the call is of course the subclass of method ~ ~

2. Unsafe downward transition

Fruit f=new Fruit ();
Apple aaa= (Apple) F; -Insecure---downward transition, compile error-free but execution will go wrong
aaa.myname ();
Aaa.mymore ();

F is the parent class object, the subclass of instance AAA must not point to the parent class F AH ~ ~ ~

3.Java introduction of the concept of generics in order to solve the problem of unsafe downward transformation

4. For a safe type conversion, it is best to use if (A instanceof B) to infer the next ~ ~

Java up and down transformation (with concrete examples)

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.