Characteristics of objects that use inheritance when the parent and child classes

Source: Internet
Author: User

Class Pclass
{
void Drew ()
{
System.out.println ("Pclass class: Drew Method! ");
}
}
Class Sclass extends Pclass
{
void Drew ()
{
System.out.println ("Sclass class: Drew Method! ");
}
void Newdrew ()
{
System.out.println ("Sclass class: Newdrew Method! ");
}
public class Classinherit {
public static void Main (String[]args)
{
Pclass obj1=new Pclass (); Create an object based on a parent class
Sclass obj2=new Sclass (); Creating objects based on subclasses
Obj1. Drew ();
Obj2. Drew ();
Obj2.newdrew ();
Obj1=obj2; The object of the subclass is assigned to the parent class
Obj1.  Drew (); Method that is already a subclass of Drew
}
}
Characteristics:

1. One is to assign to the parent class only the object of the class, and not to assign the object of the parent class to the child class.

2. Even if an object of a subclass is assigned to a parent class, the parent class object can only invoke methods and member variables in the parent class.

Characteristics of objects that use inheritance when the parent and child classes

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.