The method of the parent class as the parameter list in the Java polymorphism

Source: Internet
Author: User

Pet pet=new Dog (); Pet is the parent class, and dog is a subclass. This method is the parent class that does the type, and the subclass is instantiated.

Related examples:

Examples of employees returning home;

1, first to do an abstract parent class, including the idea of walking.

Package Cn.day1;public Abstract class Traffic {public abstract void run ();

  

2, in creating a class that contains the idea of going home.

Package Cn.day1;public class Employee {public void gohome (traffic tool) {Tool.run ();}}

  

3, create two subclasses, inherit the parent class. Subclass contains two tools to go home.

Package Cn.day1;public class Tube extends traffic {@Overridepublic void run () {System.out.println ("Tube Run");}}

  

Package Cn.day1;public class Car extends traffic {@Overridepublic void run () {System.out.println ("Car Run");}}

  

4, call

Package Cn.day1;public class MyText {public static void main (string[] args) {//Staff employee Ele=new employee ();//Transport Traffi C tra=new Car (); Ele.gohome (TRA);}}

I learn not solid, later in-depth study I will optimize the content of this chapter!!!

The method of the parent class as the parameter list in the Java polymorphism

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.