• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 385
  • Last Modified:

Inheritance and Abstract

I got help from you guys in here for a couple of month ago and now I got my feedback from it

I have got this answer ofmy program:

Hmm ... I can not accept your inheritance hierarchy and your abstract class when much of it is completely useless. It seems entirely possible to completely delete all the methods in the "account" class, ie. no inheritance of these appear to be? In addition, you should use the abstract classes wherever possible.

What is wrong? I cant see it. Look at my code and please explain
Account.java
----------------------------------------------
package p3;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

public abstract class Account 
{	
	//Declaration
	public UUID konto_namn;
	public String namn;
	public double balance;
	public static final int MAX_UNITS = 10;
	public List<Trans> trans = new ArrayList<Trans>();

	/**
	 * getbalance - Get balance
	 * @return balance 
	 */
	public double getbalance()
	{
		return balance;
	} //end getbalance

	
	/**
	 * setKontoNamn - Sätter Konto namnet
	 * 
	 * @param namn - UUID namnet
	 */
	public void setKontoNamn(UUID namn) 
	{} //end setKontoNamn

	/**
	 * deposit - Put in money
	 * @param amount - How much
	 */
	public void deposit( double amount ) 
	{} //end deposit


	/**
	 * withdraw - Take out money
	 * @param amount - how much
	 * @return 0
	 */
	public double withdraw( double amount ) 
	{
		return 0;
	} //end withdraw
}


----------------------------------------------
SavingsAccount.java
----------------------------------------------
package p3;

import java.util.List;
import java.util.UUID;


public class SavingsAccount extends Account{

	//Declaration
	private double ranta = 0.5;
	
	/**
	 * SavingsAccount Constructor
	 * @param amount the amount
	 */
	public SavingsAccount( double amount )
	{		
		konto_namn = UUID.randomUUID();
		balance = amount;
		namn = "SparKonto";
	} //SavingAccount

	
	/**
	 * Overloading SavingAccount constructor
	 */ 
	public SavingsAccount()
	{
		konto_namn = UUID.randomUUID();
		balance = 0.0;
		namn = "SparKonto";
	} //SavingAccount

	
	/**
	 * deposit - Put in somemoney
	 * 
	 * @param amount
	 */
	public void deposit( double amount )
	{
		System.out.println("Insättning utfört");
		balance += amount;
	} //deposit

	
	/**
	 * withdraw - Withdraw money
	 * 
	 * @param amount
	 * 
	 * @return 0
	 */
	public double withdraw( double amount )
	{
		// See if amount can be withdrawn
		if (balance >= amount)
		{
			balance -= amount;
			System.out.println("Uttag utfört");
			return amount;
		} //end if
		else
		{
			// Withdrawal not allowed
			System.out.println("Du får ej ta ut några pengar");
			return 0.0;
		} //end else
	} //end withdraw

	
	/**
	 * getbalance - Get the money
	 * 
	 * @return balance
	 */
	public double getbalance()
	{
		return balance;
	} //end getbalance
	
	
	
	
	/**
	 * getTrans - Get the list
	 * 
	 * @return balance
	 */
	public List<Trans> getTrans()
	{
		return trans;
	} //end getbalance
	
	
	
	/**
	 * setTrans - Get the list
	 * 
	 * @param o - object Trans
	 */
	public void setTrans(Trans o)
	{
		this.trans.add(o);
	} //end setTrans
		
	
	/**
	 * getName - Get name
	 * 
	 * @return namn
	 */
	public String getName()
	{
		return namn;
	} //end getName
	
	
	
	/**
	 * setKontoNamn - Set the kontoname
	 */
	public void setKontoNamn(UUID namn)
	{
		this.konto_namn = namn;
	} //end setKontoNamn

	
	/**
	 * getKontoNamn
	 * 
	 * @return konto_namn
	 */
	public UUID getKontoNamn() 
	{
		return konto_namn;
	} //end getKontoNamn

	
	
	/**
	 * getRanta
	 * 
	 * @return ranta
	 */
	public double getRanta() 
	{
		return ranta;
	} //end getRanta
	
	
	
	/**
	 * setRanta
	 * 
	 * @param amount
	 */
	public void setRanta(double amount) 
	{
		this.ranta = amount;
	} //end setRanta
	
	
	
    /**
     * Skriv ut
     * @return string of kontonamn and balance
     */
    public String toString() 
    {      
        return "Kontonamn: " +namn +"\nKontoNummer: " +konto_namn + "\nBalance: " +balance +" kronor\n------------------------\n";
    } //end toString
    
}  //end class

-------------------------------------------------------
CreditAccount.java
------------------------------------------------------
package p3;

import java.util.List;
import java.util.UUID;


/**
 * 
 * Inlämnings uppgift 2
 * 
 * 
 * @author 		Mikael Floberg
 * @version 	1.0
 * @since 		2009-09-16
 * @see			Customer
 * @see			SavingsAccount
 * @see			BankHandler
 * @see			CivicRegistrationNumber
 * @see			CreditAccount
 * @see			Account
 * @see			Trans
 */


public class CreditAccount extends Account{

	//Declaration
	private double ranta = 0.5;
	private final static double MAX_CREDIT = 1000;
	private double skuldRanta = 5.5;
	
	
	/**
	 * CreditAccount Constructor
	 * @param amount the amount
	 */
	public CreditAccount( double amount )
	{		
		konto_namn = UUID.randomUUID();
		balance = amount;	
		namn = "KreditKonto";
	} //CreditAccount

	/**
	 * Overloading CreditAccount constructor
	 */ 
	public CreditAccount()
	{
		konto_namn = UUID.randomUUID();
		balance = 0.0;
		namn = "KreditKonto";
	} //CreditAccount
	

	/**
	 * deposit - Put in somemoney
	 * @param amount - the amount
	 */
	public void deposit( double amount )
	{
		balance += amount;
		System.out.println("Insättning utfört");
	} //deposit

	
	
	/**
	 * withdraw - Withdraw money
	 * 
	 * @param amount
	 * 
	 * @return 0
	 */
	public double withdraw( double amount )
	{
		// See if amount can be withdrawn
		if (balance+MAX_CREDIT >= amount)
		{
			balance -= amount;
			System.out.println("Uttag utfört");
			return amount;
		} //end if
		
		else
		{
			// Withdrawal not allowed
			System.out.println("Du får ej ta ut några pengar.");
			return 0.0;
		} //end else
	} //end withdraw

	
	/**
	 * getName - Get name
	 * @return namn
	 */
	public String getName()
	{
		return namn;
	} //end getName
	
	
	/**
	 * getbalance - Get the money
	 * @return balance
	 */
	public double getbalance()
	{
		return balance;
	} //end getbalance
	
	
	/**
	 * getTrans - Get the list
	 * 
	 * @return balance
	 */
	public List<Trans> getTrans()
	{
		return trans;
	} //end getbalance
	
	
	
	/**
	 * setTrans - Get the list
	 * 
	 * @param o - Object Trans
	 */
	public void setTrans(Trans o)
	{
		this.trans.add(o);
	} //end setTrans
		
	
	
	/**
	 * setKontoNamn - Set the kontoname
	 * 
	 * @param namn
	 */
	public void setKontoNamn(UUID namn)
	{
		this.konto_namn = namn;
	} //end setKontoNamn

	
	/**
	 * getKontoNamn
	 * @return konto_namn
	 */
	public UUID getKontoNamn() {
		return konto_namn;
	} //end getKontoNamn

	
	
	/**
	 * getRanta
	 * @return ranta
	 */
	public double getRanta() {
		return ranta;
	} //end getRanta
	
	
	
	/**
	 * getSkuldRanta
	 * @return skuldRanta
	 */
	public double getSkuldRanta() {
		return skuldRanta;
	} //end getSkuldRanta
	
	
	/**
	 * setRanta
	 * 
	 * @param amount
	 */
	public void setRanta(double amount) {
		this.ranta = amount;
	} //end setRanta
	
	
	
    /**
     * Skriv ut
     * @return string of kontonamn and balance
     */
    public String toString() {      
        return "KontoNummer: " +konto_namn + "\nBalance: " +balance +" kronor\n------------------------\n";
    } //end toString
    
}  //end class

Open in new window

0
Mickeys
Asked:
Mickeys
  • 4
  • 4
  • 2
  • +1
3 Solutions
 
Gurvinder Pal SinghCommented:
@Mickeys: I can't see any abstract method in Account Class, so why have you named it Abstract class.

Where are you using/over-riding the inherited methods from Account Class in the derived classes?

0
 
MickeysAuthor Commented:
So how should it be? This is what I got told in here. ????
0
 
Gurvinder Pal SinghCommented:
If the intention is to show the abstraction and inheritance principle of java, then
1) Make at least one method of Account class abstract, and implement that in derived class.
2) Use the base class constructor in the derived class constructor
3) Create a main class where you will invoke the base class method using the object of derived class.

Thanks
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
HugoHiaslCommented:
You are defining the methods in the Account class and you hide it by new local methods with the same name in the inherited classes.

What sense do you see in implementing these methods in the Account class?

I assume you want to enforce that all dependent classes that inherit the Account class must implement the methods

        public double getbalance();
      public void setKontoNamn(UUID namn);
      public void deposit( double amount ) ;
      public double withdraw( double amount ) ;

Am I right?

In this case you should make them abstract:

        public abstract double getbalance();
      public abstract void setKontoNamn(UUID namn);
      public abstract void deposit( double amount ) ;
      public abstract double withdraw( double amount ) ;

and implement it in the derived classes.
0
 
MickeysAuthor Commented:
So all that really is wrong is that it should be like this instead?
public abstract class Account 
{	
	//Declaration
	public UUID konto_namn;
	public String namn;
	public double balance;
	public static final int MAX_UNITS = 10;
	public List<Trans> trans = new ArrayList<Trans>();

	/**
	 * getbalance - Get balance
	 * @return balance 
	 */
	public abstract double getbalance(); //end getbalance

	
	/**
	 * setKontoNamn - Sätter Konto namnet
	 * 
	 * @param namn - UUID namnet
	 */
	public abstract void setKontoNamn(UUID namn); //end setKontoNamn

	/**
	 * deposit - Put in money
	 * @param amount - How much
	 */
	public abstract void deposit( double amount );



	/**
	 * withdraw - Take out money
	 * @param amount - how much
	 * @return 0
	 */
	public abstract double withdraw( double amount ); 

}

Open in new window

0
 
Gurvinder Pal SinghCommented:
@Mickeys: this is good for a start, but i think you need to do all three steps that i have mentioned in my previous reply. This is just the half of the first step.

Thanks

0
 
MickeysAuthor Commented:
so the nr 2 should be

/**
       * CreditAccount Constructor
       * @param amount the amount
       */
      public CreditAccount( double amount )
      {      
                            super();
            konto_namn = UUID.randomUUID();
            balance = amount;      
            namn = "KreditKonto";
      } //CreditAccount
0
 
MickeysAuthor Commented:
or should it be


      public Account(UUID konto_namn, String namn, double balance)
      {
            this.konto_namn  = konto_namn;
            this.namn = namn;
            this.balance = balance;
      }



and then in the derrived

public CreditAccount( double amount )
      {      
                            super(UUID.randomUUID(), amount, "KreditKonto");
      } //CreditAccount
0
 
Gurvinder Pal SinghCommented:
0
 
Sathish David Kumar NCommented:
Ur never writing  any return type in ur pgm !!

check that !!

0
 
Sathish David Kumar NCommented:
<<<<<<Both are fine.

what gurvinder372: is there any return type in this method ??????
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 4
  • 4
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now