Welcome to Dream.In.Code
Become a Java Expert!

Join 150,369 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,831 people online right now. Registration is fast and FREE... Join Now!




Error trying to create an Object

 
Reply to this topicStart new topic

Error trying to create an Object

Israel
14 Feb, 2008 - 09:05 PM
Post #1

D.I.C Addict
Group Icon

Joined: 21 Nov, 2004
Posts: 626


Dream Kudos: 175
My Contributions
Anyone know why I cannot create this second object called VirtualPet? I keep getting an error saying "Compilation Error: Virtual Pet could not be resolved to a type"

Here's the tester file (with the error on line 14)
CODE
package Project2;

import javax.swing.ImageIcon;
import javax.swing.JOptionPane;


public class VirtualTester {

    public static void main(String[] args){

        ImageIcon strongDog = new ImageIcon ("Project2/strongdog.jpg");
        JOptionPane.showMessageDialog(null, strongDog.toString());
        
        VirtualPet pet1 = new VirtualPet("Bob", true, true);
        JOptionPane.showMessageDialog(null, pet1.toString());
    }
}


And here's the class file:
CODE
package Project2;

public abstract class VirtualPet{
private String name;
private boolean Eating;
private boolean Sleeping;

public VirtualPet(String string, boolean isEating, boolean isSleeping) {
    super();
    this.name = string;
    this.Eating = true;
    this.Sleeping = true;    
}



//Get Method for getName and Return
public String getName() {
    return name;
}

public boolean isEating() {
    return Eating;
}

public boolean isSleeping() {
    return Sleeping;
}

    public String toString() {          
    StringBuilder result = new StringBuilder();
    result.append("This is \n" + getName());
    
    return result.toString();

}
}

User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Error Trying To Create An Object
14 Feb, 2008 - 09:32 PM
Post #2

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,660



Thanked: 313 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
Remember, you can't instantiate abstract classes. Abstract means that it is to serve as a base class to inherit from, not that it is going to be used to create an object from.

So remove the word "abstract" from your virtualpet class definition. Since you are not explicitly inheriting from another class, you can also get rid of the call to "super()". While it won't be a problem to keep it in, you are just saying that you call the object's base class constructor... which is in this case Object. This is done by default anyways.

Then make sure that you have both files in the same directory and it should compile fine. See if that works for you. smile.gif
User is offlineProfile CardPM
+Quote Post

Israel
RE: Error Trying To Create An Object
14 Feb, 2008 - 10:17 PM
Post #3

D.I.C Addict
Group Icon

Joined: 21 Nov, 2004
Posts: 626


Dream Kudos: 175
My Contributions
Yeah, it works like that. But I'm supposed to have it in an abstract class for class. I've gotta get that string and image in one ImageIcon box too...
User is offlineProfile CardPM
+Quote Post

bhandari
RE: Error Trying To Create An Object
15 Feb, 2008 - 04:38 AM
Post #4

D.I.C Addict
Group Icon

Joined: 31 Jan, 2008
Posts: 747


Dream Kudos: 900
My Contributions
You can declare your abstract class as:

CODE
public abstract class VirtualPet{
String getName();
boolean isEating();
boolean isSleeping();
}


and then create another class(MyPet) which extends the above class as
CODE
public class MyPet extends VirtualPet{
private String name;
private boolean Eating;
private boolean Sleeping;

public MyPet(String string, boolean isEating, boolean isSleeping) {
    super();
    this.name = string;
    this.Eating = true;
    this.Sleeping = true;    
}



//Get Method for getName and Return
public String getName() {
    return name;
}

public boolean isEating() {
    return Eating;
}

public boolean isSleeping() {
    return Sleeping;
}

    public String toString() {          
    StringBuilder result = new StringBuilder();
    result.append("This is \n" + getName());
    
    return result.toString();

}
}


As of now, sun has no plans to allow abstract classes to be instantiated.

This post has been edited by bhandari: 18 Feb, 2008 - 03:21 AM
User is offlineProfile CardPM
+Quote Post

Israel
RE: Error Trying To Create An Object
17 Feb, 2008 - 08:34 PM
Post #5

D.I.C Addict
Group Icon

Joined: 21 Nov, 2004
Posts: 626


Dream Kudos: 175
My Contributions
Hmm... I'm getting errors from this part. The compiler keeps saying that the methods require a body instead of a semicolon even though its declared as abstract?

CODE
public abstract class VirtualPet{
    String getName();
    boolean isEating();
    boolean isSleeping();
    }

User is offlineProfile CardPM
+Quote Post

baavgai
RE: Error Trying To Create An Object
17 Feb, 2008 - 08:46 PM
Post #6

Dreaming Coder
Group Icon

Joined: 16 Oct, 2007
Posts: 2,289



Thanked: 136 times
Dream Kudos: 475
Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua, Cheese

My Contributions
That style of code is more in keeping with an interface:

CODE

interface VirtualPetIface {
    String getName();
    boolean isEating();
    boolean isSleeping();
}


For a similar abstract class, you must explicitly declare the methods abstract for the class do know you really meant to do that.

Here's a reasonable abstract class for your pet:

CODE

abstract class VirtualPet implements VirtualPetIface {
    protected String name;
    public VirtualPet(String name) { this.name = name; }
    public String getName() { return this.name; }
    public abstract boolean isEating();
    public abstract boolean isSleeping();
}


Hope this helps.

User is offlineProfile CardPM
+Quote Post

Nayana
RE: Error Trying To Create An Object
17 Feb, 2008 - 08:49 PM
Post #7

DIC Hawk - 나야나 नयन:
Group Icon

Joined: 14 Nov, 2007
Posts: 824



Thanked: 5 times
Dream Kudos: 175
My Contributions
- got beat to it

This post has been edited by Nayana: 17 Feb, 2008 - 08:49 PM
User is offlineProfile CardPM
+Quote Post

bhandari
RE: Error Trying To Create An Object
17 Feb, 2008 - 11:13 PM
Post #8

D.I.C Addict
Group Icon

Joined: 31 Jan, 2008
Posts: 747


Dream Kudos: 900
My Contributions
will definitely write a tutorial about abstract class and interfaces in java!!
User is offlineProfile CardPM
+Quote Post

baavgai
RE: Error Trying To Create An Object
18 Feb, 2008 - 03:17 AM
Post #9

Dreaming Coder
Group Icon

Joined: 16 Oct, 2007
Posts: 2,289



Thanked: 136 times
Dream Kudos: 475
Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua, Cheese

My Contributions
QUOTE(bhandari @ 18 Feb, 2008 - 02:13 AM) *

will definitely write a tutorial about abstract class and interfaces in java!!


Um, but your abstract class example is wrong. tongue.gif

User is offlineProfile CardPM
+Quote Post

bhandari
RE: Error Trying To Create An Object
18 Feb, 2008 - 03:20 AM
Post #10

D.I.C Addict
Group Icon

Joined: 31 Jan, 2008
Posts: 747


Dream Kudos: 900
My Contributions
QUOTE
QUOTE(bhandari @ 18 Feb, 2008 - 02:13 AM)

will definitely write a tutorial about abstract class and interfaces in java!!



Um, but your abstract class example is wrong.



ha ha!!
because that was copied from code in first post. laugh.gif
User is offlineProfile CardPM
+Quote Post

Israel
RE: Error Trying To Create An Object
18 Feb, 2008 - 06:45 PM
Post #11

D.I.C Addict
Group Icon

Joined: 21 Nov, 2004
Posts: 626


Dream Kudos: 175
My Contributions
Thank you. Now I just have to figure out why my image isn't showing up.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 02:20PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month