Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes inheritance Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "inheritance" Watch "inheritance" New topic
Author

inheritance

Aruna
Greenhorn

Joined: Mar 21, 2001
Posts: 5
Please explain superclass ref and subclass ref.I am under confusion
class Process6
{
Process6()
{
write();
}
void write()
{
System.out.println(Thread.activeCount());
}
}
class Test6 extends Process6
{
public static void main(String[] args)
{
Process6 t = new Test6();
}
public void write()
{
System.out.println(Thread.interrupted());
}
}
Ans : Prints false
Question: ( ie) Here the write() method in the subclass is executed. How? Is the write() method
Of the superclass to be executed? Pls explain?
A. Aruna
kapil apshankar
Ranch Hand

Joined: Dec 17, 2000
Posts: 66
Hi,
you could give a constructor to the subclass and call the superclass method thru it as
Test6 {
super.write();
}
Hope it helps,
Correct me if I am wrong.
Cheers
Kapil


Hope this helps. Correct me if I am wrong.<p>Cheers <img src="smile.gif" border="0"> ,<br />Kapil
Samith Nambiar
Ranch Hand

Joined: Mar 14, 2001
Posts: 147
hi Aruna,
The implication of the line :
Process6 t = new Test6();
in the main method is that the methods that are overriden
by the subclass will be used.
Now if u wish to call the write() of the class Process6
u will have to change that line to :
Process6 t = new Process6();
Hope this solves the problem
SAmith
vkswamy venkatachalam
Greenhorn

Joined: Feb 18, 2001
Posts: 21
Hi,Aruna
since the runtime reference refer to the sub class U are getting subclass write method is excecuted.by changing the ref. to superclass as samith told or U can call super class method asin sample code.
code:
class Process6{
Process6(){
write();
}
void write(){
System.out.println(Thread.activeCount());
}
}
class Test extends Process6{
public static void main(String[] args){
Process6 t = new Test();
//new Process6().write(); method 1
}
//Test(){super.write();} method 2
public void write(){
//super.write(); method 3
System.out.println(Thread.interrupted());
}
}
regrds
vkswami


vkswami
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: inheritance