OOP Concepts

An object is a software bundle of related state (attributes) and behavior (methods). Software objects are often used to model the real-world objects that you find in everyday life.

Object-Oriented Programming is just a programming paradigm based on this concept of ‘”everything is object”. A class is a blueprint or prototype from which objects are created. A class models the state and behavior of a real-world object.

In definition of a class, we can decide which attributes or methods are only locally used, i.e. private, which can be exposed to others, i.e. public. This is called data encapsulation. It allows data hiding and protection of coding corruption.

For example, a car can be described as a specific implementation of a more general ‘class’ of a thing, called a vehicle. We model this relationship in software with classes by defining a Vehicle class and a Car class. In class Vehicle, we have speed, carry of people, etc.which would be all the behaviors common to most of different types of vehicles, e.g. planes, trains, etc.. It might not make sense in our software to redefine the basic essence of speed over and over again for each different type of vehicle. Instead, we define it once in Vehicle, and then when we define Car (child class), we simply indicates that it inherits (extends) the base definition from Vehicle (parent class). Car is said to specialize the general Vehicle definition. This is how inheritance works in class definition.

Another key concept is polymorphism, which describes the idea that a general behavior from a parent class can be overridden in a child class to give it more specifics. Say we define a method StartEngine() in Vehicle, the same named method should be implemented differently in Car and Plane, since each starts engine differently.


1. http://docs.oracle.com/javase/tutorial/java/concepts/index.html

2. wikipedia: oop




Keywords static and final in Java

static means the variable/method belongs to the class not an instance, so there is only one copy of this data shared between instances.
final is a method to define the variable/method having only one initialization, either in initializing or within a constructor.
A note on final methods and final classes, this is a way of explicitly stating that the method or class can not be overridden / extended respectively.

static variable:
1. A variable which belongs to the class and not to object (instance).
2. It is initialized only once at the start of the execution, before the initialization of any instance variables.
3. Syntax: Class.variable (no need to new an object)

static method:
1. It belongs to the class and not the object (instance)
2. A static method can access only static data unless it uses an instance of some class to indirectly access the instance variable.
3. A static method can call only other static methods  unless it uses an instance of some class to indirectly access the instance method.
4. Syntax: Class.methodName() (no refer to this or super keywords)

static class:
There is a static nested class in java. When you declare a nested class static, it automatically becomes a stand alone class which can be instantiated without having to instantiate the outer class it belongs to.

public class A
 public static class B

Because class B is declared static you can explicitly instantiate as:

B b = new B();

Note if class B wasn’t declared static to make it stand alone, an instance object call would’ve looked like this:

A a= new A();
B b = a.new B();

static block codes:
It’s a static initializer. It’s executed when the class is loaded (or initialized).

static {
        private static Name = "blog";

final keyword
A final class cannot be subclassed.  All methods in a final class are implicitly final. This is done for security and efficiency reasons.

A final method can’t be overridden by subclasses. This is  used to prevent unexpected behavior from a subclass altering a method that may be crucial to the function or consistency of the class.

A final variable can only be initialized once, either via an initializer or an assignment statement. It does not need to be initialized at the point of declaration: this is called a blank final variable. A blank final instance variable of a class must be definitely assigned at the end of every constructor of the class in which it is declared; similarly, a blank final static variable must be definitely assigned in a static initializer of the class in which it is declared; otherwise, a compile-time error will occur.

public class Shirt {
static final String color = "Black";
public class Test {

public static void main(String args[]) {
    System.out.println(Shirt.color); //should provide output as "Black"
            Shirt.color = "white"; //Error because the value of color is fixed.  
    System.out.println(Shirt.color); //Code won't execute.

final static String a = “test” or static final String a = “test” ? 
They are the same as order in keyword doesn’t affect the functionality.