0% found this document useful (0 votes)
4 views7 pages

Tutorial 09 Sol

Bennett 2nd year tutorial 9

Uploaded by

Aryan singh
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)
4 views7 pages

Tutorial 09 Sol

Bennett 2nd year tutorial 9

Uploaded by

Aryan singh
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/ 7

School of Computer Science Engineering and Technology

Course- BTech Type- Core


Course Code- CSET Course Name- Object Oriented Programming
Using Java
Year- First Semester- Even Batch- BTech 2nd Semester

Tutorial-8

Tutori Name CO1 CO2 CO3


al No.
9 Abstract -- --
Class /
Interface

Objective: The main objective of this tutorial is to learn about the basics of Java
language.

9.1 Complete the below code for storing two Student record.

abstract class AbstractClass {

protected abstract int abstractMethod();

class TestClass extends AbstractClass {

protected int abstractMethod(){

return 1;

public static void main(String[] args) {

AbstractClass t = new TestClass();

System.out.println(t.abstractMethod());

Output: 1

*Note: please explain the concept of access modifier specially “protected”.


School of Computer Science Engineering and Technology
9.2 Complete the code for given output for “Test “class”:

_ _ _ _ _ _ _ class Test {
_ _ _ _ _ _ _ _ _ _;
}
public class MainClass extends Test{
public static void main(String[] args) {
_ _ _ _ _ t=new ();
int[] ans = t.getSumAndSub(25, 55);
System.out.println("Sum = " + ans[0]);
System.out.println("Sub = " + ans[1]);
}
@Override
int[] getSumAndSub(int a, int b) {
int[] ans = new int[2];
ans[0] = a + b;
ans[1] = a - b;
return______ ;
}
}

Output:
Sum= 80
Sub= -30

abstract class Test {


// Declare an abstract method
abstract int[] getSumAndSub(int a, int b);
}

public class MainClass extends Test {


public static void main(String[] args) {
MainClass t = new MainClass();
int[] ans = t.getSumAndSub(25, 55);
System.out.println("Sum = " + ans[0]);
System.out.println("Sub = " + ans[1]);
}

@Override
int[] getSumAndSub(int a, int b) {
int[] ans = new int[2];
ans[0] = a + b;
ans[1] = a - b;
return ans;
}
}

*Note: Please explain abstract class and abstract methods.


School of Computer Science Engineering and Technology
9.3 What will be the output of the following program?

public class TestClass extends Product {


TestClass(int pid, String n) {
super(pid, n);
}
public void display() {
System.out.print("Product Id = "+pro_Id + " " + " Product Name = "+pro_name);
}
public static void main(String args[]) {
TestClass[] obj = new TestClass[5] ;
obj[0] = new TestClass(23907,"Dell Laptop");
System.out.println("Product Object:");
obj[0].display();
}
}
abstract class Product {
int pro_Id;
String pro_name;
Product(int pid, String n) {
pro_Id = pid; pro_name = n;
}
abstract void display();
abstract void display1();
}

Output:
Error: The type TestClass must implement the inherited abstract method Product.display1().

After commenting the line “abstract void display1();”, following output will be generated:
Product Object:
Product Id = 23907 Product Name = Dell Laptop

9.4 What is the output of following code?

abstract class A {

abstract void myMethod(Number N);

interface B {

abstract void myMethod(Object O);

class C extends A implements B {

void myMethod(Number N) {
School of Computer Science Engineering and Technology
System.out.println("Number");

public void myMethod(Object O) {

System.out.println("Object");

public class TestClass {

public static void main(String[] args) {

A a = new C();

a.myMethod(Integer.valueOf(121));

B b = new C();

b.myMethod(Integer.valueOf(121));

C c = new C();

c.myMethod(Integer.valueOf(121));

Output:
Number
Object
Number

*Note: Please discuss about the hierarchy of Object class and Number Class.

9.5 What would be the result of the following code?

abstract class A {
int i = 111, j = 222;
abstract void methodOne();
abstract void methodTwo();
}
abstract class B extends A {
@Override
void methodOne() {
System.out.println(i);
School of Computer Science Engineering and Technology
System.out.println(j);
i = ++i;
j = --j;
}
}
class C extends B {
@Override
void methodTwo() {
System.out.println(i);
System.out.println(j);
i = i++;
j = j--;
}
}
public class TestClass {
public static void main(String[] args) {
C c = new C();
c.methodOne();
c.methodTwo();
System.out.println(c.i);
System.out.println(c.j);
}
}

Output:
111
222
112
221
112
221

*Note: Please discuss:


 Abstract classes can have both abstract and non-abstract methods.
 A concrete class must implement all abstract methods before it can be instantiated.
 Method execution order affects variable changes.

9.6 Will the code run successfully?

interface A {

int i = 111;

class B implements A {

void methodB() {

i = 222;

}
School of Computer Science Engineering and Technology

OUTPUT:
error: cannot assign a value to final variable i at i = 222;
*Note: Interface variables are implicitly public, static, and final (constants).

9.7 What will be the Output of the below code:

abstract class X {

int i = 111;

int methodX() {

return methodX(i);

abstract int methodX(int i);

class Y extends X {

@Override

int methodX(int i) {

return ++i + i++;

public class TestClass {

public static void main(String[] args) {

Y y = new Y();

System.out.println(y.methodX());

}
School of Computer Science Engineering and Technology

OUTPUT:
224

9.8 Is the below code correct?

class X {

abstract class Y {

class Z {

public class TestClass{

public static void main(String[] args) {

X x=new X();

Output:
Nothing will be displayed (As no print statement is there) but the code has no error.

PLEASE EXPLAIN THE QUESTION (A2) ASKED IN MID-SEM TEST IF


TIME PERMITS.

You might also like