• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Objectoreinted concepts

 
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class Client {
public static void main(String args[]) {
String stringRef = new String("java");
System.out.println("2):" + stringRef.getClass());
System.out.println("(3):" + stringRef.length());
Object obref = stringRef;
System.out.println("(6):" + obref.equals("Java"));
System.out.println("(7):" + obref.getClass());
stringRef = (String)obref;
System.out.println("(9):" + stringRef.equals("C++"));
System.out.println("(5):" + obref.length());
}
}

this code doesn't compile. I understand that length() method is not avialable
in the Object class so it gives the error. But i have type casted obref into stringRef so why does it gives the error.

 
Ranch Hand
Posts: 439
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't know if i am right but i'll try.
The reason why there is no length i think is because you just cast the reference that points to an Object not an actual object so therefore Object gives an error because it doesn't provide a length method. Am i right ?
 
Ranch Hand
Posts: 1070
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Val, you are right. When you convert your object hier up, if you want to use methods of the subclass they have to be in the class you converted it too also. If they are not, then you will get a compile error. If they are, then you can call that method and you will get the method of the subclass, not superclass (unless the method is static).
That is one of the reasons why interfaces and abstract classes can be so important. They make sure that the method is in the class, so you can use late-binding and polymorphism. The I/O package is a great example of these techniques.
Bill
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi.
Mosts probally i think when u type cast obj ref to string still y are using method which is not in Object directly..
Thats why u are getting erors
 
shabbir zakir
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi!
It is given in 6 chapter of khalid mughal that this code gives the error beacause length() is not in the Object class.
I got the above code from 6.2 illustrated example.
But i don't think in that way. May be the code is giving error
for some other reason like Val stated.
 
shabbir zakir
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think i am wrong. Val stated that the obref is not an object.
It just an reference
 
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
At compile time, whatever may be the object an reference is pointing to, what the compiler checks is this:
Whether the class of the reference has the method or whether it inherits the method from its superclasses. If it does not fir into the above two criteria, then nobody can rescue u.
 
reply
    Bookmark Topic Watch Topic
  • New Topic