Java object-oriented-inheritance

Source: Internet
Author: User

The inheritance of Java classes is mainly accomplished by extends keywords. Once inherited, the subclass gets the member property of the parent class and the member method. Basic syntax:

Class Name extends parent class name

{

}

Example: Fruti.java

public class Fruit

{

public void GetColor (String color)

{

SYSTEM.OUT.PRINTLN ("The color of the fruit is:" +color);

}

}

Pear.java

public class Pear extends Fruit

{

}

Note: Only single inheritance is allowed in Java, and multiple inheritance is not allowed, that is, a subclass can have only one parent class, but in Java it allows multiple layers of inheritance such as: Pear can inherit fruit, Then p can inherit the Pearjavasuper keyword super The main function is to complete the subclass call the content in the parent class, that is, call the property or method in the parent class, here to distinguish the This keyword basic structure:

Super. Attribute names (all refer to the parent class)

Super. Method name (all refers to the parent class)

For example:

Fruit.java

public class Fruit

{

String name;

public void GetColor (String color)

{

SYSTEM.OUT.PRINTLN ("The color of the fruit is:" +color);

}

}

Pear.java

public class Pear extends Fruit

{

Public Pear ()

{

Super.name= "Crazy software Java Training"; Call the Name property of the parent class and assign a value.

}

}

Java class rewrite (overwrite) Everyone should know now, once the subclass inherits the parent class, then even the method of the corresponding inheritance, but because each subclass also has its own personality, where there is a Java class to rewrite when a subclass inherits a parent class, When a method in a subclass is exactly the same as the name of a method in the parent class, the number of arguments, and the type, the method in the subclass is called to replicate the methods in the parent class, for example:

Fruit.java

public class Fruit

{

String name;

public void GetColor (String color)

{

SYSTEM.OUT.PRINTLN ("The color of the fruit is:" +color);

}

}

Pear.java

public class Pear extends Fruit

{

Public Pear ()

{

Super.name= "Crazy software Java Training"; Call the Name property of the parent class and assign a value.

}

Overriding the parent class method

public void GetColor (String color)

{

System.out.println ("Here is the method in the subclass, I rewrite its contents");

}

}

Java class overloading compared to the above, the Java class overrides, overloading refers to the method name consistent, but the parameter or return value is not the same. For example:

Fruit.java

public class Fruit

{

String name;

public void GetColor (String color)

{

SYSTEM.OUT.PRINTLN ("The color of the fruit is:" +color);

}

}

Pear.java

public class Pear extends Fruit

{

Public Pear ()

{

Super.name= "Guangzhou java Training"; Call the Name property of the parent class and assign a value.

}

Overloading of the parent class method

public void GetColor (String color,stringname)

{

System.out.println ("Here is a method in a subclass, I overload its contents");

}

}

Java object-oriented-inheritance

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.