Reputation: 11
For example I made a bird class and I am trying to make a boolean variable that starts off as unhappy and changes to happy when its flying and change back to Unhappy when it tries to swim.
This is my Bird class
public class Bird extends Animal
{
boolean mood = happy;
public void fly()
{
System.out.println("the Bird flies");
}
public void layEgg() {
System.out.println("The bird layed an egg");
}
public void doStuff() {
System.out.println("Doing some stuff...");
fly();
layEgg();
}
}
This is my main method for my bird class
public class Exploration {
public static void main(String[] args) {
Bird myBird = new Bird ();
//myBird.fly ();
//myBird.layEgg();
//myBird.doStuff();
myBird.eat();//It says that it eats
//myBird.swim(); It doesnt work because a bird isnt a penguin. This agreed to my answer
}
}
The swim method is in my penguin class that extends my bird class
public class Penguin extends Bird{
public void fly() {
System.out.println("The penguin flies");
}
public void swim() {
System.out.println("The penguins swims");
}
public void doStuff() {
System.out.println("Doing some stuff...");
swim();
}
Upvotes: 1
Views: 1637
Reputation: 80603
Boolean values can only be assigned one of two values, true
or false
. The first thing I would recommend is creating an appropriate class variable to hold this 'state' information:
protected boolean isHappy;
You can also initialize this variable with a default value. Based off your scenario, you want to start off in an 'unhappy' mood. So lets say that isHappy is false
.
protected boolean isHappy = false;
Now you have a class variable that you can set to a value of true to indicate 'happiness', and false to indicate 'unhappiness'. Create getters and setters for it, then move on to the next step - changing its values in your other methods.
So, a Bird is happy when flying. You will want to update your state information to reflect this:
public void fly() {
// Change the 'mood' of the bird
isHappy = true;
System.out.println("the Bird flies");
}
If the bird is a penguin, then it doesn't like swim (not sure I buy that, but lets roll with it)
public class Penguin extends Bird {
public void swim() {
// Change the 'mood' of the penguin bird
isHappy = false;
System.out.println("The penguins swims");
}
}
Now, if you really wanted to go deeper into object oriented principles, then you would define a default swim behavior in your top level Bird
class, then override it to more appropriate behavior in your Penguin
subclass.
public class Bird {
protected boolean isHappy = false;
public Bird() {
super();
}
public void fly() {
isHappy = true;
System.out.println("The bird flies!");
}
public void swim() {
isHappy = false; // Most birds don't like to swim!
System.out.println("This bird is going nowhere near the water");
}
}
public class Penguin {
/*
* We will override the default swim behavior for penguins. This is because
* penguins really do like to swim. All the other behavior defined by the
* Bird class is left as is, since it is still true for Penguins
*/
@Override
public void swim() {
isHappy = true; // Penguins love the water
System.out.println("The penguin swims!");
}
}
You can see the power of object oriented principles in the code above. You define most of the default behavior in your superclass, then selectively override (or change) just the pieces that are appropriate, in your subclass.
Upvotes: 3
Reputation: 1636
Change the mode
field to protected. You'd better change a name, like isHappy
. And then add swim
method to Bird
class, so you can declare a bird and a penguin like
Bird bird = new Bird();
bird.swim(); // unhappy
bird = new Penguin();
penguin.swim(); // happy
The complete code is
public class Bird extends Animal
{
protected boolean isHappy = false;
public void fly()
{
System.out.println("the Bird flies");
isHappy = true;
}
public void layEgg() {
System.out.println("The bird layed an egg");
isHappy = true;
}
public void swim() {
System.out.println("The bird can't swim");
isHappy = false;
}
public void doStuff() {
System.out.println("Doing some stuff...");
fly();
layEgg();
}
}
The Penguin
class my like this
public class Penguin extends Bird{
public void fly() {
System.out.println("The penguin flies");
isHappy = true;
}
public void swim() {
System.out.println("The penguins swims");
isHappy = true;
}
public void doStuff() {
System.out.println("Doing some stuff...");
swim();
}
}
Upvotes: 1
Reputation: 31
When you create your object, you should create a Penguin object and assign it to a variable of type Bird.
Bird myBird = new Penguin();
If you want it to also print out "The bird flies" when you call myBird.fly(), add super() to the Penguin.fly() method like below.
Boolean can only be true or false. The convention is to use isAdjective. So you would make your boolean isHappy and set it to false in the parent class where you have it. Next you change the isHappy variable in your child class.
public void fly() {
isHappy = true;
System.out.println("The penguin flies");
super();
}
public void swim() {
isHappy = false;
System.out.println("The penguins swims");
}
Upvotes: 3