Inheritance in Java - Java Inheritance Tutorial - Part 2

preview_player
Показать описание

In this second part we will learn following:

1. Calling superclass methods using super keyword in subclass
2. instanceof instruction behavior with superclass and subclass
3. Fields with subclass, shadowing superclass fields
4. Constructors are not inherited and in case Constructor is defined in super class, it has to be defined in subclass as well and a call to super class constructor is mandatory as the first statement.
5. Nested classes behaves like fields in super classes and depends on the access modifier with them.
6. Super class can be marked abstract to avoid instance creation and provides as a base class.

Рекомендации по теме
Комментарии
Автор

But parameter-less constructors get inherited by the child classes.
Try this code.

// Animal - Superclass
public class Animal{
public static final int aNumber = 100;
public Animal(){
System.out.println(aNumber);
}
}

//Cat - Subclass
public class Cat extends Animal{

}

public static void main(String[] args){
Animal animal = new Animal(); // "100"
Cat cat = new Cat(); // "100"
}

mistery
welcome to shbcf.ru