Results 1 to 2 of 2
  1. #1
    jmohandos304 is offline Senior Member
    Join Date
    Apr 2014
    Rep Power

    Default Unable to understand

    In the following code the method is overridden, but what about the variables. Why are they not overridden? Can somebody explain the behaviour?

    Java Code:
    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() );
    8 Accelerate : SportsCar

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Rep Power

    Default Re: Unable to understand

    Attributes are hidden, not overridden.
    One of the reasons to:
    a) make attributes private and try not to let them leak out in to the outside world.
    b) try not to create attributes that have the same name as a parents attribute.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Replies: 2
    Last Post: 07-24-2014, 09:29 AM
  2. Unable to download maven repository - Unable to load the mojo
    By tejkiransharma in forum Apache Maven
    Replies: 0
    Last Post: 12-31-2012, 03:44 PM
  3. GUI help. Don t understand
    By s0meb0dy in forum AWT / Swing
    Replies: 2
    Last Post: 10-27-2010, 09:40 PM
  4. I don´t understand
    By Manikyr in forum New To Java
    Replies: 6
    Last Post: 02-22-2009, 11:22 PM
  5. Replies: 2
    Last Post: 12-22-2008, 03:05 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts