Monday, August 15, 2022

Object Oriented Programming Example in Java? Tutorial

I remember when I was doing my engineering, the OOPS concept was in the first semester. Back then it looks like some alien things to me, because I have never done programming and even the smallest program we get to write like checking whether the number is palindrome or not, or printing the Fibonacci series, we never really needed to use OOPS. When needed, I just memorize things and write them down in exams :). That worked, I passed the exam but effectively I didn't learn OOP or Object Oriented Programming on those days.

It's when I started learning java (an almost truly object-oriented programming language) and was introduced to concepts like abstract classes, interface, and method overriding; something started to go into my mind.

This blog post is very basic in nature, but I really hope it could help beginners in understanding things like class, object, abstraction, polymorphism, inheritanceencapsulation, composition, cohesion, coupling, interface programming, etc.

Btw, if you are new to the world of object-oriented programming and design then I also suggest you go through these free Object-Oriented Programming courses for Beginners.  It's a great course to understand the complete process of object-oriented analysis and design for creating quality software.



What Is Object-Oriented Programming? Example

I am planning to write a clear and concise post on each of these because I really don't like too much information in one shot. It never worked with me, I believe learning is a gradual process and always starts with the most simple things, which eventually turned out to be the most important ones.

So What is Object Oriented programming?
Simple programming which is oriented on objects :) means treat everything surrounding you as an
object.

Now the question comes, what is an Object?
An Object is a particular concrete instance of Class, and what is the class? A Class is a blueprint from
which object gets created which has attributes and behavior, we call attributes as members and behavior as methods.



let's see one example: how about representing a classroom using OOPS?
A classroom is a room, which has certainly no of chairs, desks, blackboard, teacher, students, etc.

Now in order to represent that in code, we need to write a program. In Java, we will write like this.

public class ClassRoom {

//members
int noOfChairs;
int noOfDesks;
Teacher currentTeacher;
Students [] students;
Blackboard blackboard;


//methods
public int getNoOfChairs(){

}

public Teacher getNoOfTeacher(){

}

.....

}

I have omitted most of the methods, just to focus on concept rather than implementation. So now we know that class is the blueprint, which has member variables and methods and is used to create objects. Now what is the object here, any classroom in which any lecture is going on, your first lecture would be one instance, the second lecture would be another instance.

See the point, every object of the same class has the same no of variables but their value gets changed like lecture 1 was attended by 15 students by a teacher called Mr. Gupta, lecture 2 was attended by 18 students which were by Miss Amrita.

Anything which you see around can be represented in class and object e.g. this blog is an instance of Blog class, a particular movie can be an instance of Movie class like Titanic is an instance of Movie where actor/director are different than another movie.

Here is a nice diagram which shows how object oriented programmer think and use object oriented programming technique to design their services and applications:

Object Oriented Programming Example in Java? Tutorial
image_credit- Manu


That's all on the fundamentals of object-oriented programming in Java. I hope you got the idea of representing real-world things in class and objects, by doing this we try to create applications that solve real-world problems. Visualizing and understanding in terms of objects also makes programming easy to write, read and maintain.

4 comments :

Anonymous said...

Explaining in easy and good way OOPS. Great Sir....

Unknown said...

The most simpler way of explaining OOP's concept I have ever seen

Unknown said...

where is the continuation of this OOPS?

Anonymous said...

Good explanation

Post a Comment