Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java inheritance

So I've been trying to find the proper way to get what should be pretty straightforward inheritance to function (the way I want ;)) and I'm failing miserably. Consider this:

class Parent
{
  public String name = "Parent";

  public Parent() {};
  public void doStuff()
  {
     System.out.println(name);
  }
}

class Child extends Parent
{
  public String name = "Child";
  public Child()
  {
    doStuff();
  }
}

I understand that Java doesn't do what I'd expect in this case as for some reason member variables aren't overridden the same way methods are but my question is what is the proper way to achieve the behavior I'd expect? I'd like my base class to contain some functions that operate on member variables provided/defined by a given subclass. I'd think get/sets could work, but then it destroys my ability to just call super() and have my base class do the construction needed making my subclasses contain a bunch of construction instead of isolating it to the base class.

Any constructive suggestions are welcome.

like image 651
user483315 Avatar asked Oct 21 '10 17:10

user483315


People also ask

What are the 4 types of inheritance in Java?

Types of Inheritance in Java: Single, Multiple, Multilevel & Hybrid.

What is Java inheritance?

Inheritance in Java is a mechanism in which one object acquires all the properties and behaviors of a parent object. It is an important part of OOPs (Object Oriented programming system). The idea behind inheritance in Java is that you can create new classes that are built upon existing classes.


1 Answers

You can always replace a variable with getter, if you want such kind of 'variable-inheritance'. Not sure if 'variable-inheritance' ((c)) is a good design pattern in general, though.

class Parent {
    public void doStuff() {
        System.out.println(getName());
    }

    public String getName() {
        return "Parent";
    }
}

class Child extends Parent {
    public Child() {
        doStuff();
    }

    public String getName() {
        return "Child";
    }
}

Or you could have protected constructor in Parent class, taking the name to use.

edit
I wouldn't call it a 'hack'. And version with passing parameter into parent constructor can be pretty elegant too.

class Parent {
    private final String name;

    // don't let use to invoke this constructor, only for child classes
    protected Parent(String name) {
        this.name = name;
    }

    // public constructor for users
    public Parent() {
        this("Parent");
    }

    public void doStuff() {
        System.out.println(name);
    }
}

class Child extends Parent {
    public Child() {
        super("Child");
        doStuff();
    }
}

do you have any idea why Java seems to have abandoned the concept of inheriting member variables?
Java has abandoned a lot of C++ concepts (starting from infamous multiple inheritance), because they didn't add that much value to the language, but increased its complexity.
Although I don't have anything against variable-inheritance, I'm not suffering without this concept either. And personally, I like the language simple and clean.

like image 146
Nikita Rybak Avatar answered Oct 10 '22 03:10

Nikita Rybak