aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Super and sub class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Super and sub class" Watch "Super and sub class" New topic
Author

Super and sub class

ramaseshan T
Ranch Hand

Joined: Feb 17, 2005
Posts: 30


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


Ramaseshan T<br />SCJP 1.4
Nicholas Cheung
Ranch Hand

Joined: Nov 07, 2003
Posts: 4982
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 ]

SCJP 1.2, OCP 9i DBA, SCWCD 1.3, SCJP 1.4 (SAI), SCJD 1.4, SCWCD 1.4 (Beta), ICED (IBM 287, IBM 484, IBM 486), SCMAD 1.0 (Beta), SCBCD 1.3, ICSD (IBM 288), ICDBA (IBM 700, IBM 701), SCDJWS, ICSD (IBM 348), OCP 10g DBA (Beta), SCJP 5.0 (Beta), SCJA 1.0 (Beta), MCP(70-270), SCBCD 5.0 (Beta), SCJP 6.0, SCEA for JEE5 (in progress)
ramaseshan T
Ranch Hand

Joined: Feb 17, 2005
Posts: 30
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 !!!
Kenneth Brooks
Greenhorn

Joined: Feb 21, 2005
Posts: 9
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

Joined: Feb 17, 2005
Posts: 30
Can we conclude that polymorphic behaviour is present and exhibited only on Methods and not on variables?
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608

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.


Tony Morris
Java Q&A (FAQ, Trivia)
Aruna Agrawal
Ranch Hand

Joined: Jan 27, 2005
Posts: 66
what is the difference b/w non-constant and non-final
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
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 ...
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
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."


Mike Gershman
SCJP 1.4, SCWCD in process
Igor Stojanovic
Ranch Hand

Joined: Feb 18, 2005
Posts: 58
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
trivikram Kumar
Ranch Hand

Joined: Feb 21, 2005
Posts: 172
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.


Javainn<br /> <br />SCJP,SCWCD,SCBCD,<br />ICED (IBM-287,IBM-484,IBM-486)
IBM certified in DB2 V 8.1 Family Fundamentals (IBM 700)
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608

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:

Animesh Shrivastava
Ranch Hand

Joined: Jul 19, 2004
Posts: 298
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

Joined: Sep 24, 2003
Posts: 1608
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Super and sub class