Java Learning (vi): Inheritance in Java and analysis of common problems

Source: Internet
Author: User

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

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.