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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T23:21:27+00:00 2026-06-08T23:21:27+00:00

Let’s say you have some Java code as follows: public class Base{ public void

  • 0

Let’s say you have some Java code as follows:

 public class Base{
    public void m(int x){
    // code
    }
 }

and then a subclass Derived, which extends Base as follows:

 public class Derived extends Base{
    public void m(int x){ //this is overriding
      // code
    }

    public void m(double x){ //this is overloading
      // code
    }
 }

and then you have some declarations as follows:

Base b = new Base();
Base d = new Derived();
Derived e = new Derived();

b.m(5); //works
d.m(6); //works
d.m(7.0); //does not compile
e.m(8.0); //works

For the one that does not compile, I understand that you are passing in a double into Base’s version of the m method, but what I do not understand is… what is the point of ever having a declaration like “Base b = new Derived();” ?

It seems like a good way to run into all kinds of casting problems, and if you want to use a Derived object, why not just go for a declaration like for “e”?

Also, I’m a bit confused as to the meaning of the word “type” as it is used in Java. The way I learned it earlier this summer was, every object has one class, which corresponds to the name of the class following “new” when you instantiate an object, but an object can have as many types as it wants. For example, “e” has type Base, Derived, (and Object 😉 ) but its class is Derived. Is this correct?

Also, if Derived implemented an interface called CanDoMath (while still extending Base), is it correct to say that it has type “CanDoMath” as well as Base, Derived, and Object?

  • 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-08T23:21:29+00:00Added an answer on June 8, 2026 at 11:21 pm

    There are a number of cases where confining yourself to a particular (sub)class is not desired, such as the case you have where e.m(8.0);. Suppose, for example, you have a method called move that moves an object in the coordinate graph of a program. However, at the time you write the method you may have both cartesian and radial graphs, handled by different classes.

    If you rely on knowing what the sub-class is, you force yourself into a position wherein higher levels of code must know about lower levels of code, when really they just want to rely on the fact that a particular method with a particular signature exists. There are lots of good examples:

    • Wanting to apply a query to a database while being agnostic to how the connection is made.
    • Wanting to authenticate a user, without having to know ahead of time the strategy being used.
    • Wanting to encrypt information, without needing to rip out a bunch of code when a better encryption technique comes along.

    In these situations, you simply want to ensure the object has a particular type, which guarantees that particular method signatures are available. In this way your example is contrived; you’re asking why not just use a class that has a method wherein a double is the signature’s parameter, instead of a class where that isn’t available. (Simply put; you can’t use a class that doesn’t have the available method.)

    There is another reason as well. Consider:

    class Base {
       public void Blah() {
         //code
       }
    }
    
    class Extended extends Base {
       private int SuperSensitiveVariable;
    
       public setSuperSensistiveVariable(int value) {
         this.SuperSensistiveVariable = value;
       }
    
       public void Blah() {
         //code
       }
    }
    
    //elsewhere
    Base b = new Extended();
    Extended e = new Extended();
    

    Note that in the b case, I do not have access to the method set() and thus can’t muck up the super sensitive variable accidentally. I can only do that in the e case. This helps make sure those things are only done in the right place.

    Your definition of type is good, as is your understanding of what types a particular object would have.

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

Sidebar

Related Questions

Let's say I have the following classes : public class MyProductCode { private String
Let say I have some code HTML code: <ul> <li> <h1>Title 1</h1> <p>Text 1</p>
Let's imagine I have a Java class of the type: public class MyClass {
Let's say I have some json like this in mongo: {n:5} and a java
Let's say I have 2 functions: void function1(int *ptr) { printf(%d, *ptr); } and
Let's say I have a class Base with a single member message (String). Another
Let's say I have a method in java, which looks up a user in
Let me explain best with an example. Say you have node class that can
Let's say on a page I have alot of this repeated: <div class=entry> <h4>Magic:</h4>
Let's say I have this code: <p dataname=description> Hello this is a description. <a

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.