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 8435149
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T06:50:37+00:00 2026-06-10T06:50:37+00:00

I have this code form the book SCJP: 1. class Mammal { 2. String

  • 0

I have this code form the book SCJP:

1.  class Mammal {
2.    String name = "furry ";
3.    String makeNoise() { return "generic noise"; }
4.  }
5.  
6.  class Zebra extends Mammal {
7.    String name = "stripes ";
8.    String makeNoise() { return "bray"; }
9.  }
10.
11. public class ZooKeeper {
12.   public static void main(String[] args) {
13.     new ZooKeeper().go();
14.   }
15.   
16.   void go() {
17.     Mammal m = new Zebra();
18.     System.out.println(m.name + m.makeNoise());
19.   }
20. }

The result from running this code is “furry bray”.

Question 1

I don’t understand why line 17 is not :
Zebra zebra2 = new Zebra();

What is the purpose in each of the following cases, when to use which?

Mammal zebra1 = new Zebra();

vs

Zebra zebra2 = new Zebra();   

Question 2

Why is the variable name = "stripes" from the Zebra class overridden by the name = "furry" from the Mammal class? I expect the opposote: the variable from subclass will override that from superclass.

  • 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-10T06:50:38+00:00Added an answer on June 10, 2026 at 6:50 am

    Answer 1: Depends on what you want. In this case it is not that obvious. But sometimes you just want an object to be off class 1 or class 2. imagine you have a method in zebra and not in mammal and you want tot use that method, then your object needs to be off class zebra and not mammal. But if there is a third-party class with a method only accepting mammal variables, then you must pass a mammal object.

    Answer2: m.name is the variable of mammal (cannot be overridden) and m.makeNoise() is a method and this is overriden in Zebra.

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

Sidebar

Related Questions

I have the following code: echo ' <td> <input type=button name=delete value=X onclick=clearSelection(this.form, '.$type.');this.form.submit();
I have this code in my cfm, which works <cfif not StructIsEmpty(form)> <cfset larray
I have this form submit code: Event.observe(window, 'load', init, false); function init() { Event.observe('addressForm',
I currently have this useful code that I found elsewhere on StackOverflow: form.DrawToBitmap(bmp, new
Hello i have this form filling javascript: function onLine(code,nn) { document.writeform.bericht.value+=code; document.writeform.bericht.focus(); document.writeform.nickname.value+=nn; write1();
I have my form in a table and I use this code to add
I have a delphi 7 form: and my code: when I run this form
I have this code book_model_delete = { 'model' : Book, 'post_delete_redirect': /boo/list/, template_name :
I have implemented the code to get contact list form address book. For that
I have just started to learn ASP.NET MVC 3 form this book and I

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.