• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

inheritance

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
this q is from Thinking in java.
create 2 classes A and B with constructors that have arguments. Inherit a new class called C from A, create a member B inside C. Create a constructor for C and perform all initializatio within C's constructor. can anyone suggest a code for this. mine(given below give a compiler error-"No constructor matching A() found in class A"
class A {
int i;
A(int k) {
System.out.println("in A");
i = k;
}
}
class B {
int j;
B(int k) {
System.out.println("in B");
j = k;
}
}
public class C extends A {
B b = new B(4);
C() {
System.out.println("in C");
}
public static void main(String[] arrgs) {
C c = new C();
}
}
 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Bhuvana,
If you do not explicitly provide a call to the constructor of the super class in the constructor of its inherited class, a call to super() will automatically be provided as the first line in the constructor of the inherited class. Therefore, since the C() constructor in class C does not call super in its first line a call to the default super() is automatically provided for you. However, in class A you do not have a default constructor of the form A(). This causes the compiler to complain about the missing constructor A() in A. This can be corrected by providing a call to super(x) in the C() constructor where x is an integer value used to initialize i in class A.
Hope this helps,
Bob Kerfoot - SCJP
 
Bhuvana Dhruva
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks a million. i got it!
 
reply
    Bookmark Topic Watch Topic
  • New Topic