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

Analysis on the use of Java inheritance

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article introduces the relevant knowledge of "Java inheritance use case Analysis". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Inherit

Inheritance is that the subclass inherits the characteristics and behavior of the parent class, so that the subclass object (instance) has the instance domain and method of the parent class, or the subclass inherits the method from the parent class, so that the subclass has the same behavior of the parent class.

The role of inheritance: through inheritance can quickly create new classes, achieve code reuse, improve the maintainability of the program, save a lot of time to create new classes, improve development efficiency and development quality.

In Java, you can declare that a class inherits from another class through the extends keyword, which generally looks like this:

Class parent class {

. / / member variables, member methods

}

Class subclass extends parent class {

. / / Class body

}

For example:

Class teacher {/ / declares a String name; class as the parent class's member variable name, age int age; void show () {/ / defines the parent class member method, and outputs the member variable System.out.println (name); System.out.println (age) } class Student extends teacher {/ / declare a study class as a subclass and inherit the parent class} public class myfirst {public static void main (String [] args) {System.out.println ("student"); Student student=new Student (); / declare an instance object of the Student class student student.name= "Tom"; / / the subclass calls the member variable name of the parent class and assigns the value student.age=19 / / the subclass calls the member variable age of the parent class and assigns the value student.show (); / / the subclass calls the member method show}} of the parent class

The running result is:

Students

Tom

nineteen

Note:

Child classes cannot selectively inherit parent classes

Java does not support multiple inheritance, but a class can implement multiple interfaces to overcome the shortcomings of single inheritance.

The constructor is not inherited by the subclass, but the constructor of the parent class can be called from the subclass.

The advantages of inheritance

Inherited fields and methods can be used directly like any other field and method

A new field or static method with the same name as the parent class can be declared in the subclass, thus "hiding" the field or method in the parent class

You can declare a new field and method in a subclass that does not exist in the parent class

You can write a new instance method with the same name in the parent class in a subclass, which is called "method override" or "method override".

You can write a subclass constructor in a subclass that calls the parent class constructor, either implicitly or by using the keyword super.

Override and hide parent class methods

The subclass inherits all the members and methods in the parent class, but in some cases, the behavior represented by the method in the subclass is not exactly the same as that represented by the method in the parent class. for example, the method of speaking is defined in the parent language, while the method of speaking in the subclass is different: foreigners speak English and Chinese speak Chinese, so we need to override or hide the method of the parent class.

Override methods in the parent class

When an instance method in a subclass has the same signature (name, number of parameters, and type) and return value as an instance method in its parent class, the method in the subclass "overrides" the method in the parent class. For example:

Class A {public void sayHello () {/ / output English Welcome System.out.println ("Hello,Welcome to Java programs!");} public void sayBye () {System.out.println ("GoodBye,everyone") } class B extends A {public void sayHello () {/ / output Chinese welcome System.out.println ("Hello everyone, welcome to learn Java welcome!") ;}} public class myfirst {public static void main (String [] args) {B b=new B (); / / create an instance object of subclass B, using the default constructor b.sayHello (); / / call the method b.sayBye () overridden in the subclass / / call the method in the parent class}}

The running result is:

Hello, everyone, welcome to learn Java skills!

GoodBye,everyone

Note: the overridden method has the same name, number of parameters, type, and return value as the method it overrides.

Hide methods in the parent class

If a subclass defines a static class method that has the same signature (name, parameter format, and type) and return value as a class method of its parent class, the class method in the subclass is said to "hide" the class method in the parent class.

When calling an overridden method, the version of the call is the method of the subclass

When a hidden method is called, the version of the call depends on whether it is called from the parent class or from the subclass.

Class A {public static void sayHello () {/ / static class method System.out.println ("Hello, this is A's static class method");} public void sayHello2 () {/ / instance method System.out.println ("Hello everyone, this is the instance method in A") }} class B extends A {public static void sayHello () {/ / static class method System.out.println ("Hello, this is B's static class method");} public void sayHello2 () {/ / instance method System.out.println ("Hello everyone, this is B's instance method") }} public class myfirst {public static void main (String [] args) {B b=new B (); / / create an instance object of class B b An astatb; / / implicit object type conversion A.sayHello () / / call the static class method a.sayHello () of class A; / / call the static class method B.sayHello () of an object; / / call the static method a.sayHello2 () of class B / / call the instance method b.sayHello2 () of an object; / / call the instance method An a2=new A () of b object; / / create an instance object of class An a2 a2.sayHello2 () / / call the implementation method of a2 object}}

The running result is:

Hello, everyone. This is the static class method of A.

Hello, everyone. This is the static class method of A.

Hello, everyone. This is the static class method of B.

Hello, everyone. This is the example method of B

Hello, everyone. This is the example method of B

Hello, everyone. This is the example method in A.

As you can see, the hidden method that gets called is the method in the parent class, and the overridden method that gets called is the method in the subclass.

Method overrides and hidden modifiers

Methods that are overridden in a subclass have access permissions greater than but not less than those overridden by them. For example, a protected instance method (protected) in the parent class can be public but not private in the subclass. If a method is a static method in the parent class, it must also be a static method in the subclass; if a method is an instance method in the parent class, it must also be an instance method in the subclass.

Child class accesses private members of parent class

A subclass inherits all public and protected members of its parent class, but cannot inherit the private members of its parent class. So how to access the fields in the parent class in the subclass? we can provide public or protected methods in the parent class to access its private fields, and the subclass uses these methods to access the corresponding fields. For example:

Class A {/ / parent A private int value=10; / / declares a private variable value and assigns a value of 10 public int getvalue () {/ / declare a public member method getvalue, returning value return value;} class B extends A {/ A subclass B} public class myfirst {public static void main (String [] args) {B b=new B () / / create an instance object System.out.println of subclass B ("the subclass accesses the private field value in A through the public interface provided by the parent class:" + b.getvalue ());}}

The running result is:

The subclass accesses the private field value:10 in A through the public interface provided by the parent class

Use the super keyword

Use super to call methods overridden in the parent class and access hidden fields in the parent class

The subclass overrides a method in the parent class to hide the fields in the parent class. If you want to access the overridden methods in the parent class and hide the fields in the parent class in the subclass, you can call the overridden methods in the parent class and access the hidden fields in the parent class by using the keyword super in the subclass. For example:

Package first;class A {public String name= "Zhang Fei"; / / add member variable public void say () {/ / add member method say System.out.println ("I am a member of parent class A method say");} class B extends A {public String name= "Guan Yu" / / hide the parent class public void say () {/ / override method say super.say () with the same name as the field in the parent class; / / use the super keyword to call the method System.out.println in the parent class ("I am a subclass B member method say"); System.out.println ("name name of the parent class:" + super.name) / / use the super keyword to access the variable in the parent class}} public class myfirst {public static void main (String [] args) {B b=new B (); / / create an instance object b.say () of the subclass; / / call the method System.out.println overridden in the subclass ("name name of the subclass:" + b.name) / / call name}} in the subclass

The running result is:

I am the parent class A member method say

I am a subclass B member method say

Name name of the parent class: Zhang Fei

The name name of the subclass: Guan Yu

Call the parameterless constructor / parameterized constructor of the parent class using super

A subclass does not inherit the constructor of its parent class.

When using the parameterless super (), the parameterless constructor of the parent class is called

When you use the super () method with parameters, the parameterized constructor of the parent class is called.

For example:

Class SuperClass {/ / create parent class SuperClass private int n; / / declare a private variable n SuperClass () {/ / parent class parameterless constructor System.out.println ("this is the parent class SuperClass parameterless constructor") } SuperClass (int n) {/ / parent class parameter constructor System.out.println ("this is parent class SuperClass parameter constructor"); this.n = n;}} class SubClass extends SuperClass {/ / SubClass class inherits SuperClass class private int n / / declare a private variable n SubClass () {/ / automatically call the parent class's parameterless constructor System.out.println ("this is a subclass parameterless constructor");} public SubClass (int n) {/ / subclass parameterized constructor super / / call the constructor System.out.println with parameters in the parent class ("this is the subclass constructor with parameters" + n); this.n = n;} public class myfirst {public static void main (String [] args) {SubClass sc1 = new SubClass () / / create a subclass SubClass instance object and call its parameterless constructor SubClass sc2 = new SubClass; / / create a subclass SubClass instance object and call its parameterized constructor}}

The running result is:

This is the parent class SuperClass parameterless constructor

This is a subclass parameterless construction method.

This is the parameter constructor of the parent class SuperClass

This is the subclass parameter constructor 100

Be careful

If you want to initialize the fields in the parent class, you can call the constructor of the parent class through the keyword super in the constructor of the subclass

The call to the constructor of the parent class must be placed on the first line of the subclass constructor

If the parent constructor has no parameters, the parent constructor does not need to be called using the super keyword in the constructor of the subclass. The system will automatically call the parent constructor without parameters.

If the constructor of the parent class takes parameters, the constructor of the parent class must be explicitly called through the super keyword in the constructor of the subclass with the appropriate parameter list

The subclass does not inherit the constructor (constructor or constructor) of the parent class, it just calls (implicit or explicit).

Attachment: the use and understanding of inheritance

1. How is inheritance realized?

Use the extends and implements keywords

Extends inherits all objects.

Implements inherits and implements interfaces

2. In the scene of inheritance, how can you tell the same thing from your father's or your own?

Use the super and this keywords

Super keyword: we can access the members of the parent class through the super keyword, which is used to reference the parent class of the current object.

This keyword: a reference to yourself.

3. What if some methods of the parent class do not want to be inherited by the subclass?

Modify a method or property with the private keyword or final keyword

Modified by private keyword, subclasses cannot be used and inherited

The final keyword is modified and cannot be inherited, but whether it can be used depends on the permission attribute.

This is the end of the content of "Java inheritance usage case Analysis". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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