Explain the extends and super keywords inherited by Java, extendssuper

Source: Internet
Author: User

Explain the extends and super keywords inherited by Java, extendssuper

A function is also called a method!

Inheritance: Use the extends keyword in java to represent the inheritance relationship. Super is used to inherit the methods and parameters of the parent class.

Inheritance means that the Child class inherits the features and actions of the parent class, so that the child class has the same behavior of the parent class.

Note:

1. When a class does not inherit any class, the system inherits the Object by default.

2. The parent class is also called the base class, super class, and super class, And the subclass is also called the derived class, which is caused by translation problems.

3. Java inheritance is single.

4. Subclass cannot inherit the constructor of the parent class, but can inherit the parameters of the constructor class.

5. Subclass can have its own attributes and methods, that is, subclass can expand the parent class. However, child classes cannot inherit the attributes and Methods Modified by the parent class private.

Syntax format:

Default System inheritance

Class name extends Object {/* code block */}

Correct inheritance syntax

Class subclass name extends parent class name {/* code block */}

Error inheritance syntax

Class subclass name: extends parent class name. parent class name {/* multi-inheritance not supported */}

Create a parent class named Father:

Public class Father {int a; int B; int addSum; Father (int a, int B) {// constructor of the parent class this. a = a; this. B = B;} void Cal () {// method of the parent class, addSum = a + B; System. out. println (addSum);} public static void main (String [] args) {Father f = new Father (2, 2); // create an object to initialize f. cal (); // the method used by the parent class to call the parent class }}

Parent Class running result: 4

Create a subclass named Son:

Public class Son extends Father {Son (int a, int B) {// constructor of the subclass super (a, B ); // inherit the parameters from the parent class} void son () {// subclass's own method super. cal (); // subclass calls the parent class method} public static void main (String [] args) {Son s = new Son (3, 3); // create an object to initialize s. son (); // subclass call subclass method }}

Subclass running result: 6

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.