"Java Inheritance extends"

Source: Internet
Author: User

"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 inheritance

1. 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 subclass

3. Inheritance enables an object to be used directly by another object's properties and methods

4. Inheritance to implement code reuse



Limitations of inheritance

1.java can only show single inheritance, that is, a class can have only one parent class

2. Allow Multiple inheritance

3. Inheritance can inherit only non-private properties and methods

4. Construction methods cannot be inherited


See Example


Situation One:


  1. public class Mystring {


  2. public static void Main (string[] args) {


  3. Student ok=new Student ("Xiaoming");

  4. Ok.say (); The//say method has been inherited.

  5. }


  6. }

  7. Class Person

  8. {

  9. public String name;

  10. Construction method

  11. Public person ()

  12. {

  13. System.out.println ("I am the person construction method");

  14. }

  15. public void Say ()

  16. {

  17. System.out.println ("The name is:" +name);

  18. }

  19. }

  20. Class student extends person//inherit person class

  21. {

  22. Public student (String name)

  23. {

  24. This.name=name;//person's name attribute is inherited.

  25. }

  26. }

Copy Code




the output is:

650) this.width=650; "id=" aimg_290 "src=" http://techfoxbbs.com/data/attachment/forum/201505/22/ 131937lza12ieceze0v152.png "class=" Zoom "width=" 303 "alt=" 131937lza12ieceze0v152.png "/>

Conclusion 1: When a subclass is instantiated, the constructor of the parent class is called first, and then its own instantiation operation


Situation Two:


650) this.width=650; "id=" aimg_291 "src=" http://techfoxbbs.com/data/attachment/forum/201505/22/ 132007ma0178088al19l5r.png "class=" Zoom "width=" 439 "alt=" 132007ma0178088al19l5r.png "/>


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 attention

1. The return value of the two methods of the overridden parent class and subclass, function name, parameter list must be exactly the same

2. Subclass throws an exception that cannot exceed the exception thrown by the corresponding method of the parent class

3. 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:

  1. public class Mystring {


  2. public static void Main (string[] args) {


  3. Student ok=new Student ("Xiaoming");

  4. Ok.say (); The//say method has been inherited.

  5. }


  6. }

  7. Class Person

  8. {

  9. public String name;


  10. public void Say ()

  11. {

  12. System.out.println ("The name is:" +name);

  13. }

  14. }

  15. Class student extends person//inherit person class

  16. {

  17. Public student (String name)

  18. {

  19. This.name=name;//person's name attribute is inherited.

  20. }


  21. Say Method overrides

  22. public void Say ()

  23. {

  24. System.out.println ("The overridden method name is:" +name);


  25. }

  26. }

Copy Code

650) this.width=650; "id=" aimg_292 "src=" http://techfoxbbs.com/data/attachment/forum/201505/22/ 132040qkukrb3n7py46rb1.png "class=" Zoom "width=" 294 "alt=" 132040qkukrb3n7py46rb1.png "/>


"Java Inheritance extends"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.