Monday, January 30, 2017

Introduction to Java's Inheritance and Interfaces - Part 1

Before beginning to learn Java as a programming language, it is equally important to learn the evolution of the Object Oriented Programming from the days of Machine language. Here, I am just providing a tiny history of how it evolved all along. Note that it is very very tiny. There are books written all over about these Programming Paradigms.

During the days of Machine Languages, the coding is very difficult as it forces developers/coders to type instructions in binary digits like below:

10110000 01100001
01100010 11100101
00100010 10101001

Hardly, readable isn't it?. That is when the Human-Readable-Mnemonics are invented to easily remember the binary instructions.

MOV AL, 61h
CPY CH, 21H
ADD CY, CB
SUB AB, NY

Mnemonics have somewhat reduced the difficulties involved in binary languages but not completely solved the problems. Lets study a case. Say we want the processor(CPU) to do the following:
1. Read 2 numbers
2. Add the numbers
3. Store the result in another variable

This can be coded straight away as follows:
READ A       //Read data from variable A
READ B       //Read data from variable B
ADD A,B      //Add contents of B to A
MOV A, AH    //Save the contents of A to memory location AH

This is sufficient for now. But, what if we want to perform the same operation multiple times in future? The code is going to be a long list of repeated instructions.

READ A
READ B
ADD A,B 
MOV A, AH

UPD A
UPD B
READ A
READ B
ADD A,B 
MOV A, AH

UPD A
UPD B
READ A
READ B
ADD A,B 
MOV A, AH

UPD A
UPD B
READ A
READ B
ADD A,B 
MOV A, AHA

And so on. This repetition of code is strictly unwanted and highly inefficient. So, in order to resolve this, what can be done is ..Group the repeated block of statements as Function. Then assign a name to it and call the function whenever that block of code is required(is to be repeated). This type of Code-Reuse is called Functional Abstraction also called Process Abstraction. A concept that changed the style of programming since then.

Now the above code can be re-written as:

function add(A,B){
UPD A
UPD B
READ A
READ B
ADD A,B 
MOV A, AH
}

add(10,20)
add(20,30)
add(30,40) ..And so on. 

Note: The repetition of above function call - add(10,20) - can be put in a loop. Loops and Conditional Statements etc are other programming language constructs.

Functional Abstraction: The concept of hiding implementation details from the user of the function. Means, a developer who wants to use add(A,B) function need not know about the actual code inside the function block. All he/she needs is how to use the function. What parameters are to be sent(Eg: A, B) and what it returns.

This paved the way for Functional Programming.

Well! Not stopping at this, then came along another type of abstraction called Data Abstraction - The central & dominant theme of Object oriented languages.

Data Abstraction: In its plain sense, hiding the data and only exposing the methods which operate on that data. So anyone who wants to perform any operation on the data, need to use those methods associated with it.
The binding of both data and its associated methods together in a single block is called Class and its instantiation is called Object - The building blocks of Object Oriented Programming.
Note: The newly created data structure - Class - is also termed as Abstract Data Type(ADT).

The abstraction in object oriented languages is implemented using 3 more concepts:
1. Inheritance
2. Polymorphism
3. Encapsulation
Strictly remember that Abstraction is not a feature of OOPs. This is as stated earlier the central theme.

Abstraction be it Process or Data is basically Code-Reuse if you observe carefully. How effectively the code can be re-used and maintained determines the efficiency of language. Hence, the 3 concepts revolve around achieving Data abstraction.

Inheritance(Def from Oracle): The idea of inheritance is simple but powerful: When you want to create a new class and there is already a class that includes some of the code that you want, you can derive your new class from the existing class. In doing this, you can reuse the fields and methods of the existing class without having to write (and debug!) them yourself.

Here is the example of code re-use in Inheritance:

Base Code:
public class Car{
String name;
String model;
String number;

public void getDetails(){

}

public void drive(){
system.out.println("This is default drive implementation");
}

public void accelerate(){
System.out.println("This is default accelerate implementation");
}

public void applyBraks(){
System.out.println("This is default applyBrakes implementation");
}
}


Re-use Sample 1:
public class BenzCar extends Car {

float price;
float company;


/*String name;
String model;
String number;*/    //Inherited from Parent Car. Code is re-used.

//Did not want the default code. Hence, code is written. Did not re-use.
public void accelerate(){
System.out.println("Accelerate slowly and steadily.");
}   


/*drive() & applyBraks()*/  //Inherited from Parent Car. Code is re-used

}


Re-use Sample 2:
public class BMWCar extends Car {

float price;
float company;
float brandName;


/*String name;
String model;
String number;*/    //Inherited from Parent Car. Code is re-used.

//Did not want the default code. Hence, code is written. Did not re-use.
public void accelerate(){
System.out.println("Accelerate at brisk speed but steadily.");
}

//Did not want the default code. Hence, code is written. Did not re-use.
public void drive(){
system.out.println("Drive or move at highest speed all the time");
}

/*applyBraks()*/  //Inherited from Parent Car. Code is re-used

}

No comments:

How J2EE components work together in any Container - Spring or Application Server

In a Spring+Jersey+Hibernate RESTful webapplication, we can spot various J2EE components - JTA, JPA, Java Bean Validation, JSON-B API for B...