Inheritance mode for Java

Source: Internet
Author: User

There are several modes of inheritance in Java, what is the most used mode? A: In Java is a single inheritance, through the extends keyword to implement the subclass is the extension of the parent class, get the parent class public and protected all methods and properties. For example:
public class Fruit{public double weight;public void info(){System.out.println("我是一个水果!重:"+weight+"g");}}
Program entry
class Apple extends Fruit{public static void main(String[] args) {// TODO Auto-generated method stub//创建一个Apple对象Apple a=new Apple();/*在Apple中本来没有weight和info()方法的,但是继承的父类,所有子类Apple继承父类的属性和方法,也可以访问*/a.weight=56;a.info();}}
Subclasses extend the parent class, in some cases, subclasses are required to write a parent class method, overriding a parent class method in a subclass, overwriting a method of the parent class, where the subclass cannot invoke the method that the parent class is overridden in, and can invoke the method called by the parent in the subclass method, or if the overridden method in the parent class is called in the subclass, you can use super (overridden method) or the parent class is called by the caller to call the overridden method in the parent class with inheritance-related concepts:
    1. Super is a keyword that is provided by Java, and super is used to qualify the object to invoke its properties derived from the parent class's inheritance, or the method super cannot appear in the static decorated method, the static adornment method belongs to the class, the caller of the method may be a class, not an object, And so the super limit loses its meaning.
    2. Method overloading and overriding: In the same class, the method name is the same, but the parameter is different, the parameter is put back to the type or the return value type is different called the method overload; overriding is describing the relationship between the parent class and the child class, overriding the parent class method must be the same as the original method name, the parameter and the return value type

Inheritance mode for Java

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.