Implementation of Java Inheritance

Source: Internet
Author: User

First of all, the inheritance: integration is a polymorphic one, so that inheritors also have some characteristics of the successor.

Let's take a look at an example: Let's create an animal class that represents the animal species.

public class Animal {

The weight of the animal

private int weight;

public int getweight () {
return weight;

public void setweight (int weight) {
This.weight = weight;

Then we create a subclass of the dog class, inheriting the animal class, and the dog class has the properties and methods of the parent class animal.

We added a method to describe dog barking in the dog class.

public class Dog extends Animal {

Bark is the way the dog barks.

public void Bark () {
System.out.println ("wang~~~~~~wang~~~");


Finally, let's test if the dog really has some of the methods and properties of the parent class animal.

public class Mydog {
public static void Main (string[] args) {
Dog Mydog=new Dog ();
Mydog.setweight (200);
System.out.println ("My Dog ' s Weight is" +mydog.getweight ());
Mydog.bark ();


Running the program, the console will get the following information, stating that the subclass dog perfectly inherits the properties and methods of the parent class animal.

Implementation of Java Inheritance

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: 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.