Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8964683
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T16:38:39+00:00 2026-06-15T16:38:39+00:00

I have created the following puzzle for inheritance in Java: Animal.java public class Animal

  • 0

I have created the following puzzle for inheritance in Java:

Animal.java

public class Animal {
    private String sound;

    public void roar() {
        System.out.println(sound);
    }

    public void setSound(String sound) {
        this.sound = sound;
    }
}  

Tiger.java

public class Tiger extends Animal {
    public String sound;

    public Tiger() {
        sound = "ROAR";
    }
}

Jungle.java

public class Jungle {
    public static void main(String[] args) {
        Tiger diego = new Tiger();

        diego.roar();
        diego.sound = "Hust hust";
        diego.roar();
        diego.setSound("bla");
        diego.roar();
        System.out.println(diego.sound);
    }
}

Output:

null
null
bla
Hust hust

I guess this weird behaviour is taking place, because sound in Animal is private while sound in Tiger is public. But can you explain (and tell me the relevant parts of the JLS) why this happens?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T16:38:40+00:00Added an answer on June 15, 2026 at 4:38 pm

    Fields are not polymorphic, methods are polymorphic.

     diego.roar();
    

    calls roar() method in Animal and prints sound from Animal.

    diego.sound = "Hust hust";
    

    Sets sound value in Tiger class sound variable

    diego.roar();
    

    returns null; because prints sound from Animal, which is still null. Above sound assignment reflects on Tiger class variable, not Animal class.

    diego.setSound(“bla”);

    sets Animal sound to bla

    diego.roar();
    

    prints bla because setSound update sound variable of Animal class with bla.

    System.out.println(diego.sound);

    prints Hust hust due to the fact that diego is of type Tiger and you have accessed field sound of Tiger and fields are not polymorphic.

    Please refer java language specification 8.3 for more details.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In Java I have created the following class which extends ImageView to create a
I have created the following function for checking the connection status: private void checkConnectionStatus()
I have created the following class: using System.Windows; using System.Windows.Input; using MyUtils.MyArgParser; namespace MyUtils.MyImplement
I have created following class: public static class Current { public static class User
I have created the following class class Contact def initialize(id, name, phone) @id =
I have created the following custom tooltip with the custom template. <ToolTip x:Class=FireFly.Controls.CustomToolTip xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation
I have created following adapter: package org.virtualbet.gui.util; import java.util.List; import org.virtualbet.R; import org.virtualbet.model.Tip; import
I'm writing an MVC2 app using DataAnnotations. I have a following Model: public class
I have created the following class that extends JSpinner to iterate over dd/mm/yyy values.
I have created following rule in the .htaccess file located at the root of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.