Java is an object oriented programming language, a language is considered as a object oriented programming language if that language has following properties.

1) Abstraction

2) Inheritance

3) Polymorphism

4) Encapsulation


1) Abstraction

Abstraction means hiding something, we can understand abstraction with an example of car. As a driver you have controls like handle, gear and wheels but the driver never knows how the car is running and handling internal functionality. This is what we call abstraction, likewise java too have abstraction. Abstraction can be achieved using Abstract Classes and Interfaces.

Abstract Class

Abstract classes are declared using an abstract keyword, abstract classes can not be instantiated so it has to be extended by some other class to make an abstract class meaningful. Abstract classes can have both abstract and non-abstract methods in them. We will see more about abstract classes in upcoming articles.

public abstract class Human {
	
	public String getGender() {
		return null;
	}

	public abstract String getName();

	public abstract String getColor();
}


Interfaces

In Java interfaces can be created using interface keyword, interfaces can have abstract methods only. In interfaces all the functions are public abstract by default. Interfaces can extend other interfaces and can be implemented by any classes. Any class that implements an interface has to implement all its methods as well. We will see more about interfaces in upcoming articles.

public interface Human {

	public String getGender();

	public String getName();

	public String getColor();
}


2) Inheritance

When an object acquires the properties of another object this is called inheritance. Classes can inherit properties of other classes by extending them. The class that is being inherited is called super class and the class that extends other class is called sub class. Subclasses can override a function from superclasses.

public class Fruit {
		int name;

		public String getName(String name) {
			return name;
		}
}

class Apple extends Fruit {
		public String getName(String name) {
			return "Fruit " + name;
		}
}


3) Polymorphism

Polymorphism means having many forms, in java its possible to have many forms of an method. This can be achieved by method overloading and method overriding. Method overloading means having more than one method with same name and difference in either type of parameters or number of parameters or both.

public class Fruit {
	// overloaded methods
		public String getName(String name) {
			return name;
		}

		public String getName(String name, int id) {
			return name;
		}

		public String getName(int id) {
			return "Name";
		}
}


When a subclass has a method with the same name as the superclass, this is called method overriding. Here is an simple example of method overriding:

public class Fruit {
		public String getName(String name) {
			return name;
		}
}

class Apple extends Fruit {
		@Override
		public String getName(String name) {
			return "Fruit " + name;
		}
}


4) Encapsulation

Encapsulation is a practice of protecting data using private, protected like access modifiers from unauthorised access outside the class. We can make data read only by making the variable private and providing public getter only with no setter, likewise we can make data write only by providing public setter only and not a getter at all.

public class Fruit {

	private String name;
	private String color;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getColor() {
		return color;
	}

	public void setColor(String color) {
		this.color = color;
	}
}


In this article we have seen object oriented nature of Java and a brief about Abstraction, Inheritance, Encapsulation , Polymorphism and a bit about method overriding and method overloading. We will learn more about them in detail in upcoming articles.
  • By Techburps.com
  • Feb 21, 2015
  • Core Java