DZone Forums
Go Back   DZone Forums > Community > Languages & Frameworks > Java
Reload this Page Help converting abstract class to interface
Notices
Reply
 
LinkBack Thread Tools Display Modes
  (#1 (permalink)) Old
Member
 
Posts: 6
Thanks: 1
Thanked 0 Times in 0 Posts
Join Date: Apr 2009
Default Help converting abstract class to interface - 05-29-2009, 06:01 PM

I need to convert 'Bounceable' from an abstract class to an interface - and then update concrete class Basketball to implement 'Bounceable' - how do I do this ? thanks

public abstract class Bounceable extends Ball
{
public Bounceable(String type)
{
super(type);
}
public abstract String bounce();
}

(here is Ball)
public abstract class Ball
{
private String type;
public Ball(String type)
{
this.type = type;
}
public String getType()
{
return type;
}
public abstract void play();
}
}

(here is Basketball)
public class Basketball
extends Bounceable
{
public Basketball()
{
super("Basketball");
}
public String bounce()
{
return " dribble...";
}
public void play()
{
System.out.println("Basketball begins with a tipoff");
}
}
Reply With Quote
  (#2 (permalink)) Old
Member
 
Posts: 11
Thanks: 0
Thanked 2 Times in 2 Posts
Join Date: May 2009
Default 05-30-2009, 05:49 AM

First of all, some general thoughts... Bounceable should NOT extends Ball because a Bounceable Object IS NOT in all cases a Ball (IS A is what extends actually mean)...

I would suggest the following:

1. create a Bounceable interface

Code:
public interface Bounceable {
    String bounce(); // all method declarations in interfaces are public abstract implicitly
}
2. create an abstract Ball class

Code:
public abstract class Ball {
    private String type; 

    protected Ball(String type) {
        this.type = type;
    }

    public getType() {
        return type;
    }

    public abstract void play();
}
3. let Basketball implement Bounceable and extend Ball (so a Basketball IS A ball, which is right AND a bounceable object, which is also true)

Code:
public class Basketball extends Ball implements Bounceable {
    public Basketball(String type) {
        super(type);
    }

    @Override
    public String bounce() {
        return " dribble... ";
    }

    @Override 
    public void play() {
        System.out.println("Basketball begins with a tipoff");
    }
}
That should do the trick ;-)! Didn't try out the code but at least it should give you an idea :-)!
Reply With Quote
The Following User Says Thank You to Trollhorn For This Useful Post:
deepdave (06-02-2009)
  (#3 (permalink)) Old
Member
 
Posts: 6
Thanks: 1
Thanked 0 Times in 0 Posts
Join Date: Apr 2009
Default 05-30-2009, 03:56 PM

Thanks!
In 'Ball' I get the msg 'The method getType() is undefined for the type Ball'
which I think is causing my problem when I test ...
I omitted that there is a driver class to test with (oops):
public class PolyTest4
{
public static void main(String[] args)
{
Ball[] ball = {new Baseball(),new Basketball(),new Bowlingball(),new Tennisball()};
int i=1; // added to allow for ball #

System.out.println("For each type of ball...\n");

for(Ball b : ball)
{
System.out.println("Ball #"+(i)+" is a "+b.getType());
i++;

System.out.print("Playing: ");
b.play();
// checks to see if it is an object of type Bounceable
if( b instanceof Bounceable)
// if so, call bounce method of Bounceable object
System.out.println("Bouncing "+((Bounceable)b).bounce());
else
System.out.println("You can't bounce this ball!");

System.out.println("\n");
}

System.out.println("\nEnd of program.");
}
}
which returns error messages:
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
The constructor Basketball() is undefined
The constructor Tennisball() is undefined
The method getType() is undefined for the type Ball

at Polytest.PolyTest4.main(PolyTest4.java:14)

(the other 2 classes referred to are below (didn't kinow if I had to change them as they don't extend 'Bounceable)

public class Bowlingball
extends Ball
{
public Bowlingball()
{
super("Bowlingball");

}


public void play()
{
System.out.println("Set up the pins. Do you know how to score?");

}

}
and
public class Baseball
extends Ball
{
public Baseball()
{
super("Baseball");
}


@Override
public void play()
{
System.out.println("The American pasttime - batter up!");
}

}

Thanks again
Reply With Quote
  (#4 (permalink)) Old
Member
 
Posts: 11
Thanks: 0
Thanked 2 Times in 2 Posts
Join Date: May 2009
Default 05-31-2009, 05:37 AM

Ups, I omitted the return type in getType()... maybe I shouldn't post code in the future I haven't tested ;-)... of course it should have been

Code:
    public String getType() {
        return type;
    }
That should actually do the trick, ran your Test-PRG with the Ball classes and it worked fine.

One more note: You can retrieve the Classname of a class via the Class object.

Something like this:

Code:
    String someString = "abc";
    System.out.println(someString.getClass().getSimpleName());
Bye, Oliver
Reply With Quote
  (#5 (permalink)) Old
Member
 
Posts: 6
Thanks: 1
Thanked 0 Times in 0 Posts
Join Date: Apr 2009
Default 05-31-2009, 11:33 PM

Thanks, Again, BTW ... still getting:
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
The constructor Basketball() is undefined
The constructor Tennisball() is undefined

at Polytest.PolyTest.main(PolyTest.java:14)public class PolyTest
Reply With Quote
  (#6 (permalink)) Old
Member
 
Posts: 11
Thanks: 0
Thanked 2 Times in 2 Posts
Join Date: May 2009
Default 06-01-2009, 04:34 AM

Could you please post your Basketball and Tennisball classes? It seems like both classes don't implement a parameterless constructor.

Constructor with one parameter

Code:
public Basketball(String type) {
    super(type);
}
Parameterless constructor

Code:
public Basketball() {
    super("Basketball");
}
To instantiate an object using the parameterless constructor:

Code:
Ball ball = new BasketBall();
To instantiate an object using the constructor with one parameter:

Code:
Ball ball = new BasketBall("Basketball");
Greets, Oliver
Reply With Quote
  (#7 (permalink)) Old
Member
 
Posts: 6
Thanks: 1
Thanked 0 Times in 0 Posts
Join Date: Apr 2009
Default 06-01-2009, 06:12 PM

Thanks again from this Mainframe Dinosaur ... I am trying to change 'Bounceable' to an Interface and modify the appropriate concrete classes (Basketball and Tennisball and not Bowling ball and Baseball?) to implement the new interface. I just renamed them all with a '4' appended to keep them distinct from the originals ... Again, the single error is when I try to run Polytest4 (The constructor Basketball4() is undefined
The constructor Tennisball4() is undefined
here they are:

package Polytest;

public class PolyTest4
{
public static void main(String[] args)
{
Ball4[] ball4 = {new Baseball4(),new Basketball4(),new Bowlingball4(),new Tennisball4()};
int i=1; // added to allow for ball #

System.out.println("For each type of ball...\n");

for(Ball4 b : ball4)
{
System.out.println("Ball #"+(i)+" is a "+b.getType());
i++;

System.out.print("Playing: ");
b.play();
// checks to see if it is an object of type Bounceable4
if( b instanceof Bounceable4)
// if so, call bounce method of Bounceable4 object
System.out.println("Bouncing "+((Bounceable4)b).bounce());
else
System.out.println("You can't bounce this ball!");

System.out.println("\n");
}

System.out.println("\nEnd of program.");
}
}
/////////////////////////////////////////////////////////////////
package Polytest;

public abstract class Ball4
{
private String type;

protected Ball4(String type)
{
this.type = type;
}

public String getType()
{
return type;
}

public abstract void play();
}
//////////////////////////////////////////////////////////
package Polytest;
public interface Bounceable4
{
String bounce(); // all method declarations in interfaces are public abstract implicitly
}
///////////////////////////////////////////////////////////
package Polytest;

public class Bowlingball4
extends Ball4
{
public Bowlingball4()
{
super("Bowlingball4");

}


public void play()
{
System.out.println("Set up the pins. Do you know how to score?");

}

}
///////////////////////////////////////
package Polytest;

public class Baseball4
extends Ball4
{
public Baseball4()
{
super("Baseball4");
}


@Override
public void play()
{
System.out.println("The American pasttime - batter up!");
}
}
////////////////////////////////////
package Polytest;

public class Basketball4 extends Ball4 implements Bounceable4
{
public Basketball4(String type)
{
super(type);
}

@Override
public String bounce()
{
return " dribble... ";
}

@Override
public void play()
{
System.out.println("Basketball begins with a tipoff");
}
}
////////////////////////
package Polytest;

public class Tennisball4 extends Ball4 implements Bounceable4
{
public Tennisball4(String type)
{
super(type);
}

@Override
public String bounce()
{
return " dribble... ";
}

@Override
public void play()
{
System.out.println("Basketball begins with a tipoff");
}
}
//////////////////////////////
Reply With Quote
  (#8 (permalink)) Old
Member
 
Posts: 11
Thanks: 0
Thanked 2 Times in 2 Posts
Join Date: May 2009
Default 06-02-2009, 03:24 AM

Thanks, now I got the problem...

There a two ways to solve your problem:

You can

1. change the code in PolyTest4 to:

Code:
public class PolyTest4
{
public static void main(String[] args)
{
Ball4[] ball4 = {new Baseball4(),new Basketball4("Basketball"),new Bowlingball4(),new Tennisball4("Tennisball")};
int i=1; // added to allow for ball #

System.out.println("For each type of ball...\n");

for(Ball4 b : ball4)
{
System.out.println("Ball #"+(i)+" is a "+b.getType());
i++;

System.out.print("Playing: ");
b.play();
// checks to see if it is an object of type Bounceable4
if( b instanceof Bounceable4)
// if so, call bounce method of Bounceable4 object
System.out.println("Bouncing "+((Bounceable4)b).bounce());
else
System.out.println("You can't bounce this ball!");

System.out.println("\n");
}

System.out.println("\nEnd of program.");
}
}
or

2. you can change the constructor signatures (the signature basically is the constructor name + number of parameters + their types) in the Basketball and Tennisball classes to:

Code:
public class Basketball4 extends Ball4 implements Bounceable4
{
public Basketball4()
{
super("Basketball");
}

@Override
public String bounce()
{
return " dribble... ";
}

@Override
public void play()
{
System.out.println("Basketball begins with a tipoff");
}
}
Code:
public class Tennisball4 extends Ball4 implements Bounceable4
{
public Tennisball4()
{
super("Tennisball");
}

@Override
public String bounce()
{
return " dribble... ";
}

@Override
public void play()
{
System.out.println("Basketball begins with a tipoff");
}
}
If you have further questions regarding Objects and Classes, refer to:

Lesson: Classes and Objects (The Java™ Tutorials > Learning the Java Language)

or simply ask :-)!

Bye, Oliver
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Preserving uuid to xmi:id mapping converting uml2 to eCore pierospinelli Eclipse 0 05-18-2009 04:07 AM
Problem With RSA Interface Floetic Java 0 03-25-2008 03:03 AM
DERS Interface Floetic Java 0 03-09-2008 03:03 AM
class or interface expected --compile error ravi503 Java 2 03-07-2008 12:10 AM
Converting Jars to Eclipse Plugins qbproger Eclipse 0 03-06-2008 01:48 PM


Copyright 1997-2009, DZone, Inc.
vBulletin Skin developed by: vBStyles.com