Java-hb- abstract classes and final

1, the concept of

  • Abstract class used as a type family topmost parent class, family class represents all things in common
  • The abstract modification
  • Abstract methods:abstract public void f();
  • Abstract class:
abstract class A{
    abstract public void f();
}
  • An abstract class may not have abstract methods, abstract methods must be abstract class


2, abstract class inheritance:

abstract class A{
    abstract public void f();
}

class B extends A{
    public void f(){
        System.out.printf("BBBB\n");
    }
}

Animals (A) has the function of eating dog (B) how to eat more specific refinement (output BBBB)

3, polymorphism

public class ab{
    public static void main(String[] args) {
        B bb = new B();
        bb.f();
        A aa;
        aa = bb;
        aa.f();
    }
}
输出结果:   BBBB
        BBBB


4、final

final can modify the entire class and the class members in
final modified class property, the property must be assigned a value and can only be assigned once
final modifications class several methods, that can inherit, but not overridden by subclasses

class A{
    final public int i;

    public A(){
        i = 22;
    }
    
    public void f(){
        //i = 22;error;
    }
}

Not necessarily call f (), but it will call the constructor A ()

Guess you like

Origin www.cnblogs.com/aabyss/p/12288383.html