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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T07:38:42+00:00 2026-05-29T07:38:42+00:00

Suppose I have the following classes: public class Test { public static void main(String[]

  • 0

Suppose I have the following classes:

public class Test { 

    public static void main(String[] args) {
        PetrolCar myCar = new PetrolCar();
        String mileage = myCar.getEngine().getMileage();

    }
}

class Engine {
    protected String var = "Engine";

    protected String getVar() {
        return this.var;
    }
}

class PetrolEngine extends Engine {
    protected String var = "PetrolEngine";
    protected String mileage = "0";

    PetrolEngine() {
        super();
        mileage = "100";
    }

    protected String getVar() {
        return this.var;
    }

    protected String getMileage() {
        return mileage;
    }
}

class Car {
    protected Engine engine;

    protected Engine getEngine() {
        return engine;
    }
}

class PetrolCar extends Car {
    protected PetrolEngine engine;
}

Obviously myCar.getEngine().getMileage() will not work because getEngine() will return an Engine instance, not a PetrolEngine instance. What is the workaround for this? I don’t want to redefine getEngine() in all subtypes of Car, at the same time I would like to get back the more specific type PetrolEngine when I call getEngine() on an instance of PetrolCar, without having to type cast. Is this possible?

In other words, is there a way I can associate the Engine of PetrolCar to be a PetrolEngine? In the above case, it is creating two separate instance variables engine, one of PetrolEngine type inside PetrolCar, and another Engine, which I can access as super.getEngine() from inside PetrolCar. I don’t want different variables. What I would like is that the code should know that the “Engine” of “PetrolCar” is a PetrolEngine.

EDIT: Changed MyCar to PetrolCar to avoid confusion which is leading everyone to believe that MyCar should be an instance of Car rather than a subtype.

  • 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-05-29T07:38:43+00:00Added an answer on May 29, 2026 at 7:38 am

    Yes you can. They call it covariant return (starting from Java 1.5):

    public class Test {
    
        public static void main(String[] args) {
    
            MyCar car = ...;
            car.getEngine().getMileage();
        }
    }
    
    interface Car {
        Engine getEngine();
    }
    
    interface MyCar extends Car {
        PetrolEngine getEngine();
    }
    
    interface Engine {
    }
    
    interface PetrolEngine extends Engine {
        String getMileage();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have following code package memoryleak; public class MemoryLeak { public static int
Suppose I have two classes defined the following way: public class A { public
Suppose we have following two classes: class Temp{ public: char a; char b; };
The scenario Suppose I have the following two model classes: public class ProductColor {
Suppose I have the following classes: class X; class Y; class Collection { public:
Suppose I have following string: String asd = this is test ass this is
Suppose that we have the following base and derived classes: #include <string> #include <iostream>
Suppose i have class Person { public int Id {get;set;} public string Name {get;set;}
Suppose that I have the following HBM mapping: <class name=Student table=student> <set name=classes table=student_classes
Suppose I have two C++ classes: class A { public: A() { fn(); }

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.