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 \(1/3^{rd}\) 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 \(1/3^{rd}\) 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 6, 2022 My latest PC Build in 2022

I wrote a post in the past describing my crypto mining plus gaming pc and after around 2-3 years of using it, I decided to add more GPUs to it and totally dedicate it to mining crypto. Ergo, I needed a new PC for my gaming and other day-to-day tasks apart from app development. App development I do only on my mac. In this post, I would be describing all the components I used in my PC and why I chose them over others.

May 21, 2019 Declarations in Java

In this post, we would see how to declare a class, interface, variable, constructor, and many more.

gadget unboxing hifi November 5, 2021 Unboxing of KEF Q350 Speakers

I always wanted to have a nice music system in my house since I was a kid. However, I now finally got the chance to build my own HiFi audio system. After some research and listening to different speakers at my local HiFi stores, I shortlisted the following speakers based on my budget: