4 Replies - 2525 Views - Last Post: 18 June 2012 - 01:36 PM Rate Topic: -----

#1 azeroth  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 18-June 12

"Class is not abstract and does not override abstract method"

Posted 18 June 2012 - 01:18 PM

Please note: I am a beginner in Java.
I wrote an abstract class, Shape.

abstract class Shape
{
	String n;
	public String getName(String name)
	{
		n = name;
		return n;
	}
	public abstract double getDimension();
}


I then wrote a subclass of Shape, which is also abstract:
abstract class TwoDimensionalShape extends Shape
{
	public double getDimension(byte dimension)
	{
		dimension = 2;
		return dimension; //returns dimension of shapes(subsequent subclasses)
	}
	
	public abstract double getPerimeter();
	public abstract double getArea();
}


Then, I wrote a subclass of TwoDimensionalShape, called Circle; it is not abstract.
class Circle extends TwoDimensionalShape
{
	double a;
	String n = "Circle";
	
	//constructor
	public Circle(double radius)
	{
		a = radius;
	}
	
	
	//circumference/perimeter
	public double getPerimeter()
	{
		return 2 * Math.PI * a;
	}
	
	//area
	public double getArea()
	{
		return Math.PI * (a * a);
	}
	
}


The error I am getting is: "Circle.java:1: Circle is not abstract and does not override abstract method getDimension() in Shape". Circle inherits getDimension() from TwoDimensionalShape; this overrode the abstract getDimension() in Shape. Thus, I'm not sure how to fix this error; please advise. I am also making a "Driver" class to print the values from getPerimeter() and getArea().

Is This A Good Question/Topic? 0
  • +

Replies To: "Class is not abstract and does not override abstract method"

#2 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7961
  • View blog
  • Posts: 13,580
  • Joined: 19-March 11

Re: "Class is not abstract and does not override abstract method"

Posted 18 June 2012 - 01:25 PM

This method


public double getDimension(byte dimension)



does not override this method


   public abstract double getDimension();


because they do not have the same signature. The name and the parameter list must be the same. What you've done in TwoDimensionalShape is to add a second method with the same name, which takes a byte as an argument.
Was This Post Helpful? 3
  • +
  • -

#3 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1363
  • View blog
  • Posts: 3,002
  • Joined: 30-January 11

Re: "Class is not abstract and does not override abstract method"

Posted 18 June 2012 - 01:27 PM

The Shape class has an abstract method -

public abstract double getDimension();


(taking no parameters)

Now the TwoDimensionalShape class has its own getDimension which takes a parameter -

public double getDimension(byte dimension)

This does not override the method as the signatures are not the same.

But the TwoDimensionalShape class is abstract, and so it doesn't mind that it doesn't implement getDimension with no parameters.

However when you make Circle, which is concrete, you need to implement the getDimension from Shape with no parameters.

Edit - ninja'd

This post has been edited by Ryano121: 18 June 2012 - 01:27 PM

Was This Post Helpful? 2
  • +
  • -

#4 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7961
  • View blog
  • Posts: 13,580
  • Joined: 19-March 11

Re: "Class is not abstract and does not override abstract method"

Posted 18 June 2012 - 01:31 PM

View PostRyano121, on 18 June 2012 - 03:27 PM, said:

Edit - ninja'd


Damn straight. Keep those shuriken, I think I got them from you in the first place. :)
Was This Post Helpful? 1
  • +
  • -

#5 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1363
  • View blog
  • Posts: 3,002
  • Joined: 30-January 11

Re: "Class is not abstract and does not override abstract method"

Posted 18 June 2012 - 01:36 PM

It's about time! Those things aren't cheap you know! :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1