• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Interfaces

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

This Q is from TIJ 4e, Interfaces, Ex-4

It is like this


Create an abstract class with no methods. Derive a class and add a method. Create a static method that takes a reference to the base class, downcasts it to the derived class and calls the method. In main(), demonstrate that it works. Now put the abstract declaration for the method in the base class, thus eliminating the need for the downcast.



How providing a abstract declaration for a method in base class will eliminate the need to downcast ??\

So i tried it with the following code


It still requires downcast. i checked the errara of this 4e.

But there is nothing regarding this.

Please clarify over this.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
could you tell me? what are the valid literals for float,because
if i compile
float f = \u0038; it is compiling
what is this \u0038
 
Balakrishna Thati
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Santosh,

\u0038 is a unicode representation.

when java finds \u0038, it converts it to its equivalent value of 8.

so in when you run your code, f will print 8.0

observer this code


char c='\u0042';
int \u0042= \u0038;
System.out.println("x = "+B+", c = "+c); //B's unicode value is 0042 and 8's is 0038 (in Hex)



Here out put would be x=8, c = B

for unicode value look at
http://www.ssec.wisc.edu/~tomw/java/unicode.html
 
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Santosh,
Please do not steal other's post. If you have questions to ask, make a new thread.

Balakrishna,
Even you should not have answered him.

To answer Balakrishna's original question, methods are called based on the runtime object. So, at runtime, the variable obj has an object of Derived class. So, Derived class' method will be called. But the compiler looks for method based on the type of the variable. So, if you declare a method in base class, the compiler will compile and at runtime, the actual object's method will be called.

Vijay.

 
Balakrishna Thati
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Vijay,

my question is about the requirement of downcast.

how a method declaration in base class will eliminate the need to downcast an object.

As you said, provide the method in base (remove the first comments) and try to compile. it is not getting compiling saying "incompatible types"

now provide the comments for the method in base class and remove the comments around casting. It is compiling and running normally.

my question is whether the thing mentioned in the book wrong or what??
 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Create an abstract class with no methods. Derive a class and add a method. Create a static method that takes a reference to the base class, downcasts it to the derived class and calls the method. In main(), demonstrate that it works. Now put the abstract declaration for the method in the base class, thus eliminating the need for the downcast



Balakrishna,,
This statement means
It is not asking to declare a an object with reference type Derived just

downcasts it to the derived class and calls the method

So the thing mentioned in the book is not wrong.
 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just use the Base element to call the method.
The compiler is happy, because the method is part of Base class contract, and polymorphism kicks in at runtime and calls the method from Derived.
This is how you can modify your code to respect TIJ4e requirements (no downcasting):

 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic