Loading...

Object oriented design in Java

java tech
Ram Patra Published on May 25, 2019
Image placeholder

Java would have been fully Object Oriented if it didn’t have the primitive types like int, float, boolean, etc. However, it is mostly Object Oriented so you may go through this post if you’re new to Object-Oriented Programming.

Encapsulation

Suppose you want the variable small to be always of big.

But in the below code as the variable small is public, any code can access it and assign any arbitrary value thus violating the requirement of small being of big. This happens due to the lack of encapsulation.

class Foo {
	public int big = 9;
	public int small = 3;
	public void setBig(int num) {
		big = num;
		small = num/3;
	}
	// other code here
}

The problem can be solved by changing the access modifier of small to private so that each and every code will go through setBig() method to set the value of big and small. This is nothing but a small practical example of encapsulation. Here we encapsulated the variable small by making it private and providing getters and setters for code to access it. (We should follow this practice always for all variables)

Inheritance

A class inheriting public/protected properties of another class by using the keyword extends is called inheritance in java.

The two most common reasons to use inheritance:

  • To promote code reuse.
  • To use polymorphism.

IS-A relationship

In OO, the concept of IS-A is based on class inheritance or interface implementation. IS-A is a way of saying, “this thing is a type of that thing.”

For example in the below figure:

“Car extends Vehicle” means “Car IS-A Vehicle.”
“Subaru extends Car” means “Subaru IS-A Car.”

HAS-A relationship

HAS-A relationships are based on usage, rather than inheritance. In other words, class A HAS-A B if code in class A has a reference to an instance of class B. For example, you can say the following, A Horse IS-A Animal. A Horse HAS-A Halter. The code might look like this:

public class Animal { }

public class Horse extends Animal {
	private Halter myHalter;
}

Polymorphism

Any Java object that can pass more than one IS-A test can be considered polymorphic. Other than objects of type Object, all Java objects are polymorphic in that they pass the IS-A test for their own type and for class Object.

The only way to access an object is through a reference variable:

  • A reference variable can be of only one type, and once declared, that type can never be changed (although the object it references can change provided it’s not declared final).
  • A reference variable’s type determines the methods that can be invoked on the object the variable is referencing.
  • A reference variable can refer to any object of the same type as the declared reference, or to any subtype of the declared type.
  • A reference variable can be declared as a class type or an interface type. If the variable is declared as an interface type, it can reference any object of any class that implements the interface.

More on polymorphism in Overriding & Overloading in Java.

Ram Patra Published on May 25, 2019
Image placeholder

Keep reading

If you liked this article, you may like these as well

February 16, 2020 Hubspot Interview for Senior Software Engineer

I applied to Hubspot directly on their website after seeing an open position in their Products team on LinkedIn or Stackoverflow Jobs, I do not exactly remember which one, in Dublin.

May 28, 2020 Best Mac Apps for Working From Home

For me personally, it is a mixed feeling working from home. On one hand, I have the flexibility with when to work and when not to but on the other hand, it is sometimes hard for me to find that fine line between work and life. There are days when I keep working even after office hours without realizing that I have got my life wife too :)

November 28, 2019 How I made my blog

I made this blog using JAMStack. J stands for Javascript, A for APIs, and M for Markups. So, in short, a website built using HTML, CSS, and Javascript only and maybe accessing some APIs over HTTP can be said to be built with JAMStack and are generally called Static Websites.