use java C.java Print.java yaway amp Lec3 InheriP.. Assignment4Fall2018- Class I
ID: 3755079 • Letter: U
Question
use java
C.java
Print.java
Explanation / Answer
--------------------------------------------------------
C.java
--------------------------------------------------------
import static Lab2.Print.*;
class A {
//Constructor:
A(){
println("A() construct in class A");
}
public void aMethod1() {
print(" aMethod1");
}
private void aMethod2() {
print(" Private aMethod2");
}
// this is a static method:
static void aMethod3() {
print(" static aMethod3 ");
}
}
class B extends A {
//Constructor:
B() {
println("B() extends A");
}
public void bMethod1() {
println(" bMethod1 ");
}
private void bMethod2() {
println(" private bMethod2 ");
}
}
public class C extends A { // class C extends class A as asked in the question.
C() {
println(" C() is class C extends B "); // This line will be printed whenever the constructor is called
}
C(boolean bool){ // constructor overloading. call this one to print aMethod1 and aMethod3.
println(" C() is class C extends B ");
b.aMethod1();
B.aMethod3();
}
B b = new B();
public static void main(String args[]) {
A a1 = new A();
B b1 = new B();
C c1 = new C();
C c2 = new C(true); // Calling the second constructor, this will print aMethod1 and aMethod3.
}
}
------------------------
output
-----------------------
A() construct in class A
A() construct in class A
B() extends A
A() construct in class A
A() construct in class A
B() extends A
C() is class C extends B
A() construct in class A
A() construct in class A
B() extends A
C() is class C extends B
aMethod1
static aMethod3
--------------------------
Print.java
-------------------------
package Lab2;
import java.io.*;
public class Print {
// Print with a newline:
public static void print(Object obj) {
System.out.println(obj);
}
// Print a newline by itself:
public static void print() {
System.out.println();
}
public static void println(Object obj) {
System.out.println(obj);
}
public static void println() {
System.out.println();
}
// Print with no line break:
public static void printnb(Object obj) {
System.out.print(obj);
}
// The new java SE5 printf() (from C):
public static PrintStream printf(String format, Object... args) {
return System.out.printf(format, args);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.