Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

inheritance

 
Aruna
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Samith Nambiar
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic