Java Object-oriented

Source: Internet
Author: User

    • Java inheritance

Java uses the extends keyword to declare that a class inherits from another class.

class classextends  parent class {} 

Example:

Public parent class
Animal {
String name;
ID;
Animal MyName myID) {
    name MyName;
    ID myID;
}
Introduction (){
System.out.println ("hello,everyone! My name is "+ ID +", "+ Name +". ");
}
}

// sub-class  Public class extends Animal {     publicint  myid) {         super(MyName, myID);}     }  Publicclassextends  Animal {     publicint  myID) {         super(MyName, myID);     }}

Inherited attributes:

    • Subclasses have properties that are not private to the parent class, methods.

    • Subclasses can have their own properties and methods, that is, subclasses can extend the parent class.

    • Subclasses can implement methods of the parent class in their own way.

    • Java inheritance is a single inheritance, but can be multiple inheritance, single inheritance is a subclass can inherit only one parent class, multiple inheritance is, for example, Class A inherits Class B, Class B inherits Class C, so the Class C is the parent class of Class B, and Class B is the parent class of Class A, which is a feature that differs from C + + inheritance.

    • Increases the coupling between classes (the disadvantage of inheritance, high coupling will cause the relationship between the code).

Inheritance keywords:

    • Extends keyword ( single inheritance, a subclass can have only one parent )
    • Implements keywords (using the Implements keyword can be disguised to make Java have multiple inheritance, using the scope of the class to inherit the interface, you can inherit multiple interfaces (interfaces and interfaces separated by commas).
// Implements keyword example  Public Interface A {    publicvoid  eat ();      Public void  public interface  B {    publicvoid    Public class Implements A, b {}

    • Super keyword (we can use the Super keyword to implement access to a parent class member to refer to the parent class of the current object.) )
    • The This keyword, which points to its own reference. )
class Superclass {    int i =classextends  superclass {      int i =100;      Public void ShowMessage () {        System.out.printf (Superthis. i);    }}

Output:

Super this. i = 100

    • Final keyword

The final keyword declares that a class can define a class as not inheritable, that is, a final class, or for a decorated method that cannot be overridden by a quilt class:

Declaring a class:

Final  class Name {// class Body}

Declaration method:

Modifier (Public,private,voidfinal return value type method name () {// method Body}
    • Constructors

A subclass cannot inherit the constructor (constructor or constructor) of the parent class, but the constructor of the parent class has parameters, and the constructor of the parent class must be explicitly called through the Super keyword in the constructor of the child class with the appropriate argument list.

If the parent class has a parameterless constructor, it is not necessary to call the parent class constructor with super in the constructor of the subclass, and if the Super keyword is not used, the system automatically calls the parent class's parameterless constructor.

Java Object-oriented

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.