Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is Java inheritance

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "what is Java inheritance". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn "what is Java inheritance"?

I. the concept of inheritance

1. What is inheritance?

In real life, inheritance means that children inherit the property of their parents. In a program, when one class A can get the definition of all the non-private data and operations in another class B as part or all of its own components, we call it an inheritance relationship between the two classes.

two。 For example, cats, dogs, rabbits and pigs all belong to animals, and cats, dogs, rabbits and pigs all inherit animals in the program. Persian cats and English short-haired cats inherit cats, and these animals form an inheritance system. Inherit in the program to create a new class based on the existing class, and create a new class called the subclass, which is called the parent class or superclass, and the subclass will have all the inheritable properties and methods of the parent class.

3. Implementation of inheritance

① determines the parent class.

② defines subclasses.

[class modifier] class subclass name extends parent class name

Subclasses can inherit all non-private properties and methods of the parent class.

③ implements the functions of subclasses.

4. How the class inherits the parent class case 1

/ / define Fruit fruit class class Fruit {public double weight;// weight attribute / / info () method public void info () {System.out.println ("I am a fruit, weight is" + weight + "g!") ;} public class Apple extends Fruit {public static void main (String [] args) {Apple a = new Apple (); / / create an object a.weight = 87ash / assign a.info () to the weight attribute; / / call the info method}}

The result of the output is:

I am a fruit, weighing 87.0g!

From the above code, we can see that a Fruit class is defined, and the weight property and info method are defined in the Fruit class. The Apple class inherits the Fruit class, creates an object instantiation, assigns a value to the weight property, and calls the info method.

5. How the class inherits the parent class case 2

Class Animal {public String name;// name attribute public int age;// Age attribute / / Animal call method void shout () {System.out.print ("Meow Meow ~") }} / define Cat inherit Animal class class Cat extends Animal {/ / display name and age method public void show () {System.out.print ("I am" + name+ "," + "age+" year ");}} public class p20 {public static void main (String [] args) {/ / TODO Auto-generated method stub Cat c=new Cat () / / create an instance object of the Cat class c.name = "British short-haired cat"; / / assign the name attribute of the Cat class c.ageaccoun2ActionBack / assign the age attribute of the Cat class to c.show (); / / call the show () method c.shout () of the Cat class; / / call the shout () method}} of the Cat class

The result of the output is:

I am an English short-haired cat. I am 2 years old Meow Meow.

In the above code, you first define an Animal class. The Cat class inherits the Animal class using extends, so that the Cat class is a subclass of the Animal class and Animal is a parent class. We find that there are no name, age properties, and shout () methods defined in the subclass, and you can access their non-private properties and methods. Shows that a subclass can have all the non-private properties and methods of the parent class.

II. Problems needing attention in inheritance

1. Single inheritance means that any class has only a single parent class.

For example:

Class A1 {void show () {System.out.println ("A1");}} class A2 {void show () {System.out.println ("A1");} class B extends A1 System.out.println A2 {public void show () {System.out.println ("bbb");}}

It is illegal in the above code, any class only supports single inheritance, not multiple inheritance. That is, class B cannot inherit A1 and A2 at the same time. If B b=new B (), which parent method is called by b.show ()? No answer can be given, so there can be no multiple inheritance.

two。 Multiple inheritance means that a class can have more than one parent class, and its static data properties and operations inherit from all of these parent classes.

3. Multiple classes can inherit a parent class.

For example:

Class A {} class B extends A {} class C extends A {}

It is legal in the above code that both class B and class C can inherit all the non-private properties and methods of class A.

4. The parent class of one class can inherit another parent class, which is called multi-tier inheritance.

For example:

Class A {} class B extends A {} class C extends B {}

In the above code, multi-layer inheritance class B inherits class A, class B is a subclass of class A, class C inherits class B, class C is a subclass of class B, and class C is also a subclass of class A. While one class is the parent of one class, it is also the parent of another. For example, Class B is a subclass of Class An and a parent of Class C.

At this point, I believe that everyone has a deeper understanding of "what is Java inheritance", might as well come to the actual operation of it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report