Extends usage of Java

Source: Internet
Author: User

Understanding inheritance is the key to understanding object-oriented programming. In Java, an existing class is inherited by the keyword extends, the inherited class is called the parent class (the superclass, the base class), and the new class is called a subclass (derived class). Multiple inheritance is not allowed in Java.
(1) Inheritance

[Java]View PlainCopyprint?
  1. Class animal{
  2. void Eat () {
  3. System.out.println ("Animal eat");
  4. }
  5. void Sleep () {
  6. System.out.println ("Animal Sleep");
  7. }
  8. void Breathe () {
  9. System.out.println ("Animal Breathe");
  10. }
  11. }
  12. Class Fish extends animal{
  13. }
  14. Public class Testnew {
  15. public static void Main (string[] args) {
  16. //TODO auto-generated method stub
  17. Animal an = new Animal ();
  18. FISH fn = new fish ();
  19. An.breathe ();
  20. Fn.breathe ();
  21. }
  22. }
Class Animal{void Eat () {System.out.println ("Animal eat");} void Sleep () {System.out.println ("Animal Sleep");} void Breathe () {System.out.println ("Animal Breathe");}} Class Fish extends Animal{}public class Testnew {public static void main (string[] args) {//TODO auto-generated method Stu Banimal an = new Animal (); FISH fn = new fish (); an.breathe (); Fn.breathe ();}}

In Eclipse execution:
Animal breathe!
Animal breathe!
Each class in the. java file generates a corresponding. class file under the folder bin. The execution result shows that the derived class inherits all the methods of the parent class.

(2) Coverage

[Java]View PlainCopyprint?
  1. Class animal{
  2. void Eat () {
  3. System.out.println ("Animal eat");
  4. }
  5. void Sleep () {
  6. System.out.println ("Animal Sleep");
  7. }
  8. void Breathe () {
  9. System.out.println ("Animal Breathe");
  10. }
  11. }
  12. Class Fish extends animal{
  13. void Breathe () {
  14. System.out.println ("Fish Breathe");
  15. }
  16. }
  17. Public class Testnew {
  18. public static void Main (string[] args) {
  19. //TODO auto-generated method stub
  20. Animal an = new Animal ();
  21. FISH fn = new fish ();
  22. An.breathe ();
  23. Fn.breathe ();
  24. }
  25. }
Class Animal{void Eat () {System.out.println ("Animal eat");} void Sleep () {System.out.println ("Animal Sleep");} void Breathe () {System.out.println ("Animal Breathe");}} Class Fish extends Animal{void breathe () {System.out.println ("Fish Breathe");}} public class Testnew {public static void main (string[] args) {//TODO auto-generated method Stubanimal an = new Animal (); F ISH fn = new Fish (); an.breathe (); Fn.breathe ();}}

Execution Result:

Animal Breathe
Fish Breathe

A method that defines the same name, return type, and parameter type of the parent class in the subclass is called the override of the method. The override of the method occurs between the child class and the parent class. In addition, you can provide access to the parent class with super.

Extends usage of Java

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.