1

I have a dilemma with this, I am designing a project and basically it will have a baseclass -> entityclass relationship where the entity class inherits from the baseclass. Now the base class is abstract and would have something like:

abstract class MyAbstractClass{

//All this methods need to be implemented
abstract int getHouseId(int id);
abstract string getHouseName(String s);

}

Then I would like to do something like:

 public class MyChildClass extends MyAbstractClass{

    private int _hId = 0;
    private String _hName = "";

    public int getHouseId(int id)
    {
       if(id > _hId)
       {
          return _hId;
       }
       return id;
    }
    public String getHouseName(String s)
    {
        if(s.Equals(_hName))
        {
           return _hName; 
        }
        return s;
     }

      //AND YOU HAVE YOUR SETTERS SOMEWHERE HERE
   }

Now please excuse if syntax error, I just made it on the fly, my question is is it better to declare the variables in the abstract class and set and get stuff from there, or create the variables in the child class?. I am confused by it, also if using it in the abstract class, how should they be declared in the abstract class, can I make them private there and add properties to get the values?, or it is better to do it in the child class?. Basically I want to enforce a set of programmers to always use the abstract class as the functions that they HAVE to implement.. just a bit confused about the concept. I tried to google it but did not see many articles about variables and properties in an abstract class, maybe it should be used in the child class. Thank you for your help making me understand this.

Syed Waqas Bukhary
  • 5,130
  • 5
  • 47
  • 59
user710502
  • 11,181
  • 29
  • 106
  • 161

3 Answers3

2

is it better to declare the variables in the abstract class and set and get stuff from there, or create the variables in the child class?.

The idea is to minimize the duplication. If you are duplicating the the logic and variables in each subclass, then better have it in super-class.

...if using it in the abstract class, how should they be declared in the abstract class, can I make them private there and add properties to get the values?,

The general practice is to make the variable private in abstract class. And modify/access them using public/protected getter/setter.

Basically I want to enforce a set of programmers to always use the abstract class as the functions that they HAVE to implement..

So, here you go. Have the attribute and getter/setter in abstract class. And make only those methods abstract that you want your devs to implement with their own logic.

Nishant
  • 54,584
  • 13
  • 112
  • 127
1

The whole idea of abstract classes is that they can contain some behaviour or data which you require all sub-classes to contain. So you could keep them private and then provide getters and setters.

Or if you have constants that needs to be used by all subclasses then define them as public static final.

Have a look here:

Java - Abstract class to contain variables?

Community
  • 1
  • 1
John Eipe
  • 10,922
  • 24
  • 72
  • 114
1

It's really up to you, but if a certain functionality is required by each derived class then it'd make sense to add it to your abstract base class. This includes variables, constants, methods, etc.

Making a class abstract doesn't prevent you from implementing getters and setters either, you can encapsulate these variables within the abstract base class, forcing derived classes to call these methods in order to access the variables. You'll find this to be a common practice. Your MyChildClass could actually be the abstract base class and will do just this.

If you don't need or want variables declared in your abstract class and you want the derived classes to simply contain implementations of certain functions then an alternative is making it an interface instead.

AusCBloke
  • 18,014
  • 6
  • 40
  • 44