Tips by Doing Exercise

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

Question 1 : Interface

Fill in the blanks to make the program below run successfully.


interface Shape {

______________________
______________________
}

public class Point implements Shape {

static int x, y;
public Point() {
x = 0;
y = 0;
}
public double area() {
return 0;
}
public double volume() {
return 0;
}
public static void print() {
System.out.println("point: " + x + "," + y);
}
public static void main(String args[]) {
Point p = new Point();
p.print();
}
}







Question 2 : Inheritance , Constructor chaining
Trace the execution of the following program:
class B {
public B() { }
public void p() { System.out.println("Big"); }
}

class D extends B {
public D() {}
public void p() {
super.p();
System.out.println("Deep");
}
}

public class Try {
public static void main(String[] args) {
B b = new B();
D d = new D();
b.p();
d.p();
b = d;
b.p();
}

}
Big
Big
Deep
Big
Deep
Question 3 : Inheritance, Objects and Methods
What's the output of the program after you compile it and run it? Explain how that happens.
class A {
void fun() { System.out.println("Defined in A"); }
}

class B extends A {
void fun() { System.out.println("Defined in B"); }
}

public class Two {
public static void main(String[] args) {
B me = new B();
A you = new B();
me.fun();
you.fun();

}
}


Question 4 : Interface vs. Abstract Class
All methods of an Interface are abstract methods while some methods of an Abstract class
are abstract methods (True/False)
Abstract methods of abstract class have abstract modifier (True/False)
An interface can only define constants while abstract class can have fields . (True/False)


Question 5 : Interface and Abstract Class
interface A
{
void displayA();
}

abstract class B
{
public void displayB()
{
System.out.println("Display-B");
}

abstract public void display();
}
class C extends B implements A
{
public void displayB()
{
System.out.println("Abstract Display-B");
}

public void displayA()
{
System.out.println("Display-A");
}
}

public class TestClass
{
public static void main(String args[])
{
C obj=new C();
obj.display();
obj.displayA();
obj.displayB();
}
}

What wrong with the above program ?
Main.java:16: error: c is not abstract and does not override abstract method
displayd() in A
class c extends B implements A

You might also like