• 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

Code Concepts

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What change shuld be made to this code to make it compile and give i=4
(Also the class A does not have a constructor)
class A {
int i;
A(int i) {
this.i = i * 2;
}
}
class B extends A {
public static void main(String[] args) {
B b = new B(2);
}
B(int i) {
System.out.println(i);
}
}
 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One way is to change Class B -
<PRE>
public class B extends A
{
public static void main(String[] args)
{
B b = new B(2);
}

B(int i)
{
super(i);
i = super.i ;
System.out.println(i);
}
}
</PRE>
Give me a haul if you get and need me to explain.

Ajith
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Faiza,
1)Create a constructor A(){}
2)Make the return type int for A(int i) method
3)In B(int i), add this line super.A(i)

class A {
int i;
A(){
};
int A(int i) {
this.i = i * 2;
return i;
}
}
class B extends A {
public static void main(String[] args) {
B b = new B(2);
System.out.println(b.i);
}
B(int i) {
super.A(i);
}
}
thanks
kiran
 
Without subsidies, chem-ag food costs four times more than organic. Or 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