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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T12:53:35+00:00 2026-06-12T12:53:35+00:00

In Java I have an abstract base class, let’s say WrapX , which contains

  • 0

In Java I have an abstract base class, let’s say WrapX, which contains a property of a type, say X (think Decorator DP). This class presents a method to call a specific method on the encapsulated X:

public abstract class WrapX{

  protected X instance;

  public WrapX(X x){
     this.instance = x;
  }

  public void foo(){
       instance.foo();
  }

}

public class X {

   public void foo(){
       System.out.println("foo");
   }
}

There is then a class called Y that extends from X and provides an additional method:

public class Y extends X {

  public void bar(){
     System.out.println("bar");
  }
}

Then naturally I have created WrapY that can be used as a decorated type over the type Y:

public class WrapY extends WrapX{

  ...

  public void bar(){
    instance.bar();
  }

}

So herein lies the issue. WrapY has inherited the instance property of type X from its parent WrapX. As far as Eclipse is concerned, instance is of type X and so will complain that it contains no method .bar().

Fair enough of course, but how then in this subclass can we implicitly cast the instance to an instance of Y (a valid subclass of the initial type X)… WITHOUT the need for explicit cast ascriptions littering the code, or variable shadowing?

If I just had this in the constructor:

public WrapY(Y y){
 this.instance = y;
}

Eclipse still complains that .bar() is not a method of type X because I guess it cannot infer for certain that WrapY(Y y) will be used prior to construct the WrapY instance:

public void bar(){
        instance.bar(); // ERROR
      }

Here is the current approach I have, littered with casts:

public WrapY(Y y){
 (Y)instance = y;
}

public void bar(){
  ((Y)instance).bar();
}

I haven’t come across this particular type of architectural problem in my experience before, file it under ‘Decorator-Based-Inheritance-Type-Casting'(!)… Please enlighten me as to how I can model this in a better way.

Another issue is that, if in future someone extends WrapY, the type of instance their class inherits will be the natural (uncasted) type of X, when they may reasonably assume it should be of type Y.

Thanks

  • 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-12T12:53:36+00:00Added an answer on June 12, 2026 at 12:53 pm

    You could make your Wrap class generic, for example:

    public abstract class Wrap<T extends X>{
    
        protected T instance;
    
        public Wrap(T x){
            this.instance = x;
        }
    
        public void foo(){
           instance.foo();
        }
    }
    
    
    
    public final class WrapY extends Wrap<Y> {
    
        public WrapY(Y y) {
            super(y);
        }
    
        public void bar(){
            instance.bar();
        }
    }
    

    Then for instances of WrapY, instance will be a Y.


    Update:
    If you want to inherit from WrapY, too (and address your last issue of the wrapped type being the most appropriate), do:

    public class WrapY<U extends Y> extends Wrap<U> {
    
        public WrapY(U y) {
            super(y);
        }
    
        public void bar(){
            instance.bar();
        }
    }
    
    • 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 a Java class abstract class Base { abstract void init();
I have several Abstract Base Classes which act like interfaces as known from Java
I have an abstract base class T , from which classes A and B
Java - I have a abstract base class and I want to set few
Say I have an abstract base class that has a method to generate Foo
Hi I'm implementing a given design in java. Basically I have an abstract class
I'm looking at some Java classes that have the following form: public abstract class
I have an abstract base class that many classes extend. I'd like all these
I have an abstract base class class IThingy { virtual void method1() = 0;
I have a bunch of classes extending an abstract Base class. Each subclass takes

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.