java继承面试题a b_【笔试题】Java 继承

笔试题 Java 继承

Question 1 Output of following Java Program?

class Base {

public void show() {

System.out.println("Base::show() called");

}

}

class Derived extends Base {

public void show() {

System.out.println("Derived::show() called");

}

}

public class Main {

public static void main(String[] args) {

Base b = new Derived();

b.show();

}

}

A. Derived::show() called

B. Base::show() called

参考答案

```

A

```

Question 2

class Base {

final public void show() {

System.out.println("Base::show() called");

}

}

class Derived extends Base {

public void show() {

System.out.println("Derived::show() called");

}

}

public class Main {

public static void main(String[] args) {

Base b = new Derived();

b.show();

}

}

A. Base::show() called

B. Derived::show() called

C. Compiler Error

D. Runtime Error

参考答案

```

C

```

解析:Java 中 final 修饰方法不允许被子类重写,但是可以被子类继承,final 不能修饰构造方法。

Question 3

class Base {

public static void show() {

System.out.println("Base::show() called");

}

}

class Derived extends Base {

public static void show() {

System.out.println("Derived::show() called");

}

}

public class Main {

public static void main(String[] args) {

Base b = new Derived();

b.show();

}

}

A. Base::show() called

B. Derived::show() called

C. Compiler Error

参考答案

```

A

```

解析:Java 中静态属性和静态方法可以被继承,但是没有被重写 (overwrite) 而是被隐藏。

Question 4 **Which of the following is true about inheritance in Java?

Private methods are final.

Protected members are accessible within a package and inherited classes outside the package.

Protected methods are final.

We cannot override private methods.**

A. 1, 2 and 4

B. Only 1 and 2

C. 1, 2 and 3

D. 2, 3 and 4

参考答案

```

A

```

Question 5 Output of following Java program?

class Base {

public void Print() {

System.out.println("Base");

}

}

class Derived extends Base {

public void Print() {

System.out.println("Derived");

}

}

public class Main {

private static void DoPrint(Base o) {

o.Print();

}

public static void main(String[] args) {

Base x = new Base();

Base y = new Derived();

Derived z = new Derived();

DoPrint(x);

DoPrint(y);

DoPrint(z);

}

}

A. Base

Derived

Derived

B. Base

Base

Derived

C. Base

Derived

Base

D. Compiler Error

参考答案

```

A

```

Question 6 Predict the output of following program. Note that fun() is public in base and private in derived.

class Base {

public void foo() {

System.out.println("Base");

}

}

class Derived extends Base {

private void foo() {

System.out.println("Derived");

}

}

public class Main {

public static void main(String args[]) {

Base b = new Derived();

b.foo();

}

}

A. Base

B. Derived

C. Compiler Error

D. Runtime Error

参考答案

```

C

```

参考解析

重写要遵循**"两同两小一大"**原则:

**1) 两同:**

* 1.1) 方法名相同

* 1.2) 参数列表相同

2) 两小:

2.1) 子类方法的返回值类型小于或等于父类的

2.1.1) void 时,必须相同

2.1.2) 基本类型时,必须相同

2.1.3) 引用类型时,小于或等于

2.2) 子类方法抛出的异常小于或等于父类的(异常之后)

3) 一大:

3.1) 子类方法的访问权限大于或等于父类的(访问控制修饰符后)

Question 7 **Which of the following is true about inheritance in Java.

In Java all classes inherit from the Object class directly or indirectly. The Object class is root of all classes.

Multiple inheritance is not allowed in Java.

Unlike C++, there is nothing like type of inheritance in Java where we can specify whether the inheritance is protected, public or private.**

A. 1, 2 and 3

B. 1 and 2

C. 2 and 3

D. 1 and 3

参考答案

```

A

```

Question 8 Predict the output of following Java Program?

class Grandparent {

public void Print() {

System.out.println("Grandparent's Print()");

}

}

class Parent extends Grandparent {

public void Print() {

System.out.println("Parent's Print()");

}

}

class Child extends Parent {

public void Print() {

super.super.Print();

System.out.println("Child's Print()");

}

}

public class Main {

public static void main(String[] args) {

Child c = new Child();

c.Print();

}

}

A. Compiler Error in super.super.Print()

B. Grandparent's Print()

Parent's Print()

Child's Print()

C. Runtime Error

参考答案

```

A

```

解决方案

Java 代码

class Grandparent {

public void Print() {

System.out.println("Grandparent's Print()");

}

}

class Parent extends Grandparent {

public void Print() {

super.Print();

System.out.println("Parent's Print()");

}

}

class Child extends Parent {

public void Print() {

super.Print();

System.out.println("Child's Print()");

}

}

public class Main {

public static void main(String[] args) {

Child c = new Child();

c.Print();

}

}

运行结果

Grandparent's Print()

Parent's Print()

Child's Print()

Question 9

final class Complex {

private final double re;

private final double im;

public Complex(double re, double im) {

this.re = re;

this.im = im;

}

public String toString() {

return "(" + re + " + " + im + "i)";

}

}

class Main {

public static void main(String args[]) {

Complex c = new Complex(10, 15);

System.out.println("Complex number is " + c);

}

}

A. Complex number is (10.0 + 15.0i)

B. Compiler Error

C. Complex number is SOME_GARBAGE

D. Complex number is Complex@8e2fb5(Here 8e2fb5 is hash code of c)

参考答案

```

A

```

参考链接

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值