• Post Reply Bookmark Topic Watch Topic
  • New Topic

polymorhism of objects  RSS feed

 
Jatin Dhingra
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am active java learner and really got stuck on this one !! Can somebody please put some light on this one. Below is the program on polymorphism using inheritance and overridden functions.

--------------------------------------
class sup
{
int x=1;
int y=2;
void print()
{
System.out.println("A class print method");
}
void show()
{
System.out.println("A class show method");
}

}

class sub extends sup
{
int x=3;
void print()
{
System.out.println("B class print method");
}
}


class polymorphic
{
public static void main(String[] args)
{
sup obj=new sub();
obj.print();
System.out.println("x:"+obj.x);
}
}

-----------------------------------------------------

Result i got is :

B Class print method
x:1

----------------------------------------------------

Now as I understood, Obj will be reference variable of type sup. but a new object of type sub is created and assigned to obj. This is completely legal and we can access methods and variables of class sub using obj, provided that methods and variables are ALSO present in sup.

I can understand "B Class print method", but "x:1" is not understood by me. As i think, obj is accessing variables and methods of class sub so it shud print "x:3" !!!




 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please UseCodeTags. You can use the edit button to edit your post and add them.

Polymorphism only applies to instance (e.g. non-static) methods. If you declare a field with the same name that doesn't override the field of the superclass, it hides it instead. The compiler uses the reference type to determine which one to use. So even if the actual class is sub, if the variable is declared as sup then sup's version of i is used.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Everything Rob says is correct, but I wanted to add that beginners often don't appreciate at first that while variables aren't polymorphic, subclasses can assign their own initial values to them, which gives you the effect you were after without creating two separate variables. If you change sub's definition to the below, then your test program will print 3, not 1:

 
Jatin Dhingra
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Rob:

I will surely take care of putting code tags in future !!

"If you declare a field with the same name that doesn't override the field of the superclass, it hides it instead. The compiler uses the reference type to determine which one to use. So even if the actual class is sub, if the variable is declared as sup then sup's version of i is used."

when you say field, does it mean instance variables? As i understand, when we use "new" keyword a object of sub class is created along with instance variables defined in it and assigned initial value ( as i think it will assign it value x=3). Reference variable "obj" which is defined as sup type, then holds reference to this object. so shudnt it give output as x:3 ? if i am wrong on this object creation and assignment,please provide your valuable feedback.

@Ernest:
When we say variables are not polymorphic, do we mean reference variables or instance variables? I tried giving it constructor, but still result is same. Below is modified code( With code tags )

// Definition of superclass
class sup
{
// variable definition
int x=1;
int y=2;
//print Method definition
void print()
{
System.out.println("A class print method");
}
//show method definition
void show()
{
System.out.println("A class show method");
}

}

//class sub inherited from sup class
class sub extends sup
{
// constructor
sub()
{int x=3;
}
//overrides print method of parent class
void print()
{
System.out.println("B class print method");
}
}

//test class
class polymorphic1
{
public static void main(String[] args)
{ // creation of sub class object and assignment to reference variable obj of type sup
sup obj=new sub();
//calling print method
obj.print();
System.out.println("x:"+obj.x);
}
}



 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Didn't I ask you to UseCodeTags before? I clearly see such a suggestion in my first reply in this thread. You even acknowledge so, then ignore it.
Jatin Dhingra wrote:@Rob:

I will surely take care of putting code tags in future !!

But ok, back to business.

when you say field, does it mean instance variables?

Yes.

Reference variable "obj" which is defined as sup type, then holds reference to this object. so shudnt it give output as x:3 ?

Which x do you think you're printing there? The x defined in sub or the x defined in sup? Because these two are different instance variables that just happen to have the same name.
 
Jatin Dhingra
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob,
Please bear with me as I m new to javaranch and didnt actually understood putting code tags on programs( I thought it same as comments in programs ! ). I will find out how to add them.

As i see, it is printing here x defined in sup.

From your reply,what i can infer is that class sub has two variables named x( one its own and one inherited from class sup). is it so ? if yes, can I access these two variables from "obj" defined in main method ? or it is same as for methods.. that from "obj" we can only access methods of class sub that are common to class sup, but not methods exclusive to class sub.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jatin Dhingra wrote:I m new to javaranch and didnt actually understood putting code tags on programs


well, It is simple!

just surround your code with [ code ] --code -- [ / code ] tag [Ignore the spaces]. i.e,

[ code ]
//put your code/program here
[ / code ]

alternatively, while typing/posting your code in text area just above that[like tool bar] you can find Code button. to apply this, just select your program/code and then click tha button.
 
Jatin Dhingra
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks Seetharaman !!!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!