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

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

"Java Inheritance extends"

Related Article

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.