JAVA Learning (6): inheritance in JAVA and analysis of its common problems, java FAQs
Inheritance in JAVA and Analysis of Common Problems
1. Definitions inherited in JAVA
In JAVA, classes are inherited throughExtend other classes to form new classesThe original Class is called the parent Class or the base Class, and the new Class is called the subclass or derived Class of the original Class. A subclass not only contains attributes and methods of the parent class, but also adds new attributes and methods so that the basic features of the parent class can be shared by all subclass objects.
Note: class inheritance does not change the access permissions of class members. That is to say, if the members of the parent class are public, protected, or default, its subclass still has these features.
/*************************************** * ************************ // Class ChildClass extends FatherClass/ /ChildClass class inherits FatherClass, childClass is a subclass, fatherClass is the parent class {// class subject }/**************************** **************************************/
JAVA inheritance rules: JAVA does not support multi-inheritance. Only one class can inherit from another class directly. That is, only one parent class is allowed for a subclass. That is to say, only one class name can be followed by the extends keyword. Although a class can only have one direct parent class, it can have multiple indirect parent classes.
2. FAQs in JAVA
Problems frequently encountered in inheritance:
The following code is used for analysis:
The error code is as follows:
Public class Ooprogram {// parent class public void say () {System. out. println ("I am a parent class! ");} Public class ChildOoprogram extends Ooprogram {// subclass inherits the parent class public void childsay () {System. out. println (" I'm a subclass! ") ;}} Public static void main (String [] args) {Ooprogram father = new Ooprogram (); // create the parent class father. say (); // The parent class calls the parent class ChildOoprogram child = new ChildOoprogram (); // creates a child class. say (); // subclass calls parent class child. childsay (); // subclass call subclass }}
The correct code is as follows:
Public class Ooprogram {// parent class public void say () {System. out. println ("I am a parent class! ");} Public class ChildOoprogram extends Ooprogram {// subclass inherits the parent class public void childsay () {System. out. println (" I'm a subclass! ") ;}} Public static void main (String [] args) {Ooprogram father = new Ooprogram (); // create the parent class father. say (); // The parent class calls the parent class ChildOoprogram child = father. new ChildOoprogram (); // create a child class. say (); // subclass calls parent class child. childsay (); // subclass call subclass }}
The difference between them is: how to create a subclass, that is:
ChildOoprogram child = new ChildOoprogram (); // create a subclass and ChildOoprogram child = father. new ChildOoprogram (); // create a subclass
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.