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 are the principles and characteristics of Java object-oriented inheritance

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

Share

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

This article mainly explains "what are the principles and characteristics of Java object-oriented inheritance". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what are the principles and characteristics of Java object-oriented inheritance".

Catalogue

I. Preface

II. Inheritance

What is inheritance?

Advantages and disadvantages of inheritance

Inherited usage scenarios?

Inherited format:

Characteristics of inheritance:

The concept of rewriting:

Super keyword

Comparison between super and this

I. Preface

Earlier we also talked about the concept and use of inheritance that we now know about encapsulation.

Second, what is inheritance?

Inheritance is also quite common in showing life, such as inheriting property, and it is also used similarly in our java study.

The inheritor is called a subclass or a derived class, the inheritor is called a parent class, a base class or a superclass, and the objec class is the parent class of all classes.

(later introduction)

Advantages and disadvantages of inheritance

Benefit: it improves the maintainability of the code (multiple code needs to be modified, only one of them needs to be fixed).

Improved code reusability (multiple identical members can be used in the same class)

Disadvantages: the disadvantage of inheritance is that the coupling between the code is high, and the modification of the parent class and subclass will also change.

Inherited usage scenarios?

Two are subordinate, such as cats and animals, students and people, etc.

The key word of inheritance is extends.

Inherited format:

Public class subclass name extends parent class name {}

For example:

Public class Cat extends Animal {} / / Cats inherit the characteristics of animal inheritance:

Subclasses can have non-private methods and member variables of the parent class, or they can override the non-private (private-decorated) methods of the parent class. The methods of all subclasses access the parameterless construction of the parent class by default

The concept of rewriting:

Rewriting is a subclass that restates the non-private methods in the parent class. The general characteristics of rewriting are the same method name, the same format, the same return type, and different method bodies.

Such as:

In the fu class:

Public class fu {public void play () {System.out.println ("fu likes playing badminton");}}

In the zi class:

Public class zi extends fu {public void play () {/ / a pair of parent class methods rewrite System.out.println ("zi likes playing basketball");}}

It can also be written that access requires zi class to be greater than or equal to fu class

Decorating in the fu class is the default:

Public class fu {void eat () {System.out.println ("eat method in fu");}}

In the zi class:

Public class Zi extends Fu {@ Override// check method overrides correctness public void eat () {System.out.println ("eat method in zi");}}

Overrides in the zi class:

It doesn't matter whether public class Zi extends Fu {/ @ Override is written or not. This only plays an auxiliary role: void eat () {System.out.println ("eat method in zi");}}

Zi can also:

Public class Zi extends Fu {@ Override protected void eat () {System.out.println ("eat method in zi");}}

Decorate the access relationship: public > protected > default (not written) > private

When the permission modifier of the parent class is the default (that is, the modifier is not written), the modifier methods for subclass overrides can be default and

Default before, and so on. Note: when the permission modifier of the parent class is private, that subclass is not private

Before. Subclasses cannot override such methods when the parent class is modified by private.

Super keyword

When we want to use a member variable in the parent class when the member variable in the subclass has the same name as the member variable in the parent class

Or when we override a method in the parent class and we still want to call the method in the parent class. At this point, we are going to use super

Keyword to call a member in the parent class.

Comparison between super and this

The principle of super and this is similar. This solves the problem of local variable pair when local variable and member variable have the same name.

The override super of the member is to solve the override of the subclass to the parent class.

When a local variable, a member variable, or a variable in the parent class has the same name, the local variable is accessed. Can be changed with this and super.

Public class fu {public int age=40; public fu () {} / / Parametric Construction public fu (int age) {/ / Parametric Construction this.age=age;} public void eat () {System.out.println ("eat method in fu");} public class Zi extends Fu {public int age=18;public Zi () {} public Zi (int age) {this.age=age;} public void eat () {super.eat () System.out.println ("eat method in zi");} public void show () {int age=1; System.out.println (age); / / 1 value of local variable System.out.println (this.age); / / 18 value of subclass member System.out.println (super.age); / / 40 value of parent class} public class Demo {public static void main (String [] args) {zi z=new zi (); z.eat () Z.show ();}} Thank you for your reading, the above is the content of "what are the principles and characteristics of Java object-oriented inheritance". After the study of this article, I believe you have a deeper understanding of what the principles and characteristics of Java object-oriented inheritance are, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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