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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T07:35:59+00:00 2026-06-10T07:35:59+00:00

I am struggling to understand Variables Shadowed Methods Overriden Concept of inheritance with Java.

  • 0

I am struggling to understand Variables Shadowed Methods Overriden Concept of inheritance with Java.

Case 1:

class Car{
   public int gearRatio = 8;
   public String accelerate() {  return "Accelerate : Car";  }
}
class SportsCar extends Car{
   public int gearRatio = 9;
   public String accelerate() {  return  "Accelerate : SportsCar";  }
   public static void main(String[] args){
      Car c = new SportsCar();
      System.out.println( c.gearRatio+"  "+c.accelerate() );
   }
}

Output: 8 Accelerate : Sportscar.

Case 2:

public class TestClass{
   public static void main(String args[ ] ){
      A o1 = new C( );
      B o2 = (B) o1;
      System.out.println(o1.m1( ) );
      System.out.println(o2.i );
   }
}
class A { int i = 10;  int m1( ) { return i; } }
class B extends A { int i = 20;  int m1() { return i; } }
class C extends B { int i = 30;  int m1() { return i; } }

Output: 30, 20

So if I understand correctly, the super class variable is always called unless the sub classes variable is called explicitly. But the opposite is true for methods where the sub classes overridden method is called unless the super class is called explicitly.

I would think variables and methods should work the same or there should be a compile error when creating sub classes with the same variables.

Can someone explain if this is correct and why java works like this please.

  • 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-10T07:36:01+00:00Added an answer on June 10, 2026 at 7:36 am

    I would think variables and methods should work the same or there should be a compile error when creating sub classes with the same variables.

    Well, that’s simply not the way Java works.

    Variables are not handled polymorphically – there’s no concept of “overriding” a variable. Methods, however, are handled polymorphically. Behaviour can be specialized, but not state.

    Note that if your variables are private, as they almost always should be, the situation is never even visible.

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

Sidebar

Related Questions

I am using Java with SQLiteJDBC . I was struggling to understand why my
I'm currently struggling to understand how I should organize/structure a class which I have
I'm struggling to understand why nothing is output using the following: class Program {
I am struggling to understand on java threads work so excuse this rather simple
I´m struggling to understand this concept: I have a fixed size definition: (from http://msdn.microsoft.com/pt-br/library/aa931918.aspx
I am struggling to understand why my instance variables not being saved. Whenever I
I'm struggling to understand how to combine the TimePicker http://trentrichardson.com/examples/timepicker/ solution to my existing
I'm really struggling to understand cocos2d 2.0 and how its layers and coordinates work.
I am really struggling to understand why, when I change my code to use
I'm struggling to understand what precisely does it mean when a value has type

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.