In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
The editor today takes you to understand how to inherit super in JAVA object-oriented. The knowledge points in this article are introduced in great detail. Friends who feel helpful can follow the editor to browse the content of the article, hoping to help more friends who want to solve this problem to find the answer to the problem. Follow the editor to learn more about "how to inherit super in JAVA object-oriented".
1 inherit 1.1 concept
Inheritance is one of the most prominent features of object-oriented inheritance is to derive new classes from existing classes, which can absorb the data attributes and behavior of existing classes and expand new capabilities. Java inheritance is a technology that builds new classes based on the definition of existing classes. The definition of new classes can add new data or new functions, and can also use the functions of the parent class, but can not selectively inherit the parent class (superclass / base class). This inheritance makes it very easy to reuse the previous code, can greatly shorten the development cycle and reduce the development cost.
1.2 Features
Using the extends keyword to represent inheritance relationships
It is equivalent to a copy of the function of the parent class.
Java only supports single inheritance
Inheritance can be passed on (a grandfather / son / grandson relationship)
The private members of the parent class cannot use the private resources of the parent class because of private restrictions on access.
Inheritance is mostly used for functional modifications, and subclasses can expand their functions while having the functions of the parent class.
Like the relationship of is a.
1.3 activity: inheriting an getting started case
Create package: cn.tedu.oop2
Create a class: TestExtends.java
Package cn.tedu.oop2;/* this class is an entry case for inheritance * / public class TestExtends {public static void main (String [] args) {/ / 5. Create three classes of objects Animal a = new Animal (); Cat c = new Cat (); MiaoMiao m = new MiaoMiao (); / / 6. Use object call methods for testing / * 3. Inheritance is equivalent to that the subclass copies the function of the parent class and is transitive. The grandfather's function is passed to the father, and the father's function is passed to the grandson * / a.eat (); / / the grandfather class uses its own method c.eat (); / / the father class can use the method inherited from the grandfather class m.eat () / / the grandchild class can also use the method inherited from the grandfather class}} / * 1. We use the extends keyword to establish the inheritance relationship between the subclass and the parent class. The format: the subclass extends parent class * / / * 2.Java only supports single inheritance. A subclass can only have one parent class, but a parent class can have multiple subclasses * / / 1. Create a small animal class-grandfather class class Animal {/ / 4. The common way to add a grandfather class is public void eat () {System.out.println ("Animal can eat anything");}} / / 2. Create kittens-dads / * 6. Inheritance is the relationship of is a, such as kitten is a kitten, MiaoMiao is a kitten * inheritance requires that the child class must be a subordinate type of the parent class, the dependency is very strong, strong coupling * / class Cat extends Animal {/ / 7. Define the attributes in the parent class, int a = 10 leading / normal attributes private int b = 100 leading / private attributes} / / 3. Create MiaoMiao class-grandchild class class MiaoMiao extends Cat {/ * 4. Subclasses can have their own unique methods to achieve the expansion of the function, which is better than the blue * / / 8. The method that defines the grandchild class public void studyJava () {System.out.println ("learning Java"); System.out.println (a); / * 5. After the subclass inherits the parent class, all non-private resources of the parent class can be used * Note: because this private resource is modified by private, it does not have access rights * / System.out.println (b); / / No, private resources are restricted access}} 2 super
We can think of super as the object of the parent class: Father super = new Father ()
1. When the member variable of the parent class has the same name as the variable of the subclass, use super to specify the member variable of the parent class
two。 Use super to call the function of the parent constructor on the first line of the subclass constructor
Super ();-calls the no-parameter construction of the parent class
Super (parameter);-calls the constructor of the parameter corresponding to the parent class
Note: in the constructor, the location of the call must be the first line
3 use of inheritance 3.1 exercise: the use of member variables in the inheritance of super
Create package: cn.tedu.oopextends
Create a class: TestExtends1.java
Package cn.tedu.oop2;/* this class is used to test the use of variables in inheritance * / public class ExtendsDemo1 {public static void main (String [] args) {/ / 7. Create an anonymous object of the subclass and call study () new Son (). Study ();}} / / 1. Create the parent class class Father {/ / 3. Create the member variable of the parent class int sum = 1; int count = 2;} / / 2. Create a subclass class Son extends Father {/ / 4. Create a member variable of the subclass int sum = 10; / / 5. Create the common method public void study () {System.out.println ("goog good study, day day up") of the subclass; / / 6. Create a local variable of the subclass int sum = 100; / / 8. Print the local variable sum System.out.println (sum) of the subclass; / / 100 / 9. Print the member variable sum System.out.println (this.sum) of the subclass; / / 10 / / 10. Print the member variable of the parent class sum / * when the member variable of the parent class has the same name as the member variable of the subclass, you can use super to specify the member variable of the parent class * We can think of super as the object of the parent class: Father super = new Father (); * / System.out.println (super.sum); / / 1 System.out.println (count) Exercise: the use of constructors in inheritance of super
Create package: cn.tedu.oop2
Create a class: ExtendsDemo2.java
Package cn.tedu.oop2;/* this class is used to test the use of constructors in inheritance * 1. When a subclass creates an object, the constructor * 2 of the parent class is called by default. The reason is that super () exists by default in the first line of the subclass constructor;-- represents the invocation of the parent class's no-parameter construct * 3. When the parent class has no parameter construction, you can call other parameter-containing constructions of the parent class through super (parameters) * the subclass must call the constructor of a parent class, no matter it is no parameter or contains parameters, you can choose one * 4. Constructors cannot be inherited! For syntax reasons: requires that the name of the constructor must be the class name of this class * the constructor method that cannot have a parent class name in the subclass * / public class ExtendsDemo2 {public static void main (String [] args) {/ / 6.1 create the parent class object / / Father2 f = new Father2 () through the no-parameter construction of the parent class / / 6.2 create the parent class object / / Father2 f2 = new Father2 ("ha ha") through the parameter-containing construction of the parent class; / / 7. Create a subclass object Son2 s = new Son2 ();}} / / 1. Create the parent class class Father2 {/ / 3. Create a no-parameter construction of the parent class / / public Father2 () {/ / System.out.println ("I am the no-parameter construction of the parent class"); / /} / / 4. Create the parameter-containing construction of the parent class public Father2 (String s) {System.out.println ("I am the parameter-containing construction of the parent class" + s);}} / / 2. Create a subclass class Son2 extends Father2 {/ / 5. Create a parameterless construction of the subclass public Son2 () {/ / super (); / / call the parent's parameterless construction super ("Hello ~"); System.out.println ("I am the parameterless construction of the subclass");}} 4 method override Override
After inheritance, the subclass has the function of the parent class
In a subclass, you can add functions specific to the subclass, or you can modify the original functionality of the parent class
Overwrite / overwrite occurs when the signature of a method in a subclass is exactly the same as that of the parent class
Note: private methods of the parent class cannot be overridden
Rewriting requirements: two identical, two small and one big
Identical: the method name parameter list should be exactly the same
Two primary schools:
The return value type of the subclass is less than or equal to the return value type of the parent class (note that it is the inheritance relationship, not the value size).
The subclass throws an exception less than or equal to the parent class method throws an exception
One: the modifier limit of the subclass method is greater than or equal to the modifier permission of the parent class overridden method.
4.1 exercise: use of member methods in inheritance
Create package: cn.tedu.oop2
Create a class: ExtendsDemo3.java
Package cn.tedu.oop2;/* this class is used to test the use of methods in inheritance * / public class ExtendsDemo3 {public static void main (String [] args) {/ / 4. Create an object to test Father f = new Father (); Son s = new Son (); f.eat (); s.eat (); f.play (); s.play ();}} / / 1. Create the parent class class Father {/ / 3. Define the common method in the parent class public void eat () {System.out.println ("Dad loves meat");} public void play () {System.out.println ("Dad loves to fly kites");}} / / 2. Create a subclass class Son extends Father {/ / 5. If the subclass is not satisfied with the method of the parent class, you can rewrite the method of the parent class / * the principle of rewriting: the method name of the subclass method is the same as that of the parameter list and the parent class method * one large: the method modifier permission of the subclass method > = the * two small of the parent class method: the return value type of the subclass method
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.