d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Java Abstract Classes And Interfaces
Add Reply New Topic New Poll
Member
Posts: 1,307
Joined: Apr 9 2007
Gold: 1,676.45
Sep 15 2016 12:37am
Ok so I'm having trouble with getting a portion of my code to work. I'm trying to creat an object but I can't because I keep getting "Dog is not abstract and does not override abstract method setName(String) in Nameable" Below is my code that I'm using for this part


Nameable.java
Code
package blah.blah.zoo;


public interface Nameable {

String getName();

void setName(String name);

}


Animal.java
Code
package blah.blah.zoo;


public abstract class Animal implements Nameable{

private String name;
private int numLegs, age, numOfAnimals=0;

public Animal(String name, int numLegs, int age){

this.name = name;
this.numLegs = numLegs;
this.age = age;
numOfAnimals++;
}

public String getName(){

return name;
}

public int getNumLegs(){

return numLegs;
}

public int getAge(){

return age;
}

public int numOfAnimals(){

return numOfAnimals;
}

public String toString(){

return "Animal Object: [name=" + name + " , age=" + age + " , numLegs=" + numLegs + "]";
}
}


Dog.java
Code
package blah.blah.zoo;

public class Dog extends Animal{

public Dog(String name, int numLegs, int age){

super(name, numLegs, age);
}
}


Lastly, my test file test.java
Code
package blah.blah.zoo;

public class test{

public static void main(String[] args){

Dog ABC = new Dog("John", 4, 12);

//System.out.println(ABC.getName());
}

}



If someone could point me in the right direction with figuring out what I need to add/change that would be great. I'm having a hard time understanding the purpose of Nameable.java

This post was edited by ice060788 on Sep 15 2016 12:38am
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Sep 15 2016 06:32am
`setName` is part of the interface. the entire point of an interface is that your class provides an implementation. if you look at animal/dog, you do not define it anywhere. hence the problem.

Quote
I'm having a hard time understanding the purpose of Nameable.java


interfaces provide a contract so the user can call it regardless of the internal implementation. `Comparable` is a great example. you're able to sort arrays no matter what's inside because using the interface guarantees the class can be sorted correctly no matter what class it is.
Member
Posts: 1,307
Joined: Apr 9 2007
Gold: 1,676.45
Sep 15 2016 07:59pm
Ok I get it now, Thanks. One more question though. if I want to call a function from Dog.java how would I do that if the Dog.java file looks like this and impliments another class called Runner


Dog.java
Code
package blah.blah.zoo;

public class Dog extends Animal implements Runner{

public Dog(String name, int numLegs, int age){

super(name, numLegs, age);
}

public void run(){

System.out.print(" is running");
}

}


Runner.java
Code
package blah.blah.zoo;

public interface Runner{

public abstract void run();
}


I want to call the run function from Dog in the main but I can't figure out how. I tried Dog.run() but gave me errors. I guessing it has something to do with Dog extending Animal but just not 100% sure
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Sep 15 2016 08:38pm
Runner lassie = new Dog("lassie", 3, 1);
lassie.run();

Quote
I tried Dog.run() but gave me errors


you're calling it like a class method, but it's an instance method. hence you need an instance which you get by calling its constructor.
Member
Posts: 1,307
Joined: Apr 9 2007
Gold: 1,676.45
Sep 15 2016 09:15pm
Ok. I already created an array for Animal. Do I need to make a Runner array as well even though depending on the type of animal it can run. This is what I created in my main

Code
public class Zoo{

public static void main(String[] args){


Animal[] zooAnimal = new Animal[100];

zooAnimal[0] = new Dog("Snoopy", 4, 8);
zooAnimal[1] = new Dragon("Molly", 4, 1);
.........And so on.......................
Member
Posts: 23,444
Joined: Jun 30 2009
Gold: 167.53
Oct 14 2016 06:11pm
Someone can correct me if I am wrong but I don't think you can call the run method unless you instantiate the Animals as Runners. You don't have a run method in the abstract class.
You would have to instantiate the objects with the interfaces they have.
So if you do:

Code
Runner dragon = new Dragon("James", 4, 1);
dragon.run();


Then you can call the run method - otherwise you won't even have the option of calling on the run method unless you cast the Animal down to Dog or Dragon.
A good way to do this is to simply instantiate an ArrayList and add all the Runners you want into that Arraylist. Then you can cycle through the ArrayList with a for/while loop and call on the run method which will call on each object's unique run method.

This post was edited by umeshieee on Oct 14 2016 06:13pm
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll