James Gosling: idealism, the Internet and Java, Pt I

Sunday 29 April 2018

Java and OOPS concepts

Java is an object oriented programming language which can be downloaded and installed in your local Machine from java.com


To explain java binaries and tools requires block diagram which gives clear picture. For now I brief few important tools.

  • JDK - java development toolkit
  • JVM - Java virtual machine
  • JRE - Java Run time environment
From oracle site we can download JDK which is shipped along with JVM and JRM. Mostly java programmers need the JDK. For end users JRE alone is enough.

Basic OOPS characteristics are

Inheritance

Encapsulation

Abstraction

and Polymorphism

The above concepts collectively makes application software extendable and changeable with less effort.
Also helps in Developing design patterns. Design patterns gives solutions for common challenges usually a programmer faces while development applications.
Factory design pattern, Abstract design pattern, Builder pattern etc. I explain these Design patterns in another article.

Inheritance:
Inheritance is all about reusing one class behavior and members in it's child class. In java terminology these classes are called as super class and sub class. If class B extends class A then, class A becomes super class and class B becomes class A's sub class.

public class A{
  public A(){
 }
   -------
}
public class B extends A{
   public B(){
    } 
    -------
}   
Encapsulation:
Encapsulation is a technique of putting the source code all together within a unit. Java uses class and interface as keywords to represent unit. Data Hiding is one of the buzzwords which is possible only with implementation of Encapsulation in the program.

Abstraction:
Abstraction is the concept of hiding the irrelevant data from client/user visibility. To implement this feature java uses abstract keyword. this can be be prefixed to a class and methods. Once the class is abstract class then it's object can not be created. And id one or all methods of same class are followed by abstract keyword it can not be implemented.
The point here is java programming language gives implementation freedom to the developers where ever expected.
Sometimes java program is written to a another programmer rather a end user. Usually this code is called Application programming interface in short API. When a programmer writes a program with the intention to be used by another programmer, He/She may not need all the details [line by line] instruction's description is not required. Right?
The punch line is abstraction helps in showing only relevant data to the client of our program.
One of the mostly used design pattern is abstract factory design pattern.

Polymorphism:
In generic terminology poly means many morph means forms. It means many forms of a single method names.
Polymorphism can be achieved within the class or using inheritance i.e extends keyword.

Overloading and overriding:

Overloading rules:
within the class a same method can be duplicated but accepts different arguments and returns different result
  1.       Number of arguments must be different
  2. Or  Sequence of arguments must be different
  3. Or  Type of the arguments must be different.
Uses:
Let us take a method .
     int add(int a, int b){
        return a+b;
     }

Now let us overload this method to add decimal values
 float add(float a, float b){
     return a+b;
 }
Now based on the arguments passed to the add() respect add() method will be invoked.
Overriding:
Overriding is possible only by using inheritance.For an instance super class has add(int a, int b). and sub class also needs add operation but computes differently. Then programmer simply overrides the add() of super class overrides the add() with his new implementation. 
public class SuperClass{

  int add(int a, int b){
       ....
   }
}

public Subclass extends SuperClass{
@Overrides
int add(int a, int b){
// computes his own add operation
}
} 
In next article I write about tools of JDK and JRE and internals of JVM Java Virtual Machine.

Please click the link for more good look and feel and elegant font

Happy Coding. ;-)


1 comment:

  1. Interesting high level introduction to Java and object oriented concepts. Thanks!

    itcodehub

    ReplyDelete

Popular posts

Demonstration of Java NullPointerException

NullPointerException causes and reasons Since Java is object oriented programming language, every thing is considered to be an object. and t...