"Inheritance" in Java is a concept in object-oriented software technology. If a class A inherits from another class B, it is called a subclass of B, and B is referred to as the parent class of a. Inheritance allows subclasses to have various properties and methods of the parent class without having to write the same code again. While subclasses inherit the parent class, you can redefine some properties and override some methods to override the parent class's existing properties and methods to get a different function from the parent class. Basic concepts of inheritance1. Inheritance is one of the three main features of object-oriented. (Encapsulation, inheritance, polymorphism)2. The inherited class is the parent class, and the class that inherits the parent class is a subclass3. Inheritance enables an object to be used directly by another object's properties and methods4. Inheritance to implement code reuse
Limitations of inheritance 1.java can only show single inheritance, that is, a class can have only one parent class2. Allow Multiple inheritance3. Inheritance can inherit only non-private properties and methods4. Construction methods cannot be inherited See example situation One:
- public class Mystring {
- public static void Main (string[] args) {
- Student ok=new Student ("Xiaoming");
- Ok.say (); The//say method has been inherited.
- }
- }
- Class Person
- {
- public String name;
- Construction method
- Public person ()
- {
- System.out.println ("I am the person construction method");
- }
- public void Say ()
- {
- System.out.println ("The name is:" +name);
- }
- }
- Class student extends person//inherit person class
- {
- Public student (String name)
- {
- This.name=name;//person's name attribute is inherited.
- }
- }
Copy Code
the output is:
<ignore_js_op>
Conclusion 1: When a subclass is instantiated, the constructor of the parent class is called first, and then its own instantiation operation
situation Two:
at this point the keyword super is introduced. Super represents a reference to a parent class and can call methods and properties of the parent class. If you call the Say method of the parent class, you can use the. Super.say () call. conclusion Two: When the parent class has no default constructor, the subclass must display the constructor method of calling the parent class
overriding of inherited methods concept: In Java, subclasses can inherit methods from the parent class, but sometimes subclasses do not want to use the parent class's method intact, but want to make some changes, this need to adopt the method of rewriting, also known as the method overrides. method overrides the attributes that need attention1. The return value of the two methods of the overridden parent class and subclass, function name, parameter list must be exactly the same2. Subclass throws an exception that cannot exceed the exception thrown by the corresponding method of the parent class3. The access level of the subclass method must not be lower than the access level of the corresponding method of the parent class (such as the parent class method, protected, subclass rewrite will be protected or public) such as:
- public class Mystring {
- public static void Main (string[] args) {
- Student ok=new Student ("Xiaoming");
- Ok.say (); The//say method has been inherited.
- }
- }
- Class Person
- {
- public String name;
- public void Say ()
- {
- System.out.println ("The name is:" +name);
- }
- }
- Class student extends person//inherit person class
- {
- Public student (String name)
- {
- This.name=name;//person's name attribute is inherited.
- }
- Say Method overrides
- public void Say ()
- {
- System.out.println ("The overridden method name is:" +name);
- }
- }
Copy Code |
"Java Inheritance extends"