Java Tutorial Java Inheritance Examples of detailed _java

Source: Internet
Author: User
Tags control characters int size

What is inheritance (extends)?

Inheritance is: The newly defined class is the phenomenon of getting properties and methods from an existing class. This existing class is called the parent class, and the class that gets the properties and methods from this parent class is called a subclass.

Extendsdemo1.java

Copy Code code as follows:

* * What is the inheritance * *
public class ExtendsDemo1 {
public static void Main (string[] args) {
Truck T = new truck ();
t.size = 100; It is not advisable to write this. It is best to initialize a member variable with a construction method, or to provide a set (), Get () interface.
An instance of the Wagon class T from the car class inherits the size, color attribute.
And a van has one more crate than a car.
}
}

Class Car {//Auto
int size; Car size
String color; Color
}

Class Truck extends Car {//truck
String Packingbox; IBCs
}

Benefits of Inheritance

1. Improve the reusability of the Code

2. Make a relationship between classes and classes, creating conditions for polymorphism.

Inheritance (extends) format

Copy Code code as follows:

Class Subclass extends Superclass {
Execute the statement;
}


Super keyword

1. The Super keyword and this use the same

2. This represents the reference to this class, and super represents the parent class reference

3. When a subclass and parent class have a member with the same name, you can distinguish between super and this

Superdemo.java

Copy Code code as follows:

/* Use of super
* Output Result:
* Super.i = THIS.I = 20
*/
public class Superdemo {
public static void Main (string[] args) {
New Subclass (). ShowMessage ();
}
}

Class Superclass {
int i = 10;
}

Class Subclass extends Superclass {
int i = 20;

public void ShowMessage () {
System.out.printf ("super.i =%d, this.i =%d\n", super.i, this.i);
}
}

Overrides the method inherited from the parent class (Override)

1. An override (Override) action occurs when a subclass has the same method (return value, function name, formal parameter) that appears in the parent class.

Overridedemo1.java

Copy Code code as follows:

/* How to override a method inherited from a parent class (Override)
* Output Result:
* Superclass:i am good~
* Subclass:i am excellent~~~
*/
public class OverrideDemo1 {
public static void Main (string[] args) {
Subclass SC = new subclass ();
Sc.speak ();
}
}

Class Superclass {
public void Speak () {
System.out.println ("superclass:i am good~");
}
}

Class Subclass extends Superclass {
@Override//@Override means that the following methods appear with an overriding operation, which is checked by the compiler, and an error is made if the overridden condition is not reached. Increased code security to some extent
and robustness
public void Speak () {
Super.speak ();
System.out.println ("subclass:i am excellent~~~");
}
}

2. When overriding (Override) operations on a parent class method, subclasses do not allow methods that have the same parameters as the parent class but different return types.

Overridedemo2.java

Copy Code code as follows:

/* A method that does not allow the same parameter with the same name as the parent class but different return types is not allowed in the/* subclass
* Error message:
* OVERRIDEDEMO.JAVA:20:ERROR:F () in subclass cannot override F () in superclass
* public int F () {
*                 ^
* return type int is isn't compatible with void
* Overridedemo.java:19:error:method does not override or implement a to from a supertype
* @Override
*                          ^
* 2 Errors
*/
public class OverrideDemo2 {
public static void Main (string[] args) {

}
}

Class Superclass {
public void F () {

}
}

Class Subclass extends Superclass {
@Override
public int F () {
return 1;
}
}

3. Subclasses override the method of the parent class to be greater than or equal to the overridden method in the parent class

Overridedemo3.java

Copy Code code as follows:

/* Subclass overrides the access permission of the method of the parent class to be greater than or equal to the access rights of the parent class method
* Error message:
* OVERRIDEDEMO.JAVA:18:ERROR:F () in subclass cannot override F () in superclass
* protected void F () {
*                     ^
* Attempting to assign weaker access privileges; is public
* 1 Error
*/

public class OverrideDemo3 {
public static void Main (string[] args) {

}
}

Class Superclass {
public void F () {
}
}

Class Subclass extends Superclass {
@Override
protected void F () {//change the access right here to public

}
}

Access control characters

Access control characters are: public, protected, default, private

Some need a little bit of package knowledge, so wait until you talk about the bag.

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.