• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • paul wheaton
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Tim Holloway
  • Carey Brown
  • salvin francis

Super-Sub classes

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everybody,
Question from JQplus:
class Car
{
public int gearRatio = 8;
public String accelarate() { return "Accelarate : Car"; }
}
class SportsCar extends Car
{
public int gearRatio = 9;
public String accelarate() { return "Accelarate : SportsCar"; }

public static void main(String[] args)
{
Car c = new SportsCar();
System.out.println( c.gearRatio+" "+c.accelarate() );
}
}
The object "c" will inherit fields from Car (Super class), and methods from SportsCar (Sub). Am I right? Can somebody explain if I am not?
Thanks.
 
Ranch Hand
Posts: 2373
MySQL Database Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

U r absolutely right Igor!
The code snippet given tests the knowledge of Inheritance along with dynamic method lookup and variable shadowing. When extending a class if u declare new variable with the same name in the subclass as was in super class, the inherited super class variable will be shadowed by the new subclass variable with same name. U can also override methods. But when u r creating an object of subclass with a reference type of super class here comes the issue!
Using references of superclass will actually use the superclass variable but the correct methods will be chosen "in runtime" checking the actual type of the object.
I recommend u to go through the Inheritance chapter of Khalid Mughal in this respect. Hope it helps u.

------------------
azaman
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I think your Q. is about method overriding.As instance variables
can not ovrerridden,It is the class type of obj.Ref which determines
instance variable it refers.
So,c.gearRatio will give value 8.
For method Overriding,It is type of Object(Here new SportsCar())
detdetermines instance method should be called.So method in class SportsCar will be invoked.
 
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just modify your code like this i.e. making your methods static. See how this change the o/p
================================================================
class Car
{
public int gearRatio = 8;
public static String accelarate() { return "Accelarate : Car"; }
}
class SportsCar extends Car
{
public int gearRatio = 9;
public static String accelarate() { return "Accelarate : SportsCar"; }

public static void main(String[] args)
{
Car c = new SportsCar();
System.out.println( c.gearRatio+" "+c.accelarate() );
}
}
================================================================
 
Igor Gaschits
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks everyone for responding to my message. Muhammad, you defragmented some clusters in my brain.
Now another issue. I read Quick Tips from Certification Resources at http://www.javacommerce.com/certify/tips.html, and it sais:
"An abstract method cannot be static because static methods cannot be overriden."
1. Static methods can be overriden. The code:
class Super {
public static void aMethod() {System.out.print("Super");}
}
class Sub {
public static void aMethod() {System.out.print("Sub");}
public static void main(String args[]) {
Sub s=new Sub();
s.aMethod();
}
}
will compile and run.
2. I am sure abstractly that abstract methods cannot be static. (And it is static in my mind ). Can anybody share your thoughts WHY abstract methods cannot be static
 
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Igor
Static methods can not be overridden, your code does not show that they can be because your code has no inheritance in it.
Static methods can be hidden (or shadowed) by sub classes but not overridden. The important point in this is that hidden methods (along with variables) do not take part in run time binding, the class of the method is determined at compile time based on the type of the variable. Unlike non static methods that are called based on the type of the object that the variable is refering to.
Look at this code:
run this and you should begin to see the difference.
Hope that helps you out
Dave
 
Ashik Uzzaman
Ranch Hand
Posts: 2373
MySQL Database Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Dave,


Static methods can not be overridden,...


See the following code...Should we not say that static show() is overridden here?


------------------
azaman
 
Muhammad Farooq
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Igor,Dave and Ashik, here I didn't mean overriding of static method, static method cant be overriden as they belong to the class and not to the instance of the class. In the code provided by me, I just want to clear one thing that non-static methods override and static(and private) method dont. Thats why in case of static and private methods , the method from the super class is called.
--Farooq
[This message has been edited by Muhammad Farooq (edited August 15, 2001).]
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Igor,
See this Sun Tech Tip it explains shadowing, overriding and hiding.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
 
Igor Gaschits
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Jane. It was helpful.
Igor
 
Ashik Uzzaman
Ranch Hand
Posts: 2373
MySQL Database Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yah Farooq, that's OK....
------------------
azaman
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Although this code will compile, put the main method in the Super class to get it to run.

Originally posted by Dave Vick:
[B]Igor
Static methods can not be overridden, your code does not show that they can be because your code has no inheritance in it.
Static methods can be hidden (or shadowed) by sub classes but not overridden. The important point in this is that hidden methods (along with variables) do not take part in run time binding, the class of the method is determined at compile time based on the type of the variable. Unlike non static methods that are called based on the type of the object that the variable is refering to.
Look at this code:
run this and you should begin to see the difference.
Hope that helps you out
Dave[/B]


 
Ashik Uzzaman
Ranch Hand
Posts: 2373
MySQL Database Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ranchers,
I think we better to say this static method issue literally as :
static methods cannot be overridden but they can be hidden in the subclass by decalaring and/or defining a method with identical signature and modifiers.
Do u all agree?

------------------
azaman
 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i will like to add one point to this..
the all rules whihc apply when we override a method will apply in case of static too...
so be careful abuot that
and if i am wrong tell me so..
bye
 
Ashik Uzzaman
Ranch Hand
Posts: 2373
MySQL Database Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All rules means what..... (rules on static variables?)....any example?
------------------
azaman
 
They gave me pumpkin ice cream. It was not pumpkin pie ice cream. Wiping my tongue on this tiny ad:
professionally read, modify and write PDF files from Java
https://products.aspose.com/pdf/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!