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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T22:39:35+00:00 2026-06-13T22:39:35+00:00

The following code: class Base{} class Agg extends Base{ public String getFields(){ String name=Agg;

  • 0

The following code:

class Base{}

class Agg extends Base{

   public String getFields(){
    String name="Agg";
    return name;
   }
}

public class Avf{
   public static void main(String args[]){
   Base a=new Agg();
   //please take a look here
   System.out.println(((Agg)a).getFields());  // why a needs cast to Agg? 

  }
}

My question is: why we can’t replace ((Agg)a).getFields() to a.getFields()? Why we need to type cast on a? And I mention that getFields() is not defined in class Base, thus class Agg does not extend this method from its base class. But if I defined method getFields() in class Base,
like:

class Base{
    public String getFields(){
        String name="This is from base getFields()";
        return name;
    }
}

everything would be all right. Then
((Agg)a).getFields() is equivalent to a.getFields()

In the code

Base a=new Agg();    

Does this line means a has the reference of Agg() and a can invoke directly the method of class Agg. But why is there difference if I do not define the method getFields() in class Base? Can any one explain this to me?

  • 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-13T22:39:36+00:00Added an answer on June 13, 2026 at 10:39 pm

    System.out.println(((Agg)a).getFields()); // why a needs cast to Agg?

    Because Base doesn’t have a method called getFields.

    The declaration

    Base a;
    

    …tells the compiler the interface you’re going to use when using a. It doesn’t matter that you then assign new Agg(); to it, the interface you have to a is still Base. And since Base doesn’t have getFields, trying to use getFields is a compilation error.

    Base a=new Agg();

    Does this line means a has the reference of Agg() and a can invoke directly the method of class Agg.

    It means a contains a reference to an Agg object, but the interface to that object is defined by Base, not Agg, and so you can’t call Agg methods on it if they’re not defined by Base (without a cast, and casts are dangerous and should be avoided where possible).

    If your code needs access to methods or fields defined by Agg, then the variable (or argument, in my example) should be declared Agg a, not Base a.

    This is a fundamental thing about OOP in general and the OOP implementation in Java in particular: There’s a big difference between the interface to an object and the implementation of the object. In your example, you know Base a is really an Agg, but consider this:

    void foo(Base a) {
        // Use `a` here...
    }
    
    // A long time later in a class far, far away...
    xyz.foo(new Agg());
    

    Clearly here the compiler shouldn’t allow you to use methods or fields on a that aren’t defined by Base. It’s the same situation in your code, just less obvious.

    • 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: class Test { public static void main(String[] args) {
i have written the following code class Program { static void Main(string[] args) {
Consider the following sample code: class Base { public: void f(); virtual void vf();
Consider the following code: public abstract class Base { public void getAnswer(); } public
Imagine the following code: class foreach_convert { public static void method2() { List<IComparable> x
Consider the following code: class Base { public: virtual void Foo() {} }; class
Consider the following code: class Base { void f() { } }; class Derived:
Consider the following code: template<int* a> class base {}; int main() { base<(int*)0> test;
Lets say I have the following code: public class Base { // Some stuff
In the following code: class Base { void funcA(); void funcB(); . . };

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.