-2

I had a question on an interview like this...

What if, lets say JAVA, decided to remove inheritance from the programming language, and you have over 1000 classes that use inheritance (superclass). How would you fix that if you want to change something in the superclass (for example a method or more methods). The fastest and most efficient way?

What do you think? :)

EDIT: Hey, guys, I know its not logical and Java would not do that and its the basic concept of OOP... but that a mind bender question... how would you solve the problem where you "shared" the code all around the app and now you dont have this functionality any more. How would you solve it?

klanc
  • 241
  • 2
  • 15
  • Java will never decide it. They support almost all the deprecated stuff since Java 1.0 even though it's a big pain for the JDK and HotSpot developers. So you can sleep well. – Tagir Valeev Jun 01 '15 at 14:38
  • 2
    I think that the question is nonsensical. If the programming language doesn't have inheritance, then a class cannot have a superclass. – Stephen C Jun 01 '15 at 14:39
  • all classes in java (i.e. also those you didn't write) use inheritance. java is based on the principle that all classes inherit from Object. (also, would subtyping still exist?) – njzk2 Jun 01 '15 at 14:39
  • I think the question is just about the `final` keyword or something, don't go to philosophical here. – Eugene Sh. Jun 01 '15 at 14:40
  • Yes, thats the point... okay you dont have superclass but how would you handle the code then without changing that? – klanc Jun 01 '15 at 14:40
  • 2
    I think the interviewer want you to appreciate the inheritance. So probably without it, you have to change the 1000 classes one by one..lol – Surely Jun 01 '15 at 14:41
  • What kind of interviewer would ask such a question? – Vince Jun 01 '15 at 14:44

5 Answers5

5

I believe what the question is driving at is the concept of favoring composition over inheritance.

Say we have this hierarchy:

class Parent{
   public String getName(){
      return "xyz";
   }
}
class Child extends Parent{}

We could achieve a similar relationship through composition:

class Parent{
   public String getName(){
      return "xyz";
   }
}
class Child{
   private Parent myParent;
   public String getName(){
      return myParent.getName();
   }
}

This is an oversimplified example, but the basics are there. For more info, see the answers to this question.

Community
  • 1
  • 1
Kevin Workman
  • 41,537
  • 9
  • 68
  • 107
1

Since this is a "What-If" question I think is valid trying to answer it

Since removing inheritance will mean to remove using extends and implements using a interface or extending an abstract class will no be posible

My approach: Substitute the superclass with a class that has public static properties and public (static when posible) methods making it a common access point for all the other classes to call and replace the super calls to those methods.

In this way most of the logic an properties will still be into a single class.

Mauricio Gracia Gutierrez
  • 10,288
  • 6
  • 68
  • 99
  • Yep. But there is no way not to mess with the rest 1000 classes as well.. – Eugene Sh. Jun 01 '15 at 14:46
  • @EugeneSh. I dont see that to be a limitation in the original question. They asked how would you solve it without inheritnace – Mauricio Gracia Gutierrez Jun 01 '15 at 14:47
  • Actually it is a limitation as I see it.. "How would you fix that if you want to *change something in the superclass* (for example a method or more methods)". But I don't really see a legit answer other than this. – Eugene Sh. Jun 01 '15 at 14:50
0

First of all there would be no OOP concept without inheritance, so Java team will never do that because every class in java extends java.lang.Object.

IMHO, they want to see whether you understand the inheritance. Because if inheritance is removed, then there is no concept of Super class at all. So there is no point in thinking about 'fastest and efficient way'.

Let's assume, there is no inheritance, and you modified the code to remove the errors by using the class associations. Any popular IDE will do that very easily.

For example, in Eclipse, Select the method -> Press Alt + Shift + R (to modify all references). So another aim of that question might be to test your knowledge on IDE usage.

K139
  • 3,654
  • 13
  • 17
0

Under your assumption the fastest and most efficient thing is to stay with current Java version and/or wait till some team of enthusiasts will fork the OpenJDK to evolve it in more sensible way than removing the inheritance.

Tagir Valeev
  • 97,161
  • 19
  • 222
  • 334
0

There are alternative patterns to inheritance even if inheritance is supported by the language, and certainly when inheriting from a class causes the 2 classes to be tightly coupled it can be worthwhile considering if inheritance is the always the right approach.

As an example I recently read a wonderful article/chapter on the type pattern (available here - http://gameprogrammingpatterns.com/type-object.html). There are a number of potentially applicable patterns within the book that could be of use in this manner definitely worth a read.

Of course applying this pattern (or any number of alternative patterns) to all 1,000 classes might be a bit of a long exercise.

Another option would be to implement a simple form of inheritance yourself to replace the one removed. This isn't all that uncommon for example there are a number of libraries for JavaScript and Lua (I'm sure there are many others) that add support for class like behaviour including inheritance. How difficult this is to achieve will depend on the properties of the language.

There are a very large number of caveats here including performance and supporting every feature of inheritance the 1,000 classes rely on - but in a world where Java drops inheritance as a feature anything is possible.

Anthony Blackshaw
  • 2,549
  • 2
  • 16
  • 20