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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:20:06+00:00 2026-05-28T03:20:06+00:00

I have the following problem. Am trying to make a polymorphic call and the

  • 0

I have the following problem. Am trying to make a polymorphic call and the result would depend on the variable that changes value depending on the underlying class. Tried different things however it doesn’t work. Please let me know what should be changed. Problem is that although c.w reads both the local variable w, which is defaulted to 0 and reads the one from appropriate class it always defaults to 0. Here is the code:

class Cycle{
    private int w = 0;
    public void move(){
        System.out.println("Cycle moving");
    }
    public int wheels(Cycle c){
        switch (c.w){

            case 1:     return 1; 
            case 2:     return 2;
            case 3:     return 3;
            default:    return 0;
        }
    }
}
class Unicycle extends Cycle{
    public int w = 1;
    public void go(){
        System.out.println("Unicycle go");
    }
}
class Bicycle extends Cycle{
    public int w = 2;
    public void go(){
        System.out.println("Bicycle go");
    }
}
class Tricycle extends Cycle{
    public int w = 3;
    public void go(){
        System.out.println("Tricycle go");
    }
}

public class TestCycle {

    public static void ride(Cycle c){
        c.move();
        int now = c.wheels(c);
        System.out.println(now);

    }

    public static void main(String[] args){
        Bicycle b = new Bicycle();
        ride(b);
        Unicycle u = new Unicycle();
        ride(u);
        Tricycle t = new Tricycle();
        ride(t);
    }

}
  • 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-28T03:20:07+00:00Added an answer on May 28, 2026 at 3:20 am

    Your problem (well one of them) is that you are redefining the class variable ‘w’ in each of your subclasses. Define it one as a member of ‘Cycle’ and have each subclass set it correctly in their constructors.

    class Cycle{
        protected int w;
        public void move(){
            System.out.println("Cycle moving");
        }
        public int wheels(){
            return w;
        }
    }
    
    class Unicycle extends Cycle{
        public Unicycle() {
            w = 1;
        }
         public void go(){
            System.out.println("Unicycle go");
        }
    }
    

    Or you can define an abstract method called ‘wheels()’ in the superclass and override it in the subclasses. It’s a matter of taste.

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

Sidebar

Related Questions

I have the following problem: I am trying to make 3 links/anchors (with padding
I'm trying to solve the following problem: I have a download button that when
Trying to use JSTL but have the following problem: Index.xhtml page: <?xml version=1.0 encoding=UTF-8?>
I have a bit of a problem. I am trying to do the following
I'm trying to solve the following problem: Say I have a Python script (let's
Recently I have been trying to make changes so I can do builds through
I have a problem trying the following with nHibernate: Eager loading. Use selects (here
I have written following code to make a call also have set permissions in
I have the following problem. I inherited an app and I am trying to
I have a container object that is templatized. I am trying to make 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.