I've recently started looking at Android development. This has brought me back into the world of Java software development. The last time I worked with Java, I'll admit, I didn't understand OOP nearly as much as (I think) I do now.

Having mainly used C# in my career, I'm noticing a startling difference in how inheritance is used Java and C#.

In C# it seemed like inheritance could be avoided in most situations. The task at hand could usually be accomplished by using concrete classes of the .NET framework.

In Java, from what I'm gathering from code samples, it seems like the Java framework supplies many interfaces or abstract classes that are then meant to be implemented/extended by the developer.

This seems to be too big a difference to just boil down to style. What is the reasoning behind this? I feel like I won't be writing clean Java code until I understand this.

Also, is this limited to just the Android SDK or is this a Java-wide approach to OOP?

Or put in another way,

What is it about the design of these two languages that (seems to encourage) more or less inheritance use than the other?

If the languages treat inheritance identically, and assuming my observation is valid, then it means this is related to the design of the frameworks/libraries and not the languages. What would the motivation be for this kind of design?

  • 1
    It's true. Java has way too many interfaces. I would love to know why this developer behavior is common for a specific language. I see this happening more often in Java projects than any other. There has to be a reason for this and not just an opinion.
    – Reactgular
    Nov 29, 2013 at 18:25
  • 1
    @MathewFoscarini it's only your opinion. In Java projects I participated, developers were inclined to avoid inheritance like a plague. Nothing authoritative here, only my opinion. Wanna poll?
    – gnat
    Nov 29, 2013 at 18:45
  • 2
    You, almost, never need to derive one class from another in Java. Instead, you can implement interfaces to achieve polymorphism and implement composed objects and proxy the method calls to achieve shaired functionality.
    – DwB
    Nov 29, 2013 at 19:20
  • 2
    @ThinkingMedia In general, Java is overrun witn OSS zealots / purists. Academic principles are the overriding concern. .Net developers are pragmatists that are concerned about getting the job done. They value working code more than the cleanest code.
    – Andy
    Oct 25, 2015 at 19:00

3 Answers 3


This seems to be too big a difference to just boil down to style. What is the reasoning behind this?

My understanding is that it largely is simply a stylistic decision. Well, perhaps not style, but the idioms of the language/environment. Java standard library developers followed one set of design guidelines and the .NET developers another (though they had the ability to see how Java's approach worked).

There is very little in the actual languages to encourage or dissuade inheritence. Only two things strike me as relevant:

  1. .NET introduced generics earlier in their lifetime, before too much non-generic code was implemented. The alternative is a lot of inheritence to type specialize things.

  2. A larger change was that .NET supported delegates. In Java you're stuck with (anonymous) inheritence to supply the most basic of variable functionality. This leads to a relatively large difference in how code is designed to either take advantage of delegates or to avoid the awkward inheritance structures needed to do it in Java.

  • 3
    I think this answer offers a very plausible explanation. Especially the point about anonymous inheritance in place of delegates. I've observed this a lot. Thanks.
    – MetaFight
    Nov 29, 2013 at 21:24
  • 4
    Don't forget anonymous types and lambda syntax/expression trees that were introduced in .NET 3.5. And of course opt-in dynamic typing in .NET 4. It's almost unquestionably a mixed-paradigm language now, not strictly object-oriented.
    – Aaronaught
    Nov 30, 2013 at 18:16
  • yes, in my experience having used both (including on mixed projects where the same people use both languages) people tend to prefer to use a larger number of smaller classes when coding Java, a smaller number of larger classes (trending towards god classes) when using C#. Having deliberately prototyping the same thing using the same style in both, you end up with a similar number of classes (using partial classes, you'd likely end up with more code files even when using C#).
    – jwenting
    Apr 10, 2014 at 13:43

These are very different languages, particularly in this area. Let's say you have a class. In this case, we'll make it a user control, something like a textbox. Call it UIControl. Now we want to put this in another class. In this case, since we're using a UI for our example, we'll call it the CleverPanel class. Our CleverPanel instance will want to know about things happening to its UIControl instance for various reasons. How to do this?

In C#, the basic approach is to check for various Events, setting up methods that will execute when each interesting event is triggered. In Java, which lacks events, the usual solution is to pass an object with various "event" handling methods to a UIControl method:

boolean  stillNeedIt =  ... ;
uiControl.whenSomethingHappens( new DoSomething()  {
    public void resized( Rectangle r )  { ... }
    public boolean canICloseNow()  { return !stillNeedIt; }
    public void closed()  { ... }
} );

So far, the difference between C# and Java is not profound. However, we have the DoSomething interface that is not needed in C#. Also, this interface might include a lot of methods that are not needed most of the time. In C#, we just don't handle that Event. In Java, we create a class that provides a null implementation for all the interface methods, DoSomethingAdapter. Now we replace DoSomething with DoSomethingAdapter and we don't need to write any methods at all for a clean compile. We end up just overriding the methods we need to make the program work right. So we end up needing an interface and using inheritance in Java to match what we did with events in C#.

This is an example, not a comprehensive discussion, but it gives the basics of why there's so much inheritance in Java as opposed to C#.

Now, why does Java work this way? Flexibility. The object passed to whenSomethingHappens could have been passed to CleverPanel from somewhere else completely. It might be something several CleverPanel instances should pass to their UIControl-like objects to aid a CleverWindow object somewhere. Or the UIControl could hand it off to one of its components.

In addition, instead of an adapter there might be a DoSomething implementation somewhere that has thousands of lines of code behind it. We could create a new instance of that and pass it. We might need to override one method. A common trick in Java is to have a large class with a method like:

public class BigClass implements DoSomething  {
    ...many long methods...
    protected int getDiameter()  { return 5; }

Then in CleverlPanel:

uiControl.whenSomethingHappens( new BigClass()  {
    public int getDiameter()  { return UIPanel.currentDiameter; }
} );

The open source Java Platform does a lot of this, which tends to push programmers to do more--both because they follow it as an example and simply in order to use it. I do think the basic design of the language is behind Sun's framework design and behind Java programmer's using the techniques when not using the framework.

It's real easy to create a class on the fly in Java. The class, anonymous or named, need only be referenced in one small block of code buried deep in one method. It can be created completely new or by slight modifications to a very large, existing class. (And the existing class can be top-level in it's own file, or nested in a top-level class, or defined only within a single block of code). The new class instance can have full access to all the creating object's data. And the new instance can be passed and used all over the program, representing the object that created it.

(As an aside, note that a big use of inheritance here--as in other places in Java--is simply for DRY purposes. It lets different classes reuse the same code. Note also the ease of inheritance in Java that encourages this.)

Again, this is not a comprehensive discussion; I'm just scratching the surface here. But yes, there is a startling difference in how inheritance is used between Java and C#. They are, in this respect, very different languages. It's not your imagination.

  • Note: You can avoid having to provide methods which don't anything by extending an abstract class full of empty implementations. This way you can selectively override the methods which do something. While not as ugly, it means another level of inheritance. Dec 6, 2013 at 8:37

There is absolutely no difference in the way inheritance is handled between Java and C#. When you will actually use inheritance or composition is definetely a design decision and by no way is something that Java or C# encourages or discourages. I would kindly suggest reading this article.

Hope I helped!

  • 5
    You're talking about the languages themselves, but I think the question is also about the libraries and common usage.
    – svick
    Nov 29, 2013 at 20:30
  • 4
    But there are quirks in the rest of the language that encourage extra inheritance in Java; see RalphChapin's answer
    – Izkata
    Nov 30, 2013 at 6:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.