Detailed Java uses the Externds keyword inheritance class usage _java

Source: Internet
Author: User
Tags inheritance stub

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

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 A uto-generated method Stub 
    Animal a = new Animal (); 
    FISH fn = new fish (); 
     
    An.breathe (); 
    Fn.breathe (); 
  } 
 

Implemented in Eclipse:

Animal breathe! 
Animal breathe! 

Each class in the. java file will generate a corresponding. class file under Folder Bin. The execution results show that the derived class inherits all the methods of the parent class.

(2) Covering

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 stub 
    Animal a n = new Animal (); 
    FISH fn = new fish (); 
     
    An.breathe (); 
    Fn.breathe (); 
  } 
 

Execution results:

Animal Breathe
Fish Breathe

Defines a method in a subclass that has the same name as the parent class, returns a type, and has the same parameter type, called a method overlay. The override of the method occurs between the subclass and the parent class. In addition, super can provide access to the parent class.

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.