Friday, 24 August 2012

What Is Java Inheritance? Why Is It So Important in Any Object Oriented Language?

In Object Oriented programming (i.e. the Java computer programming language) Inheritance is amongst the main principles that can be useful to use in the design of almost any application. Java inheritance allows for a neat way for you to identify interaction between the Objects (and in turn re-use your code to make sure you aren't required to type the same stuff continuously).
What is meant by Java Inheritance?
So what do I mean when I state that Inheritance permits you to define relationships among Objects? Well, allow us to think about some examples of Objects that DO have one or more relationships. Take into consideration the object Vehicle, this is a reasonably common term for:
1) Car
2) Bus
3) Motorcycle
Can you see how a Car is a Vehicle, how a Bus is a Vehicle, how a Motorcycle is a Vehicle etc. This "is a" relationship is just what Java Inheritance is all about. When you can verbally say something "is a" something else, then you really have a relationship between those two Objects, and therefore you have Inheritance.
So how exactly does Inheritance help us?
Well, using the cases given above, it means that a Car inherits behaviours and/or attributes from a Vehicle. So let us think about this for a second, what exactly is a Car? Well, it's actually a Vehicle with four wheels, doors and around Five seats. Alright, what is a Bus? It is also a Vehicle probably exceeding four wheels, doors and approximately 30 seats available. What is a Motorcycle? It's really a Vehicle having two wheels, absolutely no doors and one or two seats. At the time you begin to "map" out every one of the characteristics of the Objects, you'll start to observe just what similarities they may have (i.e. what they have in relation), and in addition what they don't have in relation to the other. This is very important with Java inheritance. Whenever your Objects share something in common, then this can be viewed as an attribute of the super class. Whatever they do not share shall be attributes of the child classes.
Exactly what is a super Class and what is a child Class?
With this illustration, the super class is the Vehicle object and the child classes are the Car, Bus and Motorcycle. The super Class is essentially the Object that will maintain each of the properties which are common. Therefore our Vehicle super Class will have the following components:
- Wheels
- Seats
Due to our examination of all of the varieties of Vehicles above, we note that all types of Vehicles have wheels and seats. But, recognize that I didn't put doors as part of the Vehicle object. The reason is, Motorcycles don't have doors! Doors would likely only be attributes of Cars and Busses, so we will have a door attribute in the Car and Bus Objects. Understand?
Coding Inheritance in Java
When you are coding this thing referred to as Inheritance in Java, what does it look like? Well, it can take the form of either an Interface or an Abstract Class. So, for the time being everything you should know about them is:
Interface = An outline (or skeletal framework) of your Object without an implementation
Abstract Class = An outline of an Object that can include an implementation




No comments:

Post a Comment