Reputation: 4712
I'm trying to wrap my head around inheritance for a uni assignment and I've ran into a few problems so far.
I'm trying to construct a method in my Pet class which holds the following code:
public class Pet {
protected String printed;
public Pet() {
}
public String checkFunc(String definitelyPrinted) {
printed = "CheckFunc is working! Oh boy!";
System.out.println("Those variables were useless");
return printed;
}
}
This is being called by:
public class KennelDemo extends Pet {
private String filename; // holds the name of the file
private Kennel kennel; // holds the kennel
private Pet pet; // holds the pet
private Scanner scan; // so we can read from keyboard
private String tempFileName;
private String dogsFile = "dogs.txt";
private String catsFile = "cats.txt";
private void checkFuncMain() {
String definitelyPrinted;
definitelyPrinted = pet.checkFunc(printed);
System.out.print(definitelyPrinted);
}
}
And then ran from the console menu here:
case "7":
checkFuncMain();
break;
and the output from this menu:
private void runMenu() {
String response;
do {
printMenu();
System.out.println("What would you like to do:");
scan = new Scanner(System.in);
response = scan.nextLine().toUpperCase();
switch (response) {
case "1":
admitDog();
break;
case "2":
changeKennelName();
break;
case "3":
printDogsWithBones();
break;
case "4":
searchForDog();
break;
case "5":
removeDog();
break;
case "6":
setKennelCapacity();
break;
case "7":
printAll();
break;
// TODO
case "a":
checkFuncMain();
break;
case "Q":
break;
default:
System.out.println("Try again");
}
} while (!(response.equals("Q")));
}
is:
Try again.
Quite simply I'm trying to just print out "CheckFunc is working, oh boy!"
via inheritance, once I understand it and how it's working I can complete my assignment.
Currently it doesn't run. I've tried a few different things (like changing String to void for checkFunc
and not returning anything) but I can't figure it out.
Could somebody explain this to me please?
Thanks in advance.
Upvotes: 1
Views: 103
Reputation: 709
I don't really see how you're using inheritance at all.
What is your subclass? I also don't really see how you're taking advantage of object oriented programming.
It's hard to even tell what is going on in your main method because you don't show what pet
is constructed as.
For inheritance, you can try something like:
class Pet {
protected String printMsg;
public Pet() {
this.printMsg = "I am just a pet, I have nothing interesting to say.";
}
public String getPrintMsg() {
System.out.println("In getPrintMsg() for " + String.valueOf(this.getClass()) + ".");
return this.printMsg;
}
}
class Cat extends Pet {
public Cat() {
super(); // Call Pet() constructor.
this.printMsg = "I am a cat, I go meow."; // Cat gets access to printMsg since it is a "protected" property of Pet, which is a parent class to Cat.
}
}
class Dog extends Pet {
public Dog() {
super(); // Call Pet() constructor.
this.printMsg = "I am a dog, I go woof and bark."; // Cat gets access to printMsg since it is a "protected" property of Pet, which is a parent class to Dog.
}
}
public class PetApp {
public static void main(final String[] args) {
Pet pet = new Pet();
System.out.println(pet.getPrintMsg());
pet = new Cat(); // Since Cat extends Pet, I can set pet of Pet type to a new instance of Cat.
System.out.println(pet.getPrintMsg());
pet = new Dog(); // Since Dog extends Pet, I can set pet of Pet type to a new instance of Dog.
System.out.println(pet.getPrintMsg());
}
}
If I now run PetApp, I get the following output:
$ gedit PetApp.java $ javac PetApp.java $ java PetApp In getPrintMsg() for class Pet. I am just a pet, I have nothing interesting to say. In getPrintMsg() for class Cat. I am a cat, I go meow. In getPrintMsg() for class Dog. I am a dog, I go woof and bark. $
Hope that helps.
To take a step further, a real world application would probably use an interface
or abstract class
for Pet, since a Pet would either be a behavior pattern or a class that has a few methods which can do something but others that need to be implemented by a child class. You cannot create an instance
of any abstract class
es, but you can define methods and even have code for some methods in an abstract class
. Where as an interface
is just the behavior pattern that any class
which implements the interface needs to follow.
Upvotes: 0
Reputation: 1537
Clearly the input is the issue. You're reading something from System.in
that doesn't match any of the case
s.
Upvotes: 0
Reputation: 614
When the user is asked to input something, you do
response = scan.nextLine().toUpperCase();
Because of this, when you enter in the letter a
, it isn't upper-case, so it won't accept that case, and it will just use the default case
.
Upvotes: 1