Inheritance in Java and analysis of common problems
1. Definitions of inheritance in Java
In Java, the inheritance of a class is implemented by extending other classes to form a new class, the original class is called the parent class (Super Class) or the base class, and the new class is called the subclass or derived class of the original class. In subclasses, not only the properties and methods of the parent class, but also new properties and methods can be added, so that the basic characteristics of the parent class can be shared by all child class objects.
Note: The inheritance of a class does not change the access rights of a class member, that is, if the member of the parent class is public, protected, or default, its subclasses still have the corresponding attributes.
/******************************************************************///class inherits the definition format class ChildClass extends The Fatherclass//childclass class inherits Fatherclass, ChildClass is a subclass, and Fatherclass is the principal of the parent class {//Class}/*********************************** *******************************/
inheritance rules for Java: Java does not support multiple inheritance, allowing only one class to inherit another class directly, that is, subclasses can have only one parent class, that is, there can only be one class name after the extends keyword. Although a class can have only one direct parent, it may have multiple indirect parent classes.
2. Frequently asked questions in Java
Frequently encountered problems in inheritance:
The following combination code is analyzed:
The code that produces the problem is as follows:
public class Ooprogram {//parent class public void Say () {System.out.println ("I am the parent Class!") ");} public class Childooprogram extends ooprogram{//subclass inherits the parent class public void Childsay () {System.out.println ("I am a subclass!") ");}} public static void Main (String [] args) {Ooprogram father=new ooprogram ();//Create parent class Father.say ();//parent class calls parent class Childooprogram Child=new Childooprogram ();//Create Subclass Child.say ();//Subclass Call 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 the parent Class!") ");} public class Childooprogram extends ooprogram{//subclass inherits the parent class public void Childsay () {System.out.println ("I am a subclass!") ");}} public static void Main (String [] args) {Ooprogram father=new ooprogram ();//Create parent class Father.say ();//parent class calls parent class Childooprogram Child=father.new Childooprogram ();//Create Subclass Child.say ();//Subclass Call Parent class Child.childsay ();//Subclass Call Subclass}}
The difference is: How to create subclasses, namely:
Childooprogram child=new Childooprogram ();//create subclass with Childooprogram child=father.new Childooprogram ();//create sub-class
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Java Learning (vi): Inheritance in Java and analysis of common problems