This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Type problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Type problem" Watch "Type problem" New topic

Type problem

Ganesh Kumar
Ranch Hand

Joined: Jul 02, 2007
Posts: 113
When creating an instance on the left hand side we are making a variable to its type.What does it mean??.I can understand that It is of type of that class name.But what does it actually does?

g go=new go();

in the above program we are making an instance of type go which is going to refer come().What does it actually says. What are all the powers or the thing the variable 'h' can do?

Please solve my problem!!!
bala kris

Joined: Jul 10, 2007
Posts: 5
Do you have Type Problem???
dhwani mathur
Ranch Hand

Joined: May 08, 2007
Posts: 621
well !!!here you are creating an instance of class go as shown in the below statement
go g=new come();

g is an instance of class go but it is the object of class come .using the instance of super class you can implement methods of derived class.That is using g you can implement methods of class come.
i hope it helps.
Ganesh Kumar
Ranch Hand

Joined: Jul 02, 2007
Posts: 113
Thanks for your reply Dhwani but it does not works as you said.It shows an compile time error
Remko Strating
Ranch Hand

Joined: Dec 28, 2006
Posts: 893
I didn't get a compile time error. Can you show your error.

Remko (My website)
SCJP 1.5, SCWCD 1.4, SCDJWS 1.4, SCBCD 1.5, ITIL(Manager), Prince2(Practitioner), Reading/ gaining experience for SCEA,
Shyam Prasad Murarka
Ranch Hand

Joined: May 02, 2005
Posts: 209
Dear Ganesh Kumar,

Taking your code from this point:

  • The first line creates an object of type "go" and THAT object is referenced by the variable "g".
  • Another object is created of type "come" and THIS object is referenced by the variable "c".
  • Now, remember in first step, the variable "g" was pointing to an object of type "go", BUT in this step we "tell" "g" to stop referring to that object and start referring to the object that "c" is referring to.So, after this step, both the variables, "g" and "c", refer to the SAME object.
  • In the last line, we create another object of type "come", and this object is referred to a variable called "h", BUT this variable "h" is of type "go", WHICH is allowed, since a "come" object IS-A "go" object.

  • Here, you also see "upcasting" taking place, where a more generalized type refers to a more specific type (down the inheritance tree).
    What's the use of upcasting? Read on...

    I think you are trying to understand the power of inheritance. If you are, then it would be much better if you tried doing it with day-to-day examples such as inheritance in the case of Animals and Shapes. It becomes really confusing with weird examples such as "go" and "come"!
    Now, consider you have a class called Person that has a method called talk().

    Now, we make classes called "IndianPerson" and "AfricanPerson" extend Person.

    Now, lets create a program that can show our inheritance at work:

    Have a look at the output.

    You will notice that even though the array is of type Person, the compiler knows(you'll understand later as to how it knows) which type it actually belongs to! That is a small power of inheritance.
    Explore this mechanism to create a hierarchy of Shapes (such as Circle, Rectangle, Triangle), which will have a calculateArea() method.

    Now, you can use "upcasting" to store more generalized types.

    Ok, I agree this post may be a lot confusing and off topic, but you should first try understanding inheritance with proper examples.

    With Best Regards,
    Shyam Prasad Murarka
    Ganesh Kumar
    Ranch Hand

    Joined: Jul 02, 2007
    Posts: 113
    Dear shyam,
    I can understand what you are saying.But by doing this what power did shyam has? If ganesh is super class and shyam is sub class.Can it access methods of shyam?
    I am asking what is the difference between the above and .

    And the last doubt is .We are making gan as ganesh type what does it means???

    Shyam please clarify my doubts
    Ganesh Kumar
    Ranch Hand

    Joined: Jul 02, 2007
    Posts: 113
    class go{
    int i,j;
    void g(){
    int k=9;
    class come extends go{
    void f(){
    int y=10;
    class demo{
    public static void main(String args[]){
    go g=new go();
    come c=new come();
    go h=new come();


    Compiler says f() cannot be resolved.Can you please say me the answer for the previous post Remko

    Shyam Prasad Murarka
    Ranch Hand

    Joined: May 02, 2005
    Posts: 209
    Dear Ganesh,

    Compiler says f() cannot be resolved.

    That's because "h" is a variable of type "go" and the compiler checks for the f() method in class "go" and then it proceeds to the super classes. It DOES not check in the subclasses of "go".
    To access the methods of class "come", you need to explicitly downcast "h" object into type "come" and then use the f()method, like done below:

    But, you HAVE to be sure that "h" was originally created either with:
  • class "come" or
  • any subclass of "come"

  • Otherwise you will get ClassCastException.
    Shyam Prasad Murarka
    Ranch Hand

    Joined: May 02, 2005
    Posts: 209
    Dear Ganesh Kumar,
    Let's say you created an abstract class called Shape.

    AND, among many other methods that you have in this Shape class is a method called intersects() which returns a boolean.
    This method checks whether a Shape object touches another Shape object.

    Now, you could do it this way:

    Basically, you would be overloading the intersects method to perform operations for different shapes.

    You create subclasses such as Rectangle, Circle, etc.

    You now publish your very own Shape API into this world.

    Now, a person from Brazil wants to extend your Shape class and create HIS own arbitrary shape. He also wants to use your intersects() method to check whether HIS shape object touches a Rectangle, Circle, etc.

    But, unfortunately, he can't! Because you haven't specified any overloaded method in the abstract class to support HIS shape. And that person is obviously NOT allowed to change your Shape class.

    So, how do you work around this?
    Simple, by generalizing the parameter accepted by intersects() method to Shape type.

    So, now any person from anywhere in this world could use HIS own shape and use your intersects() method simply by extending your Shape class.

    So, that's the power you get by doing Shape rect = new Rectangle();
    Or, as in your case, ganesh gan = new shyam();

    By Ganesh Kumar,
    If ganesh is super class and shyam is sub class.Can it access methods of shyam?

    In the above case, ganesh gan = new shyam(), you CAN access methods of shyam only if you explicitly downcast "gan" variable.

    So, in case of
    ganesh gan = new shyam()
    ganesh gan = new ganesh(),
    the difference lies in how you use those variables.
    Upcasting (ganesh gan = new shyam()) can provide you a lot of power as shown above.

    Regarding your last doubt, tell me, what do you mean by int i?
    The same meaning holds true for ganesh gan!
    It means "i" is of type int and "gan" is of type ganesh.
    Ganesh Kumar
    Ranch Hand

    Joined: Jul 02, 2007
    Posts: 113
    thanks Shyam.To be frank their is little more doubt.Will contact you regarding that soon.
    I agree. Here's the link:
    subject: Type problem
    It's not a secret anymore!