Java覆盖和隐藏
- (1) 变量只能被隐藏(包括静态和非静态),不能被覆盖;
- (2) 可以用子类的静态变量隐藏父类的静态变量,也可以用子类的非静态变量隐藏父类的静态变量,也可以用非最终变量(final)隐藏父类中的最终变量;
- (3) 静态属性(static)和静态方法(static)可以被继承,但是不能被重写、被覆盖(@Override)而是被隐藏;
- (4) 非静态方法可以被覆盖;
- (5) 不能用子类的静态方法隐藏父类中的非静态方法,否则编译会报错;
- (6) 不能用子类的非静态方法覆盖父类的静态方法,否则编译会报错;
- (7) 不能重写父类中的最终方法(final);
- (8) 抽象方法必须在具体类中被覆盖;
- (9) 静态方法和实例方法不能互相覆盖;
Java里用final修饰的成员和成员变量能否被继承
- (1) final修饰的成员和成员变量,可以被继承。
- (2) final修饰的方法可以被继承不能被重写。
- (3) final修饰的类,不能被继承。
/**
* Cat class
* Java 中实例方法被覆盖,静态方法被隐藏
*
* @author libin
* @date 2019/1/28 17:05
*/
class Animal {
public static void testClassMethod() {
System.out.println("Class" + " method in Animal.");
}
public void testInstanceMethod() {
System.out.println("Instance " + " method in Animal.");
}
}
public class Cat extends Animal {
public static void testClassMethod() {
System.out.println("The class method" + " in Cat.");
}
@Override
public void testInstanceMethod() {
System.out.println("The instance method" + " in Cat.");
}
public static void main(String[] args) {
Cat myCat = new Cat();
Animal myAnimal = myCat;
Animal.testClassMethod();
myAnimal.testInstanceMethod();
}
}
参考答案
```
Class method in Animal.
The instance method in Cat.
```
Class method in Animal.
The instance method in Cat.
```
/**
* Demo class
*
* @author libin
* @date 2019/1/28 16:25
*/
class A {
static void fun() {
System.out.println("A.fun()");
}
}
class B extends A {
static void fun() {
System.out.println("B.fun()");
}
}
public class Demo {
public static void main(String args[]) {
A a = new B();
a.fun();
}
}
参考答案
```
A.fun()
```
A.fun()
```
/**
* Son class
*
* @author libin
* @date 2019/1/29 10:25
*/
class Dad {
protected static String me = "dad";
public void printMe() {
System.out.println(me);
}
}
public class Son extends Dad {
protected static String me = "son";
@Override
public void printMe() {
System.out.println(me);
}
public static void main(String[] args) {
Dad dad = new Son();
Son son = new Son();
System.out.println("son.me:" + son.me);
System.out.println("dad.me:" + dad.me);
System.out.println("Son.me:" + Son.me);
System.out.println("Dad.me:" + Dad.me);
son.printMe();
dad.printMe();
}
}
参考答案
```
son.me:son
dad.me:dad
Son.me:son
Dad.me:dad
son
son
```
son.me:son
dad.me:dad
Son.me:son
Dad.me:dad
son
son
```
反编译Class代码
```
class Dad
{
protected static String me;
class Dad
{
protected static String me;
public void printMe() {
System.out.println(Dad.me);
}
static {
Dad.me = "dad";
}
}
public class Son extends Dad
{
protected static String me;
@Override
public void printMe() {
System.out.println(Son.me);
}
public static void main(final String[] args) {
final Dad dad = new Son();
final Son son = new Son();
System.out.println("son.me:" + Son.me);
System.out.println("dad.me:" + Dad.me);
System.out.println("Son.me:" + Son.me);
System.out.println("Dad.me:" + Dad.me);
son.printMe();
dad.printMe();
}
static {
Son.me = "son";
}
}
</div>
**参考链接**
* [https://docs.oracle.com/javase/tutorial/java/IandI/override.html](https://docs.oracle.com/javase/tutorial/java/IandI/override.html)
* [https://www.cnblogs.com/cs1003/p/3408119.html](https://www.cnblogs.com/cs1003/p/3408119.html)