This assignment is an individual assignment.
For Questions 1-3: consider the following code:
public class A
{
private int number;
protected String name;
public double price;
public A()
{
System.out.println(“A() called”);
}
private void foo1()
{
System.out.println(“A version of foo1() called”);
}
protected int foo2()
{
Sysem.out.println(“A version of foo2() called);
return number;
}
public String foo3()
{
System.out.println(“A version of foo3() called”);
Return “Hi”;
}
}//end class A
public class B extends A
{
private char service;
public B()
{
super();
System.out.println(“B() called”);
}
public void foo1()
{
System.out.println(“B version of foo1() called”);
}
protected int foo2()
{
int n = super.foo2();
System.out.println(“B version of foo2() called”);
return (n+5);
}
public String foo3()
{
String temp = super.foo3();
System.out.println(“B version of foo3()”);
return (temp+” foo3”);
}
}//end class B
public class C extends B
{
public C()
{
super();
System.out.println();
}
public void foo1()
{
System.out.println(“C version of foo1() called”);
}
}//end class C
Assignment
B b1 = new B();
B b3 = new B();
int n = b3.foo2();
//b4 is a B object reference
System.out.println(b4.foo3());
public class N extends String, Integer
{
}
When you compile, you get the following message:
N.java:1: ‘{‘ expected
public class N extends String, Integer
^
1 error
Explain what the problem is and how to fix it.
Hi,
The following is the code for the current assignment. I have explained the output along with the comments.
If you have any questions or issues with the solution, please leave me a comment. I shall help you in resolving them.
Thank you.
Q1
Output is
A() called
B() called
As "A" is the super class of "B", the constructor of the super class is invoked in B's constructor - super() and hence, "A() called" gets display first and then the "B() called" in the B's constructor is printed
Q2
Output is
A() called
B() called
A version of foo2() called
B version of foo2() called
For first 2 lines in output, the explanation is same as the Q1. The next two lines get printed as the foo2() method in B class is invoked. In that method, the super class ie. A's foo2() is invoked and hence the next two lines are printed
Q3.
Output is
A() called
B() called
A version of foo3() called
B version of foo3()
Hi foo3
For explanation for the first 2 lines refer to Q1, the next lines are printed from foo3 method in A class and then the line in B's foo3 method.
The last line, the "Hi" is returned from the super class method ie A is appended with a string literal ("foo3") in B class and printed.
Q4. The class definition of Class N extends multiple class - String and Integer. However Java doesnt supports classes to extend from Multiple Super classes. Hence, the syntax error is reported.
To fix the problem, you can include 2 super class as member in a new class and then extends that class with class N.
The other solution is to use Interfaces.
//Code
//A.java
public class A
{
private int number;
protected String name;
public double price;
public A()
{
System.out.println("A()
called");
}
private void foo1()
{
System.out.println("A version of
foo1() called");
}
protected int foo2()
{
System.out.println("A version of
foo2() called");
return number;
}
public String foo3()
{
System.out.println("A version of
foo3() called");
return "Hi";
}
}//end class A
//B.java
public class B extends A
{
private char service;
public B()
{
super();
System.out.println("B()
called");
}
public void foo1()
{
System.out.println("B version of
foo1() called");
}
protected int foo2()
{
int n = super.foo2();
System.out.println("B version of
foo2() called");
return (n+5);
}
public String foo3()
{
String temp = super.foo3();
System.out.println("B version of
foo3()");
return (temp+ " foo3");
}
}//end class B
//C.java
public class C extends B
{
public C()
{
super();
System.out.println();
}
public void foo1()
{
System.out.println("C version of
foo1() called");
}
}//end class C
//Class N
//Here N extends new class - Z which includes String and Integer as
members
public class N extends Z {
public N() {
s = "hello";
i = 10;
}
}
//Class Z
public class Z {
protected String s;
protected Integer i;
}
//TestApp.java
public class TestApp {
public static void main(String[] args) {
System.out.println("Q1 ->
");
B b1 = new B();
System.out.println("Q2 -> ");
B b3 = new B();
int n = b3.foo2();
System.out.println("Q3 ->
");
//b4 is a B object reference
B b4 = new B();
System.out.println(b4.foo3());
}
}
Get Answers For Free
Most questions answered within 1 hours.