• 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

inheritance

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Did you just should on me? You should read this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic