Java Learning Note 3--inheritance

Source: Internet
Author: User

Inherited

    1. Benefits of Inheritance: Subclasses have all the properties and methods of the parent class, the peivate adornment is not valid; Implement code reuse

class  Subclass  extends  Parent class such as: class dog extends animal{               } Parent class: Package com.imooc;public class animal { public int age; public  string name; public void eat () {  system.out.println ("Animals have food");  }} Subclass: package com.imooc;public class dog extends animal {} Test Class:package  Com.imooc;public class initail { public static void main (String[] args )  {  // TODO Auto-generated method stub         dog dog = new dog ();         dog.age  = 10;        dog.name =  "Xiaohui";         dog.eat ();  }}

Rewrite

    1. Method overrides: Subclasses can override methods inherited by the parent class when the calling method takes precedence over methods of calling subclasses

    2. Syntax rules: A: Return value type B: Method name C: The parameter type and number are the same as the method inherited by the parent class, only called method overrides

Rewrite Dog class: Package Com.imooc;public class Dog extends Animal {public void Eat () {System.out.println ("Age" +age+ "dog Can Eat");}}

3. Initialization order of inheritance: 1) initializing the parent class and then initializing the subclass 2) executes the properties in the initialization object before executing the initialization in the constructor method

Final use in Java

    1. Final, non-modifiable meaning; Final can modify classes, methods, properties, and variables. Final decorated class, the class is not allowed to be inherited; The method cannot be overridden, the property is not implicitly initialized (the class initialization property must have a value), or it can be assigned in a constructor method (but only one is selected). Final modified variable, the value of the variable can only be assigned once, which becomes a constant.

The use of super in Java

    1. Super: Used inside the object, can represent the parent class object

    2. Access the properties of the parent class object: Super.age; Methods to access the parent class object: Super.eat ()

    3. Super Application: 1) The construction of the subclass must call the constructor of its parent class, the system calls the parent class without a parameter constructor, implicitly calls Super (), 2) if the subclass constructor method does not explicitly call the constructor of the parent class, and the parent class does not have a parameterless constructor method, the compilation error occurs. You need to add super () to the first line in the subclass construction method.

The object class in Java

The 1.Object class is the parent class for all classes, and if a class does not inherit another class using extends, the class inherits the object class by default. The methods in the object class are suitable for all subclasses.

1) toString (): Returns the hash code for the object, which can be represented by overriding the ToString () method--eclipse has the method of automatically helping us to rewrite toString (),eclipse->source-> Generate toString ()

2) equals (): Compares whether the reference to the object is pointing to the same piece of memory as dog dog = new Dog ()

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

eclipse->source-> Generate hashcode () and Equals ()

When overridden, compares the values of two objects:

public boolean equals (Object obj) {if (this = = obj) return true;  if (obj = = null) return false;  if (getclass () = Obj.getclass ()) return false;  Dog other = (dog) obj;  if (age! = Other.age) return false; return true; }


Java Learning Note 3--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.