In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/03 Report--
An overview of inheritance
When there are the same attributes and behaviors in multiple classes, the content is extracted into a single class, so that multiple classes no longer need to define these attributes and behaviors, just follow that class.
Multiple classes can be called subclasses, and a single class is called a parent class or a superclass.
Subclasses can directly access non-private properties and behaviors in the parent class.
Create inheritance relationships between classes through the extends keyword.
Class SubDemo extends Demo {}
The emergence of inheritance improves the reusability of the code.
The emergence of inheritance creates a relationship between classes and provides the premise of polymorphism.
The characteristics of inheritance
Java only supports single inheritance, not multiple inheritance.
A class can have only one parent class, not multiple parent classes.
Class SubDemo extends Demo {} / / ok
Class SubDemo extends Demo1,Demo2...//error
Java supports multi-tier inheritance (inheritance system)
Class A {}
Class B extends A {}
Class C extends B {}
To define inheritance, you should pay attention to:
Don't inherit just to get a function in another class.
There should be a "is a" relationship between classes. Xx1 is a kind of xx2.
Super keyword
The usage of super and this are similar.
This represents a reference to an object of this class
Super represents the identification of the memory space of the parent class.
When a member of the same name appears in the child parent class, you can use super to distinguish it.
You can use the superstatement when the subclass wants to call the parent class constructor.
Instantiation process of subclass
By default, all constructors in the subclass access the constructor of the hollow parameter of the parent class.
Because the first line of each constructor has a default statement super ()
Final keyword
Final can modify classes, methods, and variables.
Final-decorated classes cannot be inherited.
Final modification methods cannot be overridden.
The variable modified by final is a constant. Can only be assigned once.
Inner classes can only access local variables that are modified by final.
The subclass will have the data in the parent class, so it is important to know how the parent class initializes the data.
When there is no constructor with null arguments in the parent class, the constructor of the subclass must specify the constructor to access through the this or super statement.
Recently, 51CTO+IT 18 hands free big data course, interested friends can go to teacher Xu Peicheng live guest or video specific study.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.