Java-inheritance, java inheritance

Source: Internet
Author: User

Java-inheritance, java inheritance

  • Inheritance
  • Method Rewriting
  • Inherited initialization sequence

When we create an object, the constructor executed is equivalent to initializing an object.

  

Animal. java

Package com. test; public class Animal {public int age = 2; public String name; public void eat () {System. out. println ("animals can eat");} public Animal () {System. out. println ("animal constructor"); age = 4 ;}}

Dog. java

Package com. test; public class Dog extends Animal {public void eat () {System. out. println ("age" + age + "years old dogs can eat");} public Dog () {System. out. println ("dog constructor ");}}

Initail. java

package com.test;public class Initail {    public static void main(String[] args) {        Dog dog = new Dog();        //dog.age = 1;        dog.eat();    }}

Run Initail. java. The execution result is as follows:

Animal Constructor
Dog Constructor
A 4-year-old dog can eat.

  • Final keywords

Using final keywords for identification has "final" meaning

  • Super keyword

Used inside an object to represent a parent class Object

  • Object Class

The Object class is the parent class of all classes. If a class does not have extend and the keyword is clearly identified to inherit another class, this class inherits the Object class by default.

Methods In the Object class, suitable for all subclasses

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.