File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Class Declaration Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Class Declaration" Watch "Class Declaration" New topic
Author

Class Declaration

Paulo Aquino
Ranch Hand

Joined: Apr 29, 2002
Posts: 200

Using the code above,
whats the difference in the following declaration??
Base a = new Agg();
Agg a = new Agg();
[ edited to shorten super long line -ds ]
[ August 11, 2002: Message edited by: Dirk Schreckmann ]

Be Afraid...Be very Afraid...
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
Hi Paulo,
The difference is that class Agg has a getFields method and class Base doesn't. If you put an abstract (or concrete for that matter) getFields method in class Base then you can use a polymorphic reference without generating a compiler error.
Hope this helps,
Michael Morris


Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. - Ernst F. Schumacher
Paulo Aquino
Ranch Hand

Joined: Apr 29, 2002
Posts: 200
so meaning is a polymorphic reference? given that i put an abstract method getFields() at class Base...
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
whats the difference in the following declaration??
Base a = new Agg();

This declares an identifier of type Base that refers to an object of type Agg - which is fine because an Agg is a Base.
Agg a = new Agg();
This declares an identifier of type Agg that refers to an object of type Agg.


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
Hi Paulo,
Dirk is completely correct in his explanation of the differences between the two references. I was explaining why you were getting a compiler error. Anytime you use a parent object to refer to a child, you can only call methods or refer to variables that are defined in the parent class. Then when you call methods on the reference then by polymorphism, the methods on the child class will be called.
Sorry for any confusion,
Michael Morris
Paulo Aquino
Ranch Hand

Joined: Apr 29, 2002
Posts: 200
whats the difference in the following declaration??
Base a = new Agg();
This declares an identifier of type Base that refers to an object of type Agg - which is fine because an Agg is a Base.
Agg a = new Agg();
This declares an identifier of type Agg that refers to an object of type Agg

I have a follow up question on this Dirk Schreckmann...sorry for my insistent questioning i just want to understand things...At compile time Base a = new Agg(), will it go to the Base() class and then after that? will it still go to the Agg() class?
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
Hi Paulo,
Java guarantees that the method will be called on the actual type. Consider:

If you then do something like:

Then the value of name would be "C".
Hope this clears it up,
Michael Morris
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
At compile time Base a = new Agg(), will it go to the Base() class and then after that? will it still go to the Agg() class?
If you are referring to the constructors, then the Agg class constructor is called first, followed by the Base class constructor. But if you are referring to polymorphic behavior related to method invocations, then no, as Michael has pointed out, only the method of the object type is invoked. Note that that method could invoke it's super class method that was overridden.
For a better understanding of polymorphic behavior, take a look at The "How My Dog Learned Polymorphism" Story in the JavaRanch Campfire Stories and The Managing Inheritance Section of Sun's Java Tutorial.
Good Luck.
Paulo Aquino
Ranch Hand

Joined: Apr 29, 2002
Posts: 200
class Process {
byte b=127;
Process() {
this.methodA();
}
void methodA() {
System.out.println("Value of b is = " + b );
}
public static void main(String [] args) {
Processor p = new Processor();
}
}
class Processor extends Process {
byte b=126;
Processor() {
System.out.println("Value of b = " + b);
}
void methodA() {
System.out.println("Value of b = " + this.b);
}
}
What is the Output?
1.Prints Value of b = 0 and Value of b is = 126.
2.Compile-time error occurs.
3.Prints Value of b = 126 and Value of b = 126.
4.Prints Value of b = 127 and Value of b = 126.
the correct answer is :1
here i can't get how zero is printed ?
can anybody help me?
When you invoke the constructor in the staement "Processor p = new Processor();", the following events transpire.
1. If this is the first reference to class Processor, then perform any super class (Process) static initialization. Nothing is static in class Process, so nothing happens.
2. If this is the first reference to class Processor, then perform any Processor static initialization. Nothing is static in Processor, so nothing happens.
3. Inside the Processor constructor, the first thing we do is invoke the default super constructor, i.e, Process().
4. Inside the Process constructor, the first thing we do is invoke the default super constructor, i.e. Object().
5. Back from the Object() constructor with no visible effect, and currently in the Process constructor, the next thing we do is the instance initialization. This sets the Process byte b value to 127.
6. Still inside the Process() constructor, we've seen a lot of stuff done before the first statement is encountered. Now it's time to execute the Process() constructor statements, in order. That is, invoke "methodA()".
7. The "methodA" that gets invoked is from class Processor, not class Process, because we're really an object of type Processor. Even though we're deep in the bowels of the Process() constructor, polymorphism takes over and it's the Processor "methodA()" that is invoked.
8. Inside methodA(), "this.b" refers to the "b" in class Processor. This value has not been initialized yet, so we print a value of zero.
9. Return from class Processor methodA() to the Process constructor.
10. We're done with the Process() constructor so return to the Processor() constructor.
11. Back from the Process() constructor with significant visible effect, and currently in the Processor constructor, the next thing we do is the instance initialization. This sets the Processor byte b value to 126. Note how this is occurring after the value was printed from the super constructor.
12. Now we're finished instance initialization, and we execute the constructor statements in order, that is, we print out the b value of 126.
The point here is that the base class constructor is executed before the instance initialization of the derived class. Also, via methods and polymorphism, the base class can retrieve and display (or alter!) pre-instance initialization values from the derived class. The derived class instance initialzation occurs subsequent to the base class constructor. Understanding this should be worth a question or two on the certification exam.

Thanks for your patience in teaching me the rudiments of OOP concepts. Actually, im planning to take up SCJP2 1.4 within this year that's why im brushing up on my OOP concept analization. Anyway, i searched through the forum and found the post above. I repost it again, and hopefully some ranchers will find them useful. Thank you very much!
Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
Posts: 787
Hi Paulo:
Consider following code:

In my mind following is the sequence of steps:
1. main() is lanuched.
2. An object of type Processor is requested.
3. Compiler goes to Processor class' constructor
4. Compiler finds that Processor class is
sub-class of Process class.
5. Compiler first creates a sub-object of
Process class.
6. Compiler lanuches the constructor of Process
class.
7. Now funny thing happens: Though constructor
of Process is in action, all references made in it,
are not resolved to Process class but they are
resolved to Processor class. In other words,
imagine that Process's constructor is just
another method in Processor class and is being
executed as any method in Processor with all
references resolving to Processor class.
8. Therefore, when MethodA is called, MethodA of
Processor is launched. At this time instance var
b (of Processor class) is not yet initiallized.
Therefore a zero is printed.
9. Now instance variable b of Processor object
initialized to 126.
10. code in Processor constructor is executed
that prints 126.
I hope this helps.
Thanks
Barkat
 
jQuery in Action, 2nd edition
 
subject: Class Declaration