0% found this document useful (0 votes)
48 views3 pages

Cosc 3021 (Selif Test Exercise)

Class and interface are compared in the first paragraph. The general syntax to define an interface is provided. The second paragraph contrasts an abstract class and a final class, noting that an abstract class can have both abstract and non-abstract methods while a final class cannot be extended. The third paragraph identifies two syntax errors in the provided code: 1) BS is not defined as extending AS and 2) BS constructor is missing super keyword to call parent constructor. The fourth paragraph outputs the results of calling methods that increment and decrement an integer field between instances of classes AD and BD, its subclass. The output is: Incremented I = 401, Decremented I = 21, Incremented I = 111,

Uploaded by

Tshu tshu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views3 pages

Cosc 3021 (Selif Test Exercise)

Class and interface are compared in the first paragraph. The general syntax to define an interface is provided. The second paragraph contrasts an abstract class and a final class, noting that an abstract class can have both abstract and non-abstract methods while a final class cannot be extended. The third paragraph identifies two syntax errors in the provided code: 1) BS is not defined as extending AS and 2) BS constructor is missing super keyword to call parent constructor. The fourth paragraph outputs the results of calling methods that increment and decrement an integer field between instances of classes AD and BD, its subclass. The output is: Incremented I = 401, Decremented I = 21, Incremented I = 111,

Uploaded by

Tshu tshu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 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