• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Overriding and Polymorphism Question, Whizzlabs 11 of 15 on sample test

Robert Elbourn
Ranch Hand
Posts: 69
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class SuperBase { void print(SuperBase a){System.out.println("SuperBase");}}
class Based extends SuperBase { void print(Based a){System.out.println("Based");}}
class Derived extends Based {static void print(Derived a){System.out.println("Derived");}}

public class SuperBaseTest {

public static void main(String[] args) {

SuperBase a1= new SuperBase();
SuperBase a2 = new Based();
SuperBase a3 = new Derived();

Based b2 = new Based();
Based b3 = new Derived();

Derived d3 = new Derived();

a1.print(new SuperBase());//SuperBase
a1.print(new Based());//SuperBase
a1.print(new Derived());//SuperBase

a2.print(new SuperBase());//SuperBase
a2.print(new Based()); //SuperBase
a2.print(new Derived()); //SuperBase

a3.print(new SuperBase()); //SuperBase
a3.print(new Based());//SuperBase
a3.print(new Derived());//SuperBase

b2.print(new SuperBase()); //SuperBase
b2.print(new Based()); //Based
b2.print(new Derived()); //Based

b3.print(new SuperBase());//SuperBase
b3.print(new Based()); //Based
b3.print(new Derived()); //Based

d3.print(new SuperBase()); //SuperBase
d3.print(new Based()); //Based
d3.print(new Derived()); //Derived


I have created this compilable class( Base is now Based as I had a class already defined that I couldn't be bothered to package differently)
this prints out all the valid permutations and combinations that the question could have to offer (I think)
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic