能被子类继承,若是没有被final修饰,则能被重写,当父类引用指向子类对象时,表现出多态性。java
不能被子类继承,更不能被重写,没有多态性(有的人理解为父类的全部包括私有的成员都能被继承,只是在子类中不可见,我更倾向于前者)。当子类中出现与父类私有方法名和参数相同的时候会发生什么呢?编程
class Parent{ private void f() { System.out.println("parent"); } public static void main(String[] args) { Parent p = new Child(); p.f(); } } class Child extends Parent{ public void f() { //父类的私有方法在子类中不可见,子类的f()方法是一个全新的方法,编译器认为f()方法没有被重写
System.out.println("child");
}
}
打印结果:安全
parent
静态方法能够被继承,不能被重写,也就不能表现出多态性this
class Parent{
public static void f() {
System.out.println("parent");
}
}
class Child extends Parent{
public static void f() {
System.out.println("child");
}
public static void main(String[] args) {
Parent p = new Child(); //静态方法能被继承,但不能被重写
p.f();
Child c = new Child();
c.f();
}
}
打印结果:spa
parent
child
构造方法不能被继承,不能被重写,没有多态性。code
构造方法既不是静态方法也不是非静态方法,构造方法中会有一个this对象做为参数传进去,因此咱们能够在构造方法内部对对象属性进行初始化,也能够在构造方法内调用非静态方法。对象
若是该非静态方法被重写过,那么构造器内部会不会存在多态行为呢?参考Java编程思想中的一个例子:blog
class Glyph {
void draw() {
System.out.println("Glyph.draw()");
}
Glyph() {
System.out.println("Glyph() before draw()");
draw();
System.out.println("Glyph() after draw()");
}
}
class RoundGlyph extends Glyph {
private int radius = 1;
RoundGlyph(int r) {
radius = r;
System.out.println("RoundGlyph.RoundGLyph(), radius = " + radius);
}
void draw() {
System.out.println("RoundGlyph.draw(), radius = " + radius);
}
}
class RolyConstructors {
public static void main(String[] args) {
new RoundGlyph(5);
}
}
在父类构造器中调用被子类重写的非静态方法,会发生多态行为,但这并非咱们想要的结果,缘由以下:继承
所以,在编写构造器中有一条有效的准则:“用尽量简单的方法使对象进入正常状态;若是能够的话,避免调用其余方法”。在构造器中,惟一可以安全调用的是基类中的final方法(包括private方法),由于这些方法不能被子类覆盖,也就不会出现上述的问题。get
都能被继承(与是不是静态的无关),不能被重写,没有多态性。当子类中定义了与父类相同的属性时,子类会在不一样的存储空间同时保留本身的和父类的属性
class Child extends Parent {
public static int a = 2;
public void getA() {
System.out.println("a = "+a);
}
public void ParentA() {
System.out.println("super.a = " + super.a);
}
public static void main(String[] args) {
Parent p = new Child();
System.out.println(p.a); //任何域访问操做都由编译器解析
Child c = new Child();
c.getA(); //直接访问field默认会获取本身的域
c.ParentA(); //经过super.field能获取父类的域
}
}
我的理解为能够被继承,可是不能直接访问,能经过父类public、default、或protected方法间接访问(也有人理解为不能为继承)
class Parent {
private int a;
public Parent(int a) {
this.a = a;
}
public int getA() {
return a;
}
}
class Child extends Parent {
public Child(int a) {
super(a);
}
public static void main(String[] args) {
Child c = new Child(1);
System.out.println(c.getA()); //结果为1
}
}
当父类和子类存在相同私有属性时:
class Parent {
private int a;
public Parent(int a) {
this.a = a;
}
public int getA() {
return a;
}
}
class Child extends Parent {
private int a = 2;
public Child(int a) {
super(a);
}
public static void main(String[] args) {
Child c = new Child(1);
System.out.println(c.getA()); //1
System.out.println(c.a); //2
}
}
关于继承与多态以及对象初始化过程还有不少不是很理解的地方,先记录下来,等往后有时间研究一下java虚拟机的原理再来完善!