Question: public class Animal{ private int age; // the only instance variable public Animal(){ // no-arg constructor this.age = 100; } public int getAge() {return this.age;}
public class Animal{ private int age; // the only instance variable public Animal(){ // no-arg constructor this.age = 100; } public int getAge() {return this.age;} // getter method for age public void setAge(int age) {this.age = age;} // setter method for age public void makeNoise(){ System.out.println("Beep"); } Public void makeNoise(int times){ System.out.println("Beep " + times + " times"); } } // end of class Animal public class Cat extends Animal{ public Cat() {} // no-arg constructor public Cat(int age){ // constructor with one input this.setAge( this.getAge() + age); } public void makeNoise(){ System.out.println("Meow"); } public void makeNoise(int times){ System.out.println("Meow " + times + " times"); } } // end of Cat class public class Lion extends Cat{ public Lion(int age){ super(age); } public void makeNoise(){ System.out.println("Roar"); } // end of Lion class
In a separate java file, assume that we have these codes inside of a main method. What is going to be printed?
Animal object1 = new Cat(); Animal object2 = new Lion(4); System.out.println(object1.getAge()); System.out.println(object2.getAge());
104, 4
100, 4
100, 104
104, 100
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
