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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Question abt inheritance..

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
class Super
{
int i = 10;
public Super()
{
i = 30;
}
}
class Sub extends Super
{

public Sub()
{
i = 20;
}

}
public class Test
{

public static void main(String[] args)
{
Super s = new Sub();
System.out.println(s.i);

}
}

OUTPUT FOR THIS PROGRAM IS 20

but ..
if we define Sub as follws its givig the output as 30

class Sub
{int i;
public Sub()
{
i = 20;
}
}
y is this happenning?
plzz clarify my doubt
 
Rancher
Posts: 43028
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
This thread is continued here.
 
Joel Salatin has signs on his property that say "Trespassers will be Impressed!" Impressive tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
    Bookmark Topic Watch Topic
  • New Topic