Java Basic concept finishing (iii)

Source: Internet
Author: User
Tags instance method stub

1, the object of the upper transformation

You cannot call and use new members and variables of the subclass object, and you cannot call the new method of the child class.

On a transformed object, you can access the subclass inherited or hidden member variables, or you can invoke subclass inheritance or instance methods that are overridden by subclasses. Therefore, if the subclass overrides an instance method of the parent class, the instance method of the subclass rewrite must be called when the object's top-transition object calls the instance method.

2. Abstract methods

For the abstract method, only declarations are allowed, implementations are not allowed, and final and abstract are not allowed to modify a method or a class at the same time, the abstract method can only be an instance method, and the abstract class may have an abstract method. The abstract method cannot be used in a non-abstract class.

3, object-oriented programming, often used to the abstract class, the abstract class only care about the operation, not related to the specific implementation of the details of the problem,

Package com. Example1;

public class Exmaple5_12 {

public static void Main (string[] args) {
TODO auto-generated Method Stub
Mobiletelephone telephone = new Mobiletelephone ();
Sim sim = new Simofchinamobile ();
Sim.setnumber ("2454545");
Telephone.usersim (SIM);
Telephone.showmessage ();
SIM = new Simofchinaunicon ();
Sim.setnumber ("454664646");
Telephone.usersim (SIM);
Telephone.showmessage ();

}

}

Abstract class SIM {
public abstract void Setnumber (String n);

Public abstract String Givenumber ();

Public abstract String givecorpname ();
}

Class Mobiletelephone {
SIM Card;

public void Usersim (SIM card) {
This.card = Card;
}

public void ShowMessage () {
System.out.println ("Use card name is" + card.givecorpname ());
System.out.println ("SIm ' number is" + card.givenumber ());
}
}

Class Simofchinamobile extends SIM {
String number;

@Override
public void Setnumber (String N) {
TODO auto-generated Method Stub
Number = n;

}

@Override
Public String Givenumber () {
TODO auto-generated Method Stub
return number;
}

@Override
Public String Givecorpname () {
TODO auto-generated Method Stub
Return "Unicom card";
}

}

Class Simofchinaunicon extends SIM {
String number;

@Override
public void Setnumber (String N) {
TODO auto-generated Method Stub
Number = n;

}

@Override
Public String Givenumber () {
TODO auto-generated Method Stub
return number;
}

@Override
Public String Givecorpname () {
TODO auto-generated Method Stub
Return "China Mobile";
}

}

4, inheritance is an existing class to create a new class mechanism, the use of inheritance, you can create a common class of properties, according to the general class to create a special attribute of the new class.

Java Basic concept finishing (iii)

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.