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

  • Home
  • SEARCH
  • 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 8302587
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T17:19:44+00:00 2026-06-08T17:19:44+00:00

When a class extends another, it inherits all methods and variables of the superclass.

  • 0

When a class extends another, it inherits all methods and variables of the superclass. Both methods and variables can be used differently in the subclass, if you define it differently in the subclass with the same signature.
Now Oracle distincts between overwriting and hiding (http://docs.oracle.com/javase/tutorial/java/IandI/override.html).
It says that an instance method overwrites its superclass’s method, while a class method hides it.
“The distinction between hiding and overriding has important implications. The version of the overridden method that gets invoked is the one in the subclass. The version of the hidden method that gets invoked depends on whether it is invoked from the superclass or the subclass.”

Lets assume I have 2 classes Yes and Maybe. Yes extends Maybe.
Maybe has String a.

class Maybe {
    String a;
    public static void printOut() {
        System.out.println("Maybe");
    }
    public void printAndSet() {
        a = "Maybe";
        System.out.println(a);
    }

}
class Yes extends Maybe {
    public static void printOut() {
        System.out.println("Yes");
    }
    pubilc void printAndSet() {
         a = "Yes";
    }
}
class Print{
    public static void mail(String[] args) {
        Maybe m = new Maybe();
        Yes y = new Yes();
        Maybe.printOut();
        Yes.printOut();
        m.printAndSet();
        y.printAndSet();
}

And I say: It will print out
maybe
yes
maybe
yes

But after I read the Oracle article I thought it would have to print out:

yes
yes
maybe
yes

Because the instance method overwrites its superclass method.

I’m quite sure I am right with the output, but I’m sure aswell, that Oracle knows
better so I’m thinking I just didn’t understand the article.
It can’t be true that when I call an instance method from an object of a superclass, that it uses the overwritten method.
So I do not understand why to distinguish overwriting and hiding!
Can someone help out?

Edit; Inserted code instead of describing the classes!

  • 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-08T17:19:45+00:00Added an answer on June 8, 2026 at 5:19 pm

    Static methods can’t be overridden at all. They’re not called polymorphically, since they don’t act on an instance of the class, but on the class itself.

    If you call Maybe.printOut(), it will call the static printOut() method defined in Maybe. The fact that there is also a method printOut() defined in Yes is irrelevant: those two methods have nothing in common, except their name.

    Note that you could confirm or infirm your doubts by simply writing a program and executing it.

    The problem with hiding methods only occurs when you start calling static methods on an instance of an object. This is very bad practice, and should never be done. If you don’t respect this rule, and have the following:

    Maybe m = new Maybe();
    Maybe y = new Yes();
    
    m.printOut(); // DON'T DO THAT: it should be Maybe.printOut();
    y.printOut(); // DON'T DO THAT: it should be Maybe.printOut() or Yes.printOut();
    

    the result will be maybe maybe, because in the case of static methods, what counts is not the concrete type of the objects (Maybe and Yes), but their declared type (Maybe and Maybe).

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

Sidebar

Related Questions

In Java, you can use instanceof to check if a class extends another class
i have a class that extends another. when i iterate the current object i
I have a class extends NamedParameterJdbcDaoSupport. well this superclass has a final setDataSource method
I used Cursor in a class extends BaseAdapter, class NewDefinedAdapter extends BaseAdapter { private
Is there a way in PHP to know if one class inherits another? class
I have a class called Node and another class called ClassicNode which extends Node.
I have the following situation. I have a Java Class that inherits from another
How do you test to see if a class extends another class by name?
I have class that extends another class, that extend another class.. and so on.
I have a class that extends generic class that also extends (another) generic class.

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.