Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Subclass not compiling.

  1. #1
    Member
    Join Date
    Feb 2013
    Posts
    38
    Thanks
    4
    Thanked 1 Time in 1 Post

    Default Subclass not compiling.

    Hi all. How's everybody doing? Good I hope. I'm hanging in there.

    What I have are some classes for a program I'm sure many of you have seen before, ye ol' BankAccount class. What I've done here is write a subclass that inherits from the the BA class. But the subclass doesn't compile and I can't figure out what to do. I keep getting the error below:

    SavingsAccount.java:10: error: no suitable constructor found for BankAccount(String,String)
    super(CName, CType);
    ^
    constructor BankAccount.BankAccount(int,String,String,String,d ouble,double) is not applicable
    (actual and formal argument lists differ in length)
    constructor BankAccount.BankAccount() is not applicable
    (actual and formal argument lists differ in length)
    1 error


    Below are the classes I've written. If anyone can point me in the right direction, it would be much appreciated.

    import java.util.*;  
    import java.io.*;  
    import java.text.DecimalFormat;
     
     
    class BankAccount 
    {
     
    	private int AccountNumber;
    	private String AccountType;
    	String CustName;   // Made these two public
       String CustType;
    	private double Balance;
    	private double MonthlyFee;
     
    	private static int NumberOfBankAccounts = 0;
     
    	BankAccount() 
    	{
     
    		AccountNumber = 0;
    		AccountType = "";
    		CustName ="";
    		CustType="";
    		Balance =0.0;
    		MonthlyFee = 0.0;
     
    	}
     
    	BankAccount(int AccNum, String AccType, String CName, String CType, double Bal, double mFee) 
    	{
    		AccountNumber = AccNum;
    		AccountType = AccType;
    		CustName = CName;
    		CustType = CType;
    		Balance = Bal;
    		MonthlyFee = mFee;
    	}
     
    	public int getAccountNumber() 
    	{
    		return AccountNumber;
    	}
     
    	public void setAccountNumber(int accNum) 
    	{
    		AccountNumber = accNum;
    	}
     
    	public String getAccountType() 
    	{
    		return AccountType;
    	}
     
    	public void setAccountType(String accType) 
    	{
    		AccountType = accType;
    	}
     
    	public String getCustName() 
    	{
    		return CustName;
    	}
     
    	public void setCustName(String cName) 
    	{
    		CustName = cName;
    	}
     
    	public String getCustType() 
    	{
    		return CustName;
    	}
     
    	public void setCustType(String cType) {
    		CustType = cType;
    	}
     
    	public double getBalance() 
    	{
    		return Balance;
    	}
     
    	public void setBalance(double bal) 
    	{
    		Balance = bal;
    	}
     
    	public double getMonthlyFee() 
    	{
    		return MonthlyFee;
    	}
     
    	public void setMonthlyFee(double mFee) 
    	{
    		MonthlyFee = mFee;
    	}
     
    	public static void incrementBankAccountNumber() 
    	{
    		NumberOfBankAccounts++;
    	}
     
    	public String toString() 
    	{
    		DecimalFormat formatter2 = new DecimalFormat("#0.00");   
    		return "Account #:      " + AccountNumber + 
    		" Account Type:   " + AccountType +
    		" Customer Name:  " + CustName +
    		" Customer Type:  " + CustType +
    		" Balance:        $" + Balance +
    		" Monthly Fee:    $" + formatter2.format(MonthlyFee);
     
    	}
     
    }


    class SavingsAccount extends BankAccount
    {
    	private double savingsNumber;
    	private double savingsBalance;
     
    	public SavingsAccount(){}
     
    	public SavingsAccount(String CName, String CType, double savNum, double savBal) //Not sure if CType is the arg needed.
    	{
    		super(CName, CType);
    		savingsNumber = savNum;
    		savingsBalance = savBal;
    	}
     
    	public void setsavingsNumber(double savNum)
    	{
    		savingsNumber = savNum;
    	}
     
    	public void setsavingsBalance(double savBal)
    	{
    		savingsBalance = savBal;
    	}
     
    	public double getsavingsNumber()
    	{
    		return savingsNumber;
    	}
     
    	public double getsavingsBalance()
    	{
    		return savingsBalance;
    	}
     
    	public String toString()
    	{
    		String str =  "Customer: " + super.getCustName()
    						+ "\n, has a current savings balance of: " + savingsBalance;
     
    		return str;
    	}
     
     
     
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Subclass not compiling.

    SavingsAccount.java:10: error: no suitable constructor found for BankAccount(String,String)
    super(CName, CType);
    ^
    The SavingAccount's super class: BankAccount does NOT have a constructor that takes two Strings as args.
    A couple of choices:
    Change the call to the super constructor to pass the args that it takes.
    Add a new constructor to the BankAccount class that takes two Strings as args
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Feb 2013
    Posts
    38
    Thanks
    4
    Thanked 1 Time in 1 Post

    Default Re: Subclass not compiling.

    Thanks again Norm. (I was just moving this post to proper forum.) Anyway, you're fast. So, I had been thinking along the lines of your first suggestion, but does that mean I need to pass ALL the args of the BA class AND the two new ones I made? Can you do that?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Subclass not compiling.

    does that mean I need to pass ALL the args
    It means that the call to a constructor or a method MUST pass it the args that they require.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Feb 2013
    Posts
    38
    Thanks
    4
    Thanked 1 Time in 1 Post

    Default Re: Subclass not compiling.

    Thanks for trying to help. But I'm sorry to say I'm just not getting it. I mean, I've passed it what it needs. Then I try passing all the args in the order that they appear in the super-class. Still a problem.

    --- Update ---

    Waaaaaiiiiit a minute, let me try something....

    Well, nevermind. I thought if I put all the args in the call to the super class that the super has, THAT would work. But alas...

    --- Update ---

    OK. Finally got it. Makes perfect sense now.

    public SavingsAccount(int AccNum, String AccType, String CName, String CType, double Bal, double mFee, double savNum, double savBal) //Not sure if CType is the arg needed.
    	{
    		super(AccNum, AccType, CName, CType, Bal, mFee);
    		savingsNumber = savNum;
    		savingsBalance = savBal;
    	}

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Subclass not compiling.

    Glad you got it. Please Mark this thread as solved
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Feb 2013
    Posts
    38
    Thanks
    4
    Thanked 1 Time in 1 Post

    Default Re: Subclass not compiling.

    Sure thing Norm. Thanks for your help. You know, all those args just look wild to me. Logically I can see now why they are needed, but I just assumed there was a more 'succinct' way to do this.

Similar Threads

  1. How to end a subclass?
    By Purple01 in forum Java Theory & Questions
    Replies: 2
    Last Post: November 21st, 2012, 02:26 AM
  2. copying subclass object
    By spark in forum Object Oriented Programming
    Replies: 3
    Last Post: July 23rd, 2012, 05:49 PM
  3. Cannot find methods in a subclass
    By Dirk94 in forum Object Oriented Programming
    Replies: 2
    Last Post: March 15th, 2012, 08:32 PM
  4. [SOLVED] how to extend a subclass?!
    By migongotar in forum Object Oriented Programming
    Replies: 4
    Last Post: August 2nd, 2011, 03:22 PM
  5. subclass that implements
    By speedycerv in forum Java Theory & Questions
    Replies: 1
    Last Post: March 30th, 2011, 07:35 AM