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.
B b1 = new B();
will call the B class constructor and before executing that it will
call the A class constructor and prints A() called and comes back
to the B class constructor and prints B() called
B b3 = new B();
will call the B class constructor and before executing that it will
call the A class constructor and prints A() called and comes back
to the B class constructor and prints B() called
it will call foo2() in A and prints A version of foo2() called and returns 0 and comes back to foo2() in B and prints B version of foo2() called returns 5
System.out.println(b4.foo3());
it will call foo3() in class B and from there it will goto class
A and prints A version of foo3() called
and returns Hi and backs to foo3() in B and prints B version of
foo3() called and returns Hi foo3 prints
public class N extends String, Integer
{
}
In java we can't extend more than one class and String and Integer
classes are final we can't extend
Solution:
public class N
{
}
For Questions 1-3: consider the following code: public class A { private int number; protected String...
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 class Animal { private String name; //line 1 private int weight; //line 2 private String getName(){ return name; } //line 3 public int fetchWeight(){ return weight; } //line 4 } public class Dog extends Animal { private String food; //line 5 public void mystery(){ //System.out.println("Name = " + name); //line 6 System.out.println("Food = " + food); //line 7 } } I want to know the super...
class Upper { private int i; private String name; public Upper(int i){ name = "Upper"; this.i = i;} public void set(Upper n){ i = n.show();} public int show(){return i;} } class Middle extends Upper { private int j; private String name; public Middle(int i){ super(i+1); name = "Middle"; this.j = i;} public void set(Upper n){ j = n.show();} public int show(){return j;} } class Lower extends Middle { private int i; private String name; public Lower(int i){ super(i+1); name =...
Java Programming Questions (Multiple Choice) ** Please only answer if you know it, not just copy and paste from another user. Please explain the answer. 1) Given code: public abstract class A{ } public class B extends A { } The following code sequence would correctly create an object reference of class A holding an object reference for an object of class B: A c; c = new B(); TRUE FALSE ---------------------------------------------------------------------------------------------------------------------------------------- 2) After the following code sequence is executed,...
What is output? public abstract class People { protected string name; protected int age; public abstract void PrintInfo(); public void PrintInformation() { System.out.println("In Base Class People"); public class Teacher extends People { private int experience; public void PrintInfo() { System.out.println("In Child Class Teacher"); public class Principal extends Teacher { public void PrintInformation() { System.out.println("In Child Class Principal"); public static void main(String args[]) { Principal tim; tim = new Principal(); tim.PrintInfo(); In Base Class People Error: Compiler error In Child Class...
Question 19 Given the following class: public class Swapper ( private int x; private String y public int z; public Swapper( int a, String b, int c) ( x-a; y b; zC; public String swap() ( int temp -x; x-z z temp; return y: public String tostring() ( if (x<z) return y: else return" +x+z What would the following code output? Swapper r new Suapper( 5, "no", 10); System.out.printin( r. swap ) ): no no510 510 e 15 Question 20...
Here is the code for the Infant class: public class Infant{ private String name; private int age; // in months public Infant(String who, int months){ name = who; age = months; } public String getName(){ return name;} public int getAge(){ return age;} public void anotherMonth() {age = age + 1;} } The code box below includes a live Infant array variable, thoseKids. You cannot see its declaration or initialization. Your job is to find out which Infant in the array...
Java Do 72a, 72b, 72c, 72d. Code & output required. public class Employee { private int id; private String name; private int sal; public Employee(int id, String name, int sal) { super(); this.id = id; this.name = name; this.sal = sal; } public int getid) { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; public void setName(String name) { this.name = name; } public int get Sall) { return sal;...
What is the output of this program? class A { public int i; private int j; } class B extends A { void display() { super.j = super.i + 1; System.out.println(super.i + " " + super.j); } } class Inheritance { public static void main(String args[]) { B obj = new B(); obj.i=1; obj.j=2; obj.display(); } } Java language!! // include explanation!
List the unique classes/ADTs in the following code segment: (hint: there are 6) public class LinkedQueueclass protected class QueueNode int info; QueueNode link; private QueueNode queue Front; private QueueNode queue Rear; public class QueueOverflowException extends Queue Exception public QueueOverflowException() super("Queue Overflow"); public QueueOverflowException (String msg) super (msg); System.out.println("An exception occurred");