Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (494)
Games in Android Showcase (114)
games submitted by our members
Games in WIP (563)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Hello JGO & Class/Interface Inheritance Discussion  (Read 288 times)
0 Members and 1 Guest are viewing this topic.
Offline TeamworkGuy2

Junior Member


Medals: 10



« Posted 2014-02-04 15:59:39 »

Hello JGO! I've been lurking for a few months and finally decided to get an account;  Smiley

First off, to all the awesome people who's posts I've been reading, thank you; JGO is a great community with a lot of great information and advice;
In no particular order, some of the awesome people I remember at the moment include: Riven, princec, theagentd, ClickerMonkey, wessles, Danny02, and all the other awesome people who's names I'm forgetting;

I'm currently a university student and spend every spare minute I can programming;  I like learning protocols such as HTTP, FTP, UDP, etc, and used to love benchmarking stuff;
Although I've benchmarked so many different things in Java that I've worn myself out of the habit (most results were that Java is a well balanced language and, other than string concatenation/manipulation, everything in Java is relatively fast);

----
I thought I'd post a topic along with my hello thread; The topic/question is: class inheritance (extends) and inheritance (implements) as it may relate to code readability, developer efficiency, etc;

Class inheritance is cool, we wouldn't have Swing as we know it without class inheritance;  However class inheritance seems problematic for a few reasons;  Serialization causes a lot of corner cases for classes; From security risks to enforcing correct serialization patterns in every leaf of an inheritance tree;  Class inheritance also reveals implementation details;  Protected fields and methods are accessible and the inner state of your class can be modified in ways you might not have expect when wrote the parent class;

Class inheritance is good and necessary for Java as a language;  However, I'd like to share some of the experiences I've had going 'class inheritance-less' in some of my code;

Interfaces hide implementation details;  Some time ago I was coding some structures for rendering a scene and using lwjgl's Renderable interface everything could be treated the same, stored in the same collections, etc; From complex frustrum culled scene views to simple sprites, everything identical on the surface, but different underneath;
Of course none of this is new to anyone who has used interfaces for more than a few weeks; I'm not new to it either;  However, I started wondering what challenges a developer would face if they only used interface inheritance;  Interested in this, I started coding without using class inheritance;

Some of the resulting patterns that have emerged are immutability and composition;  I find that coding large, complex classes is much easier when their design is broken down into modules rather than parent-child inheritance relationships;

For example, if a class represents a UI text field or button, it will probably need to fulfill a lot of roles;  You could recreate Swing's design pattern with thousands of lines of code nested in class inheritance trees many classes deep, or you could just create your object using a bunch of simpler objects that each provide a piece of your functionality; A button that needs a position, background, foreground text/icon, keyboard/mouse listeners, etc, could simply have fields for each of these pieces;

The basic concept in code tends toward composition:
1  
2  
3  
4  
5  
6  
7  
8  
Button implements ... {
   private Renderable backgroundModel;
   private UiElement foregroundModel; // which itself contains renderables for icons, text, etc.
  private Position pos;
   private InputHandler<Keyboard> keyHandler;
   private InputHandler<Mouse> mouseHandler;
...
}


Again, this is not revolutionary, I'm not even telling you anything you probably don't already know;  Just taking a one-sided programing technique and showing just how awesome it can be;

Maybe I just haven't had enough time with interfaces to become less enthusiastic about their usefulness, but so far interfaces are making my code much cleaner and far more reusable;

Your experiences and thoughts are welcome; Please no arguing about what percentage of code should be extends vs. implements or whether one should be used exclusively; Both patterns/techniques are perfectly valid and can coexist seamlessly in Java;  However, do you favor one over the other and do you find that it saves you time/effort by doing so?
Offline Troubleshoots

JGO Knight


Medals: 36
Exp: 7-9 months


Damn maths.


« Reply #1 - Posted 2014-02-04 16:28:31 »

I don't think the discussion should be about one type of inheritance over the other, but instead about when to use one type of inheritance over the other. Favouring a certain type of inheritance gives you restrictions; you should always keep an open mind.

Interfaces allow you to abstract out a set of behaviours, i.e they allow you to share the same type of behaviour among several classes which implement those behaviours in a certain way. Classes on the other hand allow you to inherit a fixed set of states and behaviours. Abstract classes allow you to share a fixed set of states and behaviours but also allow you to abstract out a set of behaviours.

If there's benefits of a certain type of inheritance then you should consider using that type. That said, you shouldn't abuse inheritance. If there's little or no benefit of polymorphism, then you should be considering composition over inheritance.

To answer your original question though, I find myself using abstract classes most often as they allow the greatest flexibility.

Why are all OpenGL tutorials written in Brainf**k?
Offline TeamworkGuy2

Junior Member


Medals: 10



« Reply #2 - Posted 2014-02-04 16:41:01 »

Good point Troubleshoots, all types of features offered by Java have specific situations which they solve best;
As the first post mentions in the last paragraph, I wasn't saying that one feature is exclusive over another, just that I've seen a lot of cool patterns emerge from only using interfaces; Patterns which have made my code far easier to maintain and reuse;  No more unknown super calls or hard to find fields accessed in child classes but located in parent classes, no more need to remember the internal state of every class' parent class and no more bugs caused by modifying a parent class' state at the wrong time;

The discussion was geared toward what types of experiences others have had and whether they have found that certain development patterns or techniques (related to inheritance) improve their coding life;

Thanks for your input, I've actually very rarely used abstract classes, maybe I should try it out more;  I dislike having to hard coding behavior into any class which will be used as a reference type;
idk when it started, but whenever possible I prefer all my references to be interface types;  It's so easy to switch out an interface's default implementation without changing any other code;
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Dwinin (20 views)
2014-09-12 09:08:26

Norakomi (55 views)
2014-09-10 13:57:51

TehJavaDev (64 views)
2014-09-10 06:39:09

Tekkerue (32 views)
2014-09-09 02:24:56

mitcheeb (54 views)
2014-09-08 06:06:29

BurntPizza (38 views)
2014-09-07 01:13:42

Longarmx (24 views)
2014-09-07 01:12:14

Longarmx (28 views)
2014-09-07 01:11:22

Longarmx (28 views)
2014-09-07 01:10:19

mitcheeb (36 views)
2014-09-04 23:08:59
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!