Required class variables in Java? - java

So say, for the purposes of illustration that I have an abstract class Animal, and every Animal has a numberOfLegs.
Dog extends Animal and all dogs have a numberOfLegs of 4.
Is it possible to make numberOfLegs...
Static
Required (everything that extends Animal has to define numberOfLegs, or there is some default value)
Accessible from Animal (this one I'm not as concerned about, but if say the method walk() was called from Dog, could Animal view the number of legs without having to pass an extra value?)
Sorry if this is a strange question, and yes, I understand that I could do this easily in other ways, such as making it an instance variable instead of static, but I'm just kind of curious if there's a better way.

Neither static variables or instance variables participate in polymorphism. Just declare an abstract method, e.g. int getNumberOfLegs(), in the abstract class. Your Dog class could be:
class Dog extends Animal {
private static final int NUMBER_OF_LEGS = 4;
public int getNumberOfLegs () {
return NUMBER_OF_LEGS;
}
}

Is it possible to make numberOfLegs: Static
This would make every instance of a class that extends Animal have the same numberOfLegs. See: here for an explanation.
public abstract class Animal
{
private static int NUMBER_OF_LEGS = 4;
}
Is it possible to make numberOfLegs: Required
Yes, you just have to make a property in the abstract class and initialise it in the constructor.
public abstract class Animal
{
private int numberOfLegs;
public int getNumberOfLegs()
{
return this.numberOfLegs;
}
public Animal(int numberOfLegs)
{
this.numberOfLegs = numberOfLegs;
}
}
public class Zebra extends Animal
{
public Animal(int numberOfLegs)
{
super(numberOfLegs);
}
}
everything that extends Animal has to define numberOfLegs, or there is some default value
Furthermore, if you wanted a default value for number of legs, you could inlcude a constructor for Animal without a numberOfLegs parameter and set it to a value, say 4.
public abstract class Animal
{
private int numberOfLegs;
public Animal()
{
this.numberOfLegs = 4;
}
}
Is it possible to make numberOfLegs: Accessible from Animal
Yes, you can call the method (if it's abstract in the Animal class) from any object that extends Animal
Zebra z = new Zebra(4);
System.out.println(z.getNumberOfLegs());

Related

Abstract class or casting?

I'm trying to figure out whether implementing individual methods of a subclass in an abstract superclass, or casting is the better way to go about the following scenario.
Suppose I have an abstract class Animal which has two subclasses, Dog and Cat and a Main class where I save objects of Dog and Cat in an Animal array. The following is how I would go about using methods of the subclasses in a more generalized array.
class Main{
public static void main(String[] args){
Animal[] animalArray = new Animal[2];
animalArray[0] = new Cat();
animalArray[1] = new Dog();
for (Animal a : animalArray){
if (a.getClass().equals(Dog.class){
((Dog)a).bark();
} else {
((Cat)a).meow();
}
}
}
}
However a friend suggested that casting isn't best practice, and that I should define each method in the abstract superclass in the following way:
public abstract class Animal{
public abstract String meow(){
return null;
}
public abstract String bark();
return null;
}
}
After setting the return values of these methods to null I would need to use #Override and implement them in the respective subclasses.
Which way is better? I'm afraid the abstract class will be too large and will have methods assigned to subclasses that don't make sense (even if all they do is return null). I think by using casting I can make more precise uses of the methods.
meow() and bark() shouldn't be defined in the Animal class. These methods are specific to Cat and Dog classes.
You should define an abstract method as shown below, in the Animal class and override it in the sub classes.
public abstract class Animal {
public abstract String action() {};
}
public class Dog extends Animal {
#Override
public String action() {
//your implementation (bark)
}
}
public class Cat extends Animal {
#Override
public String action() {
//your implementation (meow)
}
}
Hope it answers your query.

How to avoid redundant code for static methods

Let's assume I have the following classes:
public class Cat {
private final String noise = "meow";
pulic static void makeNoise(){
System.out.println(noise);
}
}
public class Dog {
private final String noise = "woof";
pulic static void makeNoise(){
System.out.println(noise);
}
}
As you can see, these two classes share pretty much the same code. To remove redundant code, I'd create a parent class like this:
public abstract class Animal {
protected final String noise;
public Animal(String noise) {
this.noise = noise;
}
public void makeNoise() {
System.out.println(noise);
}
}
public class Dog extends Animal{
public Dog(){
super("woof");
}
}
Now unfortunately I'm running into a two problems:
Since make noise of animal can't be static anymore, as the constants
will have to be assigned through the constructor of animal, you will
need to create a cat or dog to get the noise of that animal. Even
though it is a constant.
The method makeNoise() need's to work in the class Animal - which
doens't have a noise per default.
A possible solution would be something along the line like this:
public abstract void makeNoise();
which is neither allowed, nor would it erase the need to copy the code into each and everyone of the children of Animal.
How would you erase the need to have redundant code in the children of animal while keeping the method makeNoise static?
Static methods in Java can't be overridden in subclasses.
If you define a static method in a subclass with the same signature as the static method in the parent class, the method is not overriding the parent method is hiding it. The methods in the parent and the child class has no relation to each other.
In your example, if method static void makeNoise() exists in Animal and any subclass define the method as well, the subclass is just hiding the makeNoise method in Animal.
In your example, the best you can do with static methods is:
public static void makeNoise(String noise) {
System.out.println(noise);
}
And invoke the method this way:
Animal.makeNoise(Cat.NOISE); // A constant NOISE is defined in each subclass
If the method makeNoise is non-static, inheritance could be used to use a different noise in each subclass:
public abstract class Animal {
protected String noise;
protected Animal(String noise) {
this.noise = noise;
}
public void makeNoise() {
System.out.println(noise);
}
}
public class Cat extends Animal{
public static final String NOISE = "meow";
public Cat() {
super(NOISE);
}
}
Why would you represent a real world object behavior with a static method? Each animal has it's own behavior so you want to differentiate between them.
enum AnimalBehavior {
MEOW, WOOF, ROAR
}
You could use an enum which contains every animal behavior.
Also consider the following situation: A wolf keeps howling during a full moon night. He keeps doing it until he gets exhausted. You want to track a bar which indicates the level of energy your wolf has.
private int energy = 100;
public static void wolfHowl() {
System.out.println(AnimalBehavior.ROAR);
energy = energy - 10;
}
This won't work technically because you're using static methods.. so keep in mind how you design your stuff since that wolf could actually howl without getting tired until someone gets really pissed off.

Abstract Class Extending Concrete Classes

I have an abstract class that extends a concrete class. But I'm confused about how to use it. How am I suppose to instantiate the concrete class with the methods in the abstract class like I would normally do the other way around? Or is there another way to instantiate the classes?
An abstract class always extends a concrete class (java.lang.Object at the very least). So it works the same as it always does. If you want to instantiate it, you will have to subclass it with a concrete implementation of those abstract methods and instantiate it through the concrete class.
Just like you always do. This isn't a special case.
You can have a sort of implementation. What I mean by this is like:
Let's say you have an Animal class. The Animal class has a method names jump() and then another class that extends Mammal. The Mammal class is abstract. What my understanding is that you would like whatever class extends Mammal to HAVE to override the jump() method. This is what I believe is your question. To achieve this, I would say to create an abstract method and call that in the original method. What I mean by this is like so:
public class Animal
{
public final String name;
public final int weight;
public Animal(String name, int weight)
{
this.name = name;
this.weight = weight;
}
public void jump()
{
System.out.println(name + " Jumped");
}
}
Then you have the Mammal class:
public abstract class Mammal extends Animal
{
public Mammal(String name, int weight)
{
super(name, weight);
}
public abstract void jumpMammal();
#Override
public final void jump()
{
jumpMammal();
}
}
If any class attempts to override the Mammal class, they are required to override the jumpMammal() method, therefore running in the jump() method.
for example lets say we have a foo abstract class extending fooFather class .
public abstract class Foo extends FooFather{
}
then we create a subclass that extends Foo like this:
public class Foo2 extends Foo{
}
And initialize it like this
Foo aFoo=new Foo2();

Java Extending a class and setting values

Animal Base Class
public class Animal
{
protected String pig;
protected String dog;
protected String cat;
public void setPig(String pig_)
{
pig=pig_;
}
public void setCat(String cat_)
{
cat=cat_;
}
public void setDog(String dog_)
{
dog=dog_;
}
}
AnimalAction Class
public class AnimalAction extends Animal
{
public AnimalAction(String pig, String cat, String dog)
{
super.pig = pig;
super.cat = cat;
super.dog = dog;
}
}
Would this be the correct way to set protected variables? Is using protected variables the correct way to do this? Is there a more professional OO way to do?
You can use private variables instead of protected. This will be more apt.
You can use the constructor to set the value of the super class.
Edited:
public class Animal{
private String pig;
private String dog;
private String cat;
public Animal(String pig,String dog,String cat){
this.pig=pig;
this.dog=dog;
this.cat=cat;
}
}
public class AnimalAction extends Animal
{
public AnimalAction(String pig, String cat, String dog)
{
super(pig,dog,cat);
}
}
You should be able to use this.pig etc, since you inherited the protected members. You could also actually call the public setPig(...) methods.
There is nothing wrong in using protected member variable and then inherit them in subclass .
But If a developer comes along and subclasses your class they may mess it up because they don't understand it fully. With private members, other than the public interface, they can't see the implementation specific details of how things are being done which gives you the flexibility of changing it later.
By providing protected member variables you are just coupling tight between you subclass and superclass.
The less your member variables can be seen outside the class, the better. I would make the class variables private and make the getters public (or as required) & the setters protected.
There's no need to use the super prefix, or any other prefix, to access protected variables.
BTW - I disagree with Thomas on one point - do not call the setter methods of the superclass in your constructor. Using non-final setters in a constructor may have ugly effects if a subclass overrides them. Then they could be called on an incompletely constructed object. But you should consider making your setters final if you don't mean them to be overridden.
The principle of "design for inheritance or forbid it" is explained in the Effective Java book by Joshua Bloch.
Your example is quite confusing, but it would work. I'll give another example:
// use capitals for classes/interfaces/enums, lower case for methods/fields.
public class Animal
{
protected String name;
protected int numberOfFeet;
public Animal(String name)
{
this.name = name;
}
public void setNumberOfFeet(int numberOfFeet)
{
this.numberOfFeet = numberOfFeet;
}
}
public class Dog extends Animal
{
public Dog()
{
super("dog"); // call the constructor of the super class.
// because Dog extends Animal and numberOfFeet is protected, numberOfFeet becomes part of "this" class.
this.numberOfFeet = 4;
}
}
//Now you can create instances of Animal like:
Animal bird = new Animal("bird");
bird.setNumberOfFeet(2);
//Or use Dog to create an animal "dog" with 4 feet.
Animal dog = new Dog();
//after an accident
dog.setNumberOfFeet(3);

How can I force subclasses to redefine a constant in Java?

How can I have a generic abstract class, let's say Animal, and have it implement a constant (or a variable; it doesn't mind) which must be redefined by all subclasses?
Example:
abstract class Animal {
private static int venerableAge;
}
And force Dog to be defined something like
class Dog extends Animal {
private static int venerableAge = 10;
}
I don't want different subclasses to be able to read nor write each others' value. Just the one of themselves.
I.e., each class must have its own static "instance" of the variable. And no one will access the parent's.
Is that possible in Java?
The trick is to do this with getter methods rather than directly with fields:
abstract class Animal {
abstract int getVenerableAge();
}
class Dog extends Animal {
private static int venerableAge = 10;
public int getVenerableAge() {
return venerableAge;
}
}
EDIT:
How about letting the constructor do the contract binding:
abstract class Animal {
public Animal(int venerableAge){
//carry out sense checks here. i.e.:
if (venerableAge < 0) { /* doSomething */ }
}
}
class Dog extends Animal {
private static int venerableAge;
public Dog(int age) {
super(age)
venerableAge = age;
}
}
Now Dog and Cat are both forced to be created with venerable ages, but (as implemented above) can't see each others value.

Categories

Resources