Question 1
1) Private methods are final. 2) Protected members are accessible within a package and inherited classes outside the package. 3) Protected methods are final. 4) We cannot override private methods.
Question 2
class Base {
public void Print() {
System.out.println(\"Base\");
}
}
class Derived extends Base {
public void Print() {
System.out.println(\"Derived\");
}
}
class Main{
public 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);
}
}
Question 3
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();
}
}
Question 4
// filename Main.java
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();
}
}
Question 5
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();
}
}
Question 6
Question 7
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);
}
}
Question 8
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\");
}
}
class Main {
public static void main(String[] args) {
Base b = new Derived();;
b.show();
}
}
Question 9
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\");
}
}
class Main {
public static void main(String[] args) {
Base b = new Derived();
b.show();
}
}
There are 9 questions to complete.