The Artima Developer Community
Sponsored Link •

Design Forum
Constructor

1 reply on 1 page. Most recent reply: Dec 12, 2002 4:41 PM by Thomas SMETS

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
Arbab-ul-hassan

Posts: 4
Nickname: umpirepk
Registered: Dec, 2002

Constructor Posted: Dec 11, 2002 2:56 AM
Reply to this message Reply
Advertisement
hi friends,
i am having a problem with constructor can anyone help me.
here is the coding.
class A{
int i,j;
A(int a,int b){
i=a;
j=b;
}}
class B Extends A{
B(int aa,int bb){
i=aa;
j=bb;
}
}
class obj{
public static void main(String rags[]){
B b=new B(122,555);
}}

where i am making mistake in this program.


Thomas SMETS

Posts: 307
Nickname: tsmets
Registered: Apr, 2002

Re: Constructor Posted: Dec 12, 2002 4:41 PM
Reply to this message Reply
First remark is :
Respect of other people means that you could at least take 30 secs to read how to post properly. Just thinking that somebody is gonna help is certainly not enough... You should try to make our life easier too

The B class will never compile as it is using the default constructor of A (namely the A ( ) constructor). That constructor has been "disabled" because you defined one your self. As you instanciates B, the very first statement executed in the constructor is in fact a hidden
super ();
.
The following code herebelow hould work
class A
{
  int i,j;
/* Removing these comments should also work */
//  A () {}     
 
  A ( int a, int b)
  {
    i=a;
    j=b;
  }
 
  public String toString ()
  {
     return "i="+i+",j="+j; // Working but incorrect implementation.
  }
}
class B 
  extends A
{
  B ( int a, int b)
  {
    super (a, b); // You refere the right constructor
  }
}
 
class obj
{
  public static void main(String rags[])
  {
    B b=new B(122,555);
    System.out.println("B= " + b);
  }
}
Ooooooooh, BTW this is not a Design problem but simply a coding problem !

Rgds,

Thomas,

Flat View: This topic has 1 reply on 1 page
Topic: Defining Coarse & Fine Grained Previous Topic   Next Topic Topic: Writting with C++

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use