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:
- 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.
- 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