You are on page 1of 3

1.

Write a short difference between class and interface and write the general syntax to
define an interface

Class Interface

Syntax to Define an Interface

2. Write the difference between abstract and final class used in Java programming
language by giving examples (2 pts).

Abstract Class Final Class

3. Read and trace the following simple Java program carefully and identify the syntax
errors you observed and write your answer inside the box below.
//Define Super Class by the name
class AS{
}
//Define a sub class B
class BS AS{
//Declaration of instance variables of class B
int k;
//Define instance method of B
void dispB(){
System.out.println("k="+k);
}//End of dispB()
//Define another method in B
void Sum(){
System.out.println("m+n+k="+(m+n+k));
}//End of Sum()
}//End of sub class B
//Define another class to create objects o
public class SingleInheritance {
public static void main(String[] args) {
//Declare and create objects of sub-class Syntax Errors
AS a=new AS();
BS b=new BS(20,50);
//Initialize each instance variable
a.m=20;
a.n=40;
a.k=40;
//Call dispA() method
b.dispA();
//Call dispB() method
a.dispB();
//call Sum() method
a.Sum();
}//End of main ()
}//End of class
4. Read the following java program carefully, determine the output of the program and
write its output inside the box below (5 Pts
class AD{
protected int i; Output
public AD(int i){
this.i = i;
}//End of Constructor
public void Increment(AD base){
base.i++;
System.out.println("Incremented I = "+base.i);
}//End of Increment()
}//End of class A
class BD extends AD{
//Parameterized Constructor
public BD(int i){
//Calling a super class Constructor
super(i);
}//End of Constructor
public void Decrement(BD sub){
sub.i--;
System.out.println("Decremented I = "+sub.i);
}//End of Decrement()
}//End of Sub class B
class UpcastingExample2{
public static void main (String args[]){
AD a1;
BD b1, b2;
//create and initialize objects of b1
b1=new BD(400);
b2 = new BD(22);
a1=b2;
a1=b1;
//Create and Initialize objects of A
a1 = new AD(110);
a1.Increment(b1);
b1.Decrement(b2);
a1.Increment(b2);
//Call through b2 and pass sub objects of b2
b2.Decrement(b1);
//Call through b1 and pass objects of a1
b1.Increment(a1);
}//End of main ()

You might also like