• 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

Super and sub class

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Expected Output:2,Sub (As Sup holds a subclass object)
Actual Output: 5, Sub
Can you please explain?
 
Ranch Hand
Posts: 4982
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As the statement inside the method is invoking sup.index, and dont forget, instance members are binded in compliation time, so it always refers to the Super class member, because sup is of type Super.

Nick
[ February 21, 2005: Message edited by: Nicholas Cheung ]
 
ramaseshan T
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am not getting it..Doesn't virtual concept hold good here? Doesn't Sup object points to sub object internally? Can you pls eloborate on the sequence?

Thanks !!!
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is the line you should focus on:



You may be instantiating a Sub object, however the object reference is
of type Super. That means that the only things you that the reference will be able to see (without the cast to the subclass) are the members that are declared in the class.

However, because of the late binding nature of polymorphism the method that gets called is the overridden one.

This is actually a useful characteristic.
Take for example a Shape class that defines an abstract method of getArea() and that Shape class may have subclasses of Circle and Square. You would like to be able to iterate over a collection of objects of different types of shapes as long as they were all subclasses of Shape and call getArea(). You wouldn't want to have to know what the base class is, thus the late binding will magically find its way to the overridden method in the base class.



-ken
 
ramaseshan T
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can we conclude that polymorphic behaviour is present and exhibited only on Methods and not on variables?
 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Can we conclude that polymorphic behaviour is present and exhibited only on Methods and not on variables?



Yes.
It is for this reason, and many more, that declaring non-constant (not to be confused with non-final) members that are not private is poor form.
 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what is the difference b/w non-constant and non-final
 
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I nebver read the whole post , just read your post , so IMO in java final means constants ( for variable ) so non-constants & non-final should be same ...
 
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

what is the difference b/w non-constant and non-final


In Java, "constant" is short for "compile time constant". Java has special rules for expressions that can be fully evauated at compile time. Typically, a constant expression can have one or more literals and other compile time constants.

A final variable can also have an initializer expression that cannot be evaluated at compile time, such as a method call. There are also final variables with no initializer expressions, called "blank finals". These are initialized (once only) in an initializer block or in a constructor before they can be used.

From the JLS:


15.28 Constant Expression

ConstantExpression:
Expression

A compile-time constant expression is an expression denoting a value of primitive type or a String that is composed using only the following:

* Literals of primitive type and literals of type String
* Casts to primitive types and casts to type String
* The unary operators +, -, ~, and ! (but not ++ or --)
* The multiplicative operators *, /, and %
* The additive operators + and -
* The shift operators <<, >>, and >>>
* The relational operators <, <=, >, and >= (but not instanceof)
* The equality operators == and !=
* The bitwise and logical operators &, ^, and |
* The conditional-and operator && and the conditional-or operator ||
* The ternary conditional operator ? :
* Simple names that refer to final variables whose initializers are constant expressions
* Qualified names of the form TypeName . Identifier that refer to final variables whose initializers are constant expressions

Compile-time constant expressions are used in case labels in switch statements (�14.10) and have a special significance for assignment conversion (�5.2).

A compile-time constant expression is always treated as FP-strict (�15.4), even if it occurs in a context where a non-constant expression would not be considered to be FP-strict.

Examples of constant expressions:

true
(short)(1*2*3*4*5*6)
Integer.MAX_VALUE / 2
2.0 * Math.PI
"The integer " + Long.MAX_VALUE + " is mighty big."

 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by ramaseshan T:

Expected Output:2,Sub (As Sup holds a subclass object)
Actual Output: 5, Sub
Can you please explain?




Casting can affect the selection of compile-time items such as variables and overloaded methods, but not the selection of overridden methods.Casting the reference sup to type Super (widening it) affects the selection of the shadowed variable index within it. However,the cast doesn't affect the selection of the overridden method printVal().


kind regards
Igor
 
Ranch Hand
Posts: 172
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thats a good question...i struggled through....in getting a proper answer...Just remember one point...to make the concept clear...when instantiating the class...,the methods and static fields are referred with respect to the object created and the normal fields are referred with respect to the object references.This has made me feel comfortable with this concept.
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


what is the difference b/w non-constant and non-final




I nebver read the whole post , just read your post , so IMO in java final means constants ( for variable ) so non-constants & non-final should be same ...



This untrue, which is why I explicitly mention that there is a distinction.

Here's some code to ruffle your feathers:

 
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wow
that was a great question!!!
After some research i found out this statement in JLS "12.4.1 When Initialization Occurs"
It says:


A class or interface type T will be initialized immediately before the first occurrence of any one of the following:


T is a class and an instance of T is created.
T is a class and a static method declared by T is invoked.
A static field declared by T is assigned.
A static field declared by T is used and the reference to the field is not a compile-time constant (�15.28). References to compile-time constants must be resolved at compile time to a copy of the compile-time constant value, so uses of such a field never cause initialization.



The last point explains the behaviour of the ouptut. In class V, the string being initialized is a compile time expression constant, so the class is not initialized. Similar explanation applies to the variable in class X.

Finally the output is "UWYZ"

Hope i am making sense
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have plenty more just like it.
I really should publish them in a more formal manner.
I'll put it on the list of TODO at priority #32434634634, just below taking a shower
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic