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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T02:52:48+00:00 2026-06-03T02:52:48+00:00

Say I have an abstract base class that has a method to generate Foo

  • 0

Say I have an abstract base class that has a method to generate Foo objects (acting like a factory method). Right now my classes look something like this:

public class Foo
{
}

public class FooBar extends Foo
{
}

public abstract class MyBaseClass
{
    abstract public Foo createObject();
}

public class MyDerivedClass extends MyBaseClass
{
    @Override
    public Foo createObject()
    {
        return new FooBar();
    }
}

This is sub-optimal, because code calling MyBaseClass#createObject needs to cast the return value back to what it was originally, i.e.:

FooBar fooBar = (FooBar)myDerivedClass.createObject();

This is like taking my pants off to put them on again.

I haven’t used Java generics for a while, but I was hoping I could turn that abstract method into a templated method, something like:

public abstract class MyBaseClass
{
    abstract public <T extends Foo> T createObject();
}

But I get the following errors from Eclipse when I try to implement the method in the derived class:

public class MyDerivedClass extends MyBaseClass
{
    @Override
    public <FooBar extends Foo> FooBar createObject() // The type parameter FooBar is hiding the type FooBar
    {
        return new FooBar(); // Cannot instantiate the type FooBar
    }
}

I don’t think I’m applying the right solution to my problem of reducing the unnecessary casting going on. Any ideas?

This is with Java 1.6.

  • 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-03T02:52:49+00:00Added an answer on June 3, 2026 at 2:52 am

    You’re probably looking for a generic class (as opposed to a generic method):

    public abstract class MyBaseClass<T extends Foo> {
        public abstract T createObject();
    }
    
    public class MyDerivcedClass extends MyBaseClass<FooBar> {
        public FooBar createObject() {
            return new FooBar();
        }
    }
    

    To learn more about Java generics, I highly suggest you read the Generics tutorial.

    • 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 an abstract base class that looks like this: class StellarObject(BaseModel):
Lets say I have an abstract base class with a pure virtual that returns
Say I have an abstract base class BaseClass . I'd like to give it
Lets say I have an abstract base class and a derived class with some
Let's say I have some classes like this: abstract class View(val writer: XMLStreamWriter) {
Say we have an abstract base class IBase with pure virtual methods (an interface).
Let's say I have an abstract parent class called shape, and that there are
Here's the situation. I have a base class which implements a method that accepts
Say you have an abstract base class Task which is a task a user
Okay, so let's say I have some random abstract base class Base 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.