Search code examples
javaobject

Abstract Class getter and setter usage


I have a question in regards to how I am planning to create my abstract models, I am using an abstract class as the base class and then subclasses, is it fine to use it like this?

I did google and found some samples but none was exactly like how I am doing it.

public abstract class Vehicle {

    String type;
    String color;

    public  abstract getType();

    public abstract setType(String type);

    public abstract String getColor();

    public abstract setColor(String color);
}

And then in my subclasses, I could do something like this:

public class Truck extends Vehicle {

    String pickupBed;

    public setPickupBed(String pickupBed) {
        this.pickupBed = pickupBed;
    }

    public getPickupBed(String pickupBed) {
        return pickupBed;
    }

    public setType(String type) {
        this.type = type;
    }

    public getType(String type) {
        return type;
    }

    public setColor(String color) {
        this.color = color;
    }

    public getColor() {
        return color;
    }
}

And another class, except cars, does not have a pickup bed.

public class Car extends Vehicle {

    public setType(String type) {
        this.type = type;
    }

    public getType(String type) {
        return type;
    }

    public setColor(String color) {
        this.color = color;
    }

    public getColor() {
        return color;
    }
}

Solution

  • You need to create an abstract getter and setter only if you need to change the behavior of getting and setting for your variables in the child classes (actually if you want to force them to do it).

    If all child classes use the same (common) getter and setter realization - you need to create getters and setters in the abstract class like in the simple (no abstract) parent class:

      public abstract class Vehicle {
    
        private String type; 
        private String color;
    
        public String getType(){
            return type;
        };
    
        public String setType(String type){
            this.type = type;
        }
    
        ........
    
      }
    

    In any case, if you need to include any logic for setter/getter in the future - you may override existing getter/setter:

    public class Car extends Vehicle{
    
      public setType(String type){
       if (type.equals("type1")){
           this.type = type + "_1";
       } else {
           this.type = type + "_2";
       }
      }
    
      public String getType(String type){
       return "{" + this.type + "}";
      }
      
     }
    

    Abstract methods:

    You need to add abstract methods only if you want to force class-child to implement some logic, f.e:

       public abstract class Vehicle {
    
        protected String type;
        protected String color;
        
        public String getType(){
            return type;
        };
    
        public String setType(String type){
            this.type = type;
        }
    
        public String getTypeDescription(){
            return "Type description: " + generateDescription();
        }
    
        protected abstract String generateTypeDescription();
    
        ........
    
       }
    
      
    
       public class Car extends Vehicle{
          String generateTypeDescription(){
              return "The description for the type: " + getType() + "The car...loren ipsum...";
          }
       }