JAVA Learning (6): inheritance in JAVA and analysis of its common problems, java FAQs

Source: Internet
Author: User

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.

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.