Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

862
Views
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;
    }
}
over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

You need to create abstract getter and setter only if you need change behaviour of getiin and setting for your variables. If all child classes use the same getter and setter realization - you need to create getters and setters in the abstract class:

  public abstract class Vehicle {

    protected String type;
    protected 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 + "}";
  }
  
 }

You need to add abstract methods only if you want to force class-child 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 "bla...bla..";
      }
   }
over 4 years ago · Santiago Trujillo Report

0

You can do everything in an abstract class that you can do in a normal class except creating a new object only by using a constructor.

This means that you can simply copy and paste the getters and setters from your subclass into your parent class.

Depending on your implementation goals and style ideas, you can even make these getters/ setters final, meaning that subclasses simply cannot override them. This would look something like that: public final String getType(){...}.

over 4 years ago · Santiago Trujillo Report

0

Your super class(Vehicle)'s getter and setter shouldn't be abstract. Variables in the super class must be private or public.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!