6 Replies - 1396 Views - Last Post: 30 October 2009 - 12:27 PM Rate Topic: -----

#1 sammantah  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 40
  • Joined: 20-June 09

abstract methods,overriding and inheritance conflicts

Post icon  Posted 30 October 2009 - 10:14 AM

someone pls help!!! i'm creating full and part time classes that extend the base Student but i'm getting errors when compiling my sub classes.
below is the error:
C:\Users\SammyLove\Desktop\Chapter10\FullTimeStudent.java:3: FullTimeStudent is not abstract and does not override abstract method setCreditHrs() in Student
public class FullTimeStudent extends Student
^
1 error

Tool completed with exit code 1


and the code:


public class FullTimeStudent extends Student
{
	double FINAID_RATE = 0.8;

	public FullTimeStudent()
	{
		super("Student");
	}

	public FullTimeStudent(String name)
	{
		super(name);
	}

	public String getName(String name)
	{
		return name;
	}

	public String setName(String name)
	{
		this.name = name;
		return name;
	}

	public double getFinAid()
	{
		double finAid = TUITION_RATE * creditHrs * FINAID_RATE;
		return finAid;
	}

	public int getCreditHrs(int creditHrs)
	{
		return creditHrs;
	}

	public boolean setCreditHrs(int hrs)
	{
		if(MIN_FULLTIME_HRS<=hrs && hrs<=MAX_FULLTIME_HRS)
		 {
			 this.creditHrs=hrs;
		   }
		  else
		  this.creditHrs=0;
	}

	public String getStatus()
	{
		return new String ("fullOrPartTime");

	}

}



base code (Student):

import java.util.*;

public abstract class Student extends Exception
{
	final static double MIN_FULLTIME_HRS = 12.0;
	final static double MAX_FULLTIME_HRS = 18.0;
	final static double MIN_PARTTIME_HRS = 0.5;
	final static double MAX_PARTTIME_HRS = 11.5;
	final static double TUITION_RATE = 220.5;

	public int creditHrs;
	public  String name;

	public Student()
	{
		super("Student program");
	}

	public Student(String name)
	{
		super(name);
	}

	public abstract String getName();
	public abstract String setName();
	public abstract double getFinAid();
	public abstract int getCreditHrs();
	public abstract boolean setCreditHrs();
	public abstract String getStatus();

}





Is This A Good Question/Topic? 0
  • +

Replies To: abstract methods,overriding and inheritance conflicts

#2 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10440
  • View blog
  • Posts: 38,668
  • Joined: 27-December 08

Re: abstract methods,overriding and inheritance conflicts

Posted 30 October 2009 - 10:19 AM

If you have a subclass of an abstract class, you have to override each abstract method in the superclass or declare your subclass as abstract as well. Your compiler is telling you that you are not overriding the method setCreditHrs() in your class FullTimeStudent.
Was This Post Helpful? 0
  • +
  • -

#3 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: abstract methods,overriding and inheritance conflicts

Posted 30 October 2009 - 10:20 AM

the abstract method setCreditHrs() in Student and the setCreditHrs(int hrs) method in FullTimeStudent dont have the same signature.
the method in FullTimeStudent gets an int parameter, while the abstract class doesnt.
so either change the declaration of the abstract method in student, or change the implemantation in the subclass.
for example, in Student class change it to:
public abstract boolean setCreditHrs(int hrs);


or, you could implemet it in FullTimeStudent as:
public boolean setCreditHrs(){
//code
}


Was This Post Helpful? 0
  • +
  • -

#4 sammantah  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 40
  • Joined: 20-June 09

Re: abstract methods,overriding and inheritance conflicts

Posted 30 October 2009 - 10:39 AM

Thanks a lot to both of you, that is a clear explanation i now understand
Was This Post Helpful? 0
  • +
  • -

#5 sammantah  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 40
  • Joined: 20-June 09

Re: abstract methods,overriding and inheritance conflicts

Posted 30 October 2009 - 10:59 AM

this should probably be my last question concerning this program:
i'm now testing the classes and in my test program i have among other codes:
if(student.setCreditHrs())
			{
				System.out.println("Hours invalid for "+student.getStatus()
									+" student.\n");
				success = false;
			}


and
System.out.println("\nStudent : "+student.getName() + "is taking"+student.getCreditHrs() + "credit hours,");
		System.out.println("and is receiving "+ twoDigits.format(student.getFinAid()) +	"in financial aid.\n\n");



both the line are giving the error below

C:\Users\SammyLove\Desktop\Chapter10\Test.java:77: setCreditHrs(int) in Student cannot be applied to ()
if(student.setCreditHrs())
^
C:\Users\SammyLove\Desktop\Chapter10\Test.java:108: getCreditHrs(double) in Student cannot be applied to ()
System.out.println("\nStudent : "+student.getName() + "is taking"+student.getCreditHrs() + "credit hours,");
^
2 errors

Tool completed with exit code 1

i assume this should be something i got wrong in my data types but i have no idea how to change it
Was This Post Helpful? 0
  • +
  • -

#6 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: abstract methods,overriding and inheritance conflicts

Posted 30 October 2009 - 11:05 AM

setCreitHrs method should get an int value as a parameter.
you try to call it without passing it any argument.
in those errors you call it as:
//first time:
if(student.setCreditHrs())
//and second time:
System.out.println("\nStudent : "+student.getName() + "is taking"+student.getCreditHrs() //...



you should pass this method an int value in order to use it.
Was This Post Helpful? 0
  • +
  • -

#7 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10440
  • View blog
  • Posts: 38,668
  • Joined: 27-December 08

Re: abstract methods,overriding and inheritance conflicts

Posted 30 October 2009 - 12:27 PM

If you look in your abstract super class, the setCreditHrs() method doesn't take a parameter. In your subclass FullTimeStudent, your method setCreditHours has a parameter. Because the headers don't match, the methods aren't the same and therefore, the abstract method isn't being overridden. Try adding the matching parameter into the abstract super class method setCreditHrs().
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1