Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

6. Classes and inheritance Consider the following classes : abstract class Animal { private String name; public Animal() { this( Animal ); } public Animal(

6. Classes and inheritance

Consider the following classes:

abstract class Animal {

private String name;

public Animal() { this( "Animal" ); }

public Animal( String name ) { this.name = name; }

public String toString() { return this.name; }

public abstract String speak();

}

class Cat extends Animal {

public Cat() { this( "Brina" ); }

public Cat( String name ) { super( name + " Cat" ); }

public String speak() { return "Meow"; }

public String speak( String name ) { return name + " Meow"; }

}

class Tiger extends Cat {

public Tiger( String name ) { super( name + " Tiger" ); }

public String speak() { return super.speak( "Jennifer" ); }

}

class BigTiger extends Tiger {

public BigTiger() { super( "Ko Ko" ); }

public BigTiger( String name ) { super( name ); }

public String speak() { return "Roar"; }

public String speak( String name ) { return name + " Roar"; }

}

final class Lion extends Cat {

public String speak() { return "Mo Lion " + super.speak(); }

public String softer() { return "Marjori " + super.speak(); }

}

What gets printed when this program is run?

public class Test14 {

public static void main( String[] args ) {

Animal a;

a = new Cat();

System.out.println( a + " says " + a.speak() );

a = new Lion();

System.out.println( a + " says " + ((Lion) a).softer() );

a = new BigTiger();

System.out.println( a + " says " + a.speak() );

a = new Tiger( "Max" );

System.out.println( a + " says " + a.speak() );

a = new BigTiger( "Zach" );

System.out.println( a + " says " + ((Cat) a).speak( "Big" ) );

}

}

Can we subclass/extend from Animal like this? State Yes or No. Then explain why or why not.

class Dog extends Animal {

public Dog() {

super( "Dog" );

}

public String speak( String name ) {

return name + " says Woof";

}

}

Can we subclass/extend from Lion like this? State Yes or No. Then explain why or why not. class CowardlyLion extends Lion {

public String toString() {

return "Courage " + super.toString();

}

}

Can we subclass/extend from Tiger like this? State Yes or No. Then explain why or why not. class LittleTiger2 extends Tiger {

public String speak() {

return "Little " + super.speak();

}

}

Can we subclass/extend from Cat like this? State Yes or No. Then explain why or why not.

class StrayCat extends Cat {

public String toString() {

return "Stray " + super.toString();

}

}

Can we subclass/extend from Tiger like this? State Yes or No. Then explain why or why not.

class LittleTiger extends Tiger {

public LittleTiger() {

super( "Little Tiger" );

}

public String speak( String name ) {

return name + super.speak();

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Advances In Databases 11th British National Conference On Databases Bncod 11 Keele Uk July 7 9 1993 Proceedings Lncs 696

Authors: Michael F. Worboys ,Anna F. Grundy

1993rd Edition

3540569219, 978-3540569213

More Books

Students also viewed these Databases questions