Master Software Engineering with Java Language. Click to Begin

Sunday, December 16, 2018

Object Oriented Programming Tutorial - Learn Java OOP with examples in 30 minutes

  No comments
December 16, 2018


Object oriented programming (OOP) is a programming language model organized around objects and data rather than actions or logic. 



Procedural Programming vs Object Oriented Programming

In procedural programming, a program is considered as a logical procedure that takes input data, processes it, and produces output data. The programming challenge is how to write the logic.

In object oriented programming, we really care about are the objects we want to manipulate rather than the logic required to manipulate them. The programming challenge is how to define the data. 

Example: 

  • The first step in OOP is to identify all the objects the programmer wants to manipulate and how they relate to each other
  • Once an object has been identified,  it is generalized as a class of objects which defines the kind of data it contains and any logic sequences that can manipulate it. 
  • Each distinct logic sequence is known as a method.
  • Data (properties) in objects represents the state while methods represents the behavior.
  • Objects communicate with well defined interfaces called messages.

Object vs Class

Any entity that has state and behavior is known as an object. A class is a blueprint from which we can create individual objects.

Example: 

OOP Concepts

The main idea behind OOP is to represent the data and logic with objects instead of actions or functions. Think of objects as real-life objects like cars, buildings, animals, apples etc. And also abstract objects (things we can not see or eat) like HttpConnection or UserDataParser.  All of those objects have properties and methods to manipulate and access the data stored inside them. Eventually we can convert everything into an object.

There are 4 major concepts in OOP, that will allow us to produce reusable, maintainable, scalable and clean code.

1. Inheritance
Inheritance is the concepts that one class acquires properties and behaviors of another class. That means one class is derived from another class. The latter one is called parent class (or super class) and former one is called child class (or sub class).

Inheritance is the mechanism of code reuse. It is used to achieve Runtime Polymorphism. In Java, an "Is-A" relationship between two classes represents the Inheritance. Java sub class uses "extends" keyword to inherit from the parent class.

Syntax:
SubClass extends ParentClasss

Java Inheritance Example:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
class Car {
  void car_details() {
    System.out.println("Car Details...");
  }
}

class RacingCar extends Car {
  void racing_car_details() {
    System.out.println("Racing Car Detail...");
 }
}

public class InheritanceDemo {
  public static void main(String args[]) {
    RacingCar rc = new RacingCar();
    rc.car_details();         // output: Car Details...
    rc.racing_car_details();  // output: Racing Car Detail...
  }
}

In the above inheritance example, the car_details() method in line 16 is an inherited method from parent class. (because there is no car_details() method in the RacingCar class. but still we can call it, as it is inherited from Car class)


2. Polymorphism
Polymorphism is the concept where a single object can have multiple forms/behaviors. Child objects of a parent object can define their own unique behaviors and yet share some of the same functionality of the parent. 

Java Polymorphism Example:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
class Car {
  void car() {
    System.out.println("Car...");
  }
}

class RacingCar extends Car {
  void car() {
    System.out.println("Racing Car...");
 }
}

public class PolymorphismDemo {
  public static void main(String args[]) {
    Car rc = new RacingCar();
    rc.car();       // output: Racing Car...
  }
}

In this polymorphism example, in line 15, we use parent class's reference variable to store child class object. That means, the parent class Car can behave like a Racing Car as well. When there is a overridden method in child class (i.e. car() method), it will be dispatched run time.


3. Abstraction
Abstraction is a process of hiding the implementation details from the end user while providing only the functionality. Abstraction can be achieved using abstract classes or interfaces.

Java Abstraction Example:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
public interface Car
{
  void car();
}

class MyCar implements Car{
  void car() {
    System.out.println("My Car...");
  }
}

public class AbstractionDemo {
  public static void main(String args[]) {
    MyCar c = new MyCar();
    c.car();       // output: My Car...
  }
}



4. Encapsulation
Encapsulation is a process of hiding data  by wrapping data (variables) and code together as a single unit. Encapsulation can be achieved using access modifiers like 'private' to the property variables and providing 'public' methods (getters and setters) to manipulate those data.

Java Encapsulation Example:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Car{ 
  // private property varible (hidden to outside of this class)
  private int speed;
  
  // public getter method
  public int getSpeed() {
    return this.speed;
  }
  // public setter method
  public void setSpeed(int speed) {
    this.speed = speed;
  }
}

public class EncapsulationDemo {
  public static void main(String args[]) {
    Car c = new Car();
    c.setSpeed(10);
    int s = c.getSpeed();
    System.out.println(s);  // output: 10
  }
}

Read More