Tutorial on Getting Started with Java (eight) object-oriented (inheritance)

Source: Internet
Author: User

# # #面向对象 (Inheritance) # # #

1. Concept of inheritance: inheritance is a relationship between classes and classes, and is a relationship of "is a"

Parent class---> base class Subclass---> Derived class

Note: Inheritance in Java is single-inheritance

2. Advantages of inheritance: Subclasses have all the properties and methods of the parent class (the private adornment is not valid) to implement code reuse

3. Syntax rules: Class Subclass extends Parent class

Cases:

Package Example;public class Animal {public int age;public String name;public void Eat () {System.out.println ("animals will be hungry without food") ;}} Package Example;public class Dog extends animal{}

4. Method overrides: If the subclass is not satisfied with the method that inherits the parent class, it is possible to override the method inherited by the parent class, and the method of calling the subclass will be limited when calling the method

Syntax rules: Return value type, method name, parameter type and number are the same as the method inherited by the parent class, is called method override

Example: Increasing the Eat method in dog class

Package Example;public class Dog extends Animal{public void eat () {System.out.println ("Age: +age+" \ nthe dog will starve without eating);}}

5. Initialization order in inheritance

1) Initialize the parent class and then initialize the subclass

2) Initialize the properties in the object before performing the initialization in the constructor method

Cases:

Package Example;public class Animal {public int age;public String name;public void Eat () {System.out.println ("Age:" +age+ "\ n animals are hungry without food "); Public Animal () {System.out.println ("This is performed Animal class");}} Package Example;public class Dog extends Animal{public dog () {System.out.println ("This is the execution of the Dog class");}}

Again for example:

We assign the age to age in the animal class, and then assign the age again in the animal method

Package Example;public class Animal {public int ages = 10;public String name;public void Eat () {System.out.println ("Age:" +ag E+ "\ n animals are hungry without food");} Public Animal () {System.out.println ("This is the execution of the Animal class"); age = 20;}}

Now let's look at the output.

6. Use of the final keyword

Using the final keyword to identify the "final" meaning, that is, non-modifiable non-changeable

Final can modify classes, methods, properties, and variables

PS: When a class is decorated, the class cannot be inherited

When the method is decorated, the method is not overwritten (overridden) sequentially

When a property is decorated, the property of the class is not implicitly initialized (the property property of the class must have a value) to be assigned in the constructor method (but only one is selected)

When a variable is modified, the value of the variable can be assigned only once, which becomes a constant

7. Use of the Super keyword: used inside the object, can represent the parent class object

1) Access the properties of the parent class: Super.age

2) methods to access the parent class: Super.eat ()

Cases:

Package Example;public class Dog extends Animal{public dog () {System.out.println ("This is the execution of the Dog Class");} public void Method () {System.out.println (super.age); Super.eat ();}}

3) Super's application

The constructor method of the subclass must be called during the construction of its parent class.

public class Dog extends Animal{public dog () {super ();//This is the  same as writing and not writing, but the words must be put in the first line System.out.println ("This is the Dog class" );} public void Method () {System.out.println (super.age); Super.eat ();}}

If the constructor method of the calling parent class is not displayed in the constructor of the subclass, the system defaults to calling the parent class without a parameter construction method

If the call construction method is displayed, the first line of the method must be constructed on the violet two lei.

If the constructor of the parent class is not explicitly called in the subclass construction method, and the parent class does not have a parameterless constructor method, the compilation error

public class Animal {public int ages = 10;public String name;public void Eat () {System.out.println ("Age: +age+" \ n animals are hungry if they don't eat anything) ;} /*public Animal () {System.out.println ("Execution Animal class construction method");} */public Animal (int age) {this.age = age;}}

8. Object class: Is the parent class of all classes, if a class is not explicitly identified with the extends keyword to inherit another class, then this class inherits the object class by default.

Method in the object class, suitable for all subclasses.

1) toString () method

The hash code (object address string) of the object returned when defining a ToString () method inside the object class

You can represent the properties of an object by overriding the ToString () method.

2) equals () method

The comparison is whether the reference to the object points to the same memory address.

In general, compare two objects when comparing his values to be consistent, so rewrite them.

At this point, if you assign the same value to age

This shows that the output is false in this we establish the Equals method

Create the Equals method: Source--Generate hashcode () and Equals () ...

@Overridepublic boolean equals (Object obj) {if (this = = obj) return true;if (obj = = null) return False;if (GetClass ()! = obj . GetClass ())  //Determine whether the type of two objects is the same as return false;dog other = (Dog) obj;if (age! = Other.age) return False;return true;}

This turns out to be true.

# # # #END # #

Related articles:

Introduction to Java Tutorial (vi) Object-oriented (encapsulation)

Introduction to Java Tutorial (vii) Object-oriented (package 2)

Related Article

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.