David T.
David T.

Reputation: 23399

Java abstract field pattern

I'm not really sure how to describe this pattern that i want, but I would like something like this:

public abstract class Parent {
    protected abstract boolean foo = false; //this doesn't compile
}

public class Child1 extends Parent {
    protected boolean foo = true;
}

how do i do this?

Imagine i have 1 Parent class, but like 20 Child classes. for the vast majority of the children, the foo should be false. However, Child1 (and a few others) is the weird one with foo = true;.

what is the most proper OO Design and yet code effecient way to do this?

Upvotes: 1

Views: 289

Answers (5)

Jojo John
Jojo John

Reputation: 400

I think you need to do like this

public abstract class Parent {

    protected boolean check = false;

}

public class Child extends Parent 
{
    public void method()
    {
        this.check=true;
    }

}

// You can put it in constructor also

Upvotes: 1

Eric Jablow
Eric Jablow

Reputation: 7899

Don't use a field then. Look at this combination of classes:

public abstract class Vehicle {
    public abstract boolean isAerial();
}

public abstract Flyer extends Vehicle {
    @Override
    public final boolean isAerial() {
        return true;
    }
}
// Add Airplane, Helicopter, Dirigible, Rocket, etc.

public abstract Terrestrial extends Vehicle {
    @Override
    public final boolean isAerial() {
        return false;
    }
}
// Add Car, Truck, Boat, Bicycle, etc.

Upvotes: 0

Ted Hopp
Ted Hopp

Reputation: 234847

You can do this with a constructor or two:

public abstract class Parent {
    protected boolean foo;
    protected Parent() {
        this(false); // initialize foo to default value
    }
    protected Parent(boolean fooValue) {
        this.foo = fooValue;
    }
}

public class Child1 extends Parent {
    public Child1() {
        super(true);
    }
}

public class Child2 extends Parent {
    // no explicit super(boolean) call in c'tor gives foo the default value
}

Upvotes: 2

rgettman
rgettman

Reputation: 178293

First, instance variables cannot be abstract, only methods can.

To have overriding behavior, you need methods. I would define a method, say, isFoo in Parent that is defined to return false. No subclasses would need to override it, except for the "weird one"(s), which can override it to return true.

Alternatively, you can have a subclass of Parent called WeirdOne (doesn't have to be that name of course). The only thing it does is override isFoo to return true. Then Child1 and any other "weird" classes subclass WeirdOne. This way, it's only overridden in one place.

Upvotes: 2

SonicARG
SonicARG

Reputation: 517

If you want to extend the Parent class using Child1 class, you have to type:

public class Child1 extends Parent {

}

About the foo parameter, you can't set it abstract since is not a function (that is, only functions can be declared abstract). You can, however, override it in subclasses.

public abstract class Parent {
    protected boolean foo = false;
}

public class Child1 extends Parent {
    @Override
    protected boolean foo = true;
}

Upvotes: 0

Related Questions