Header Ads

Class and Object In Java




1.1 Object-Oriented Programming

What is Object-Oriented Programming? It is a programming language model that defines real world entities as objects. The implementation of this programming model is based on the interactions among these objects. For example, a customer (object) purchases textbooks (object) in a bookshop management program (Object-Oriented Program).  

Advantages of Object-Oriented Programming  

  • Reusability: Existing codes can be easily re-used for another objects. This is often called Inheritance in Object-Oriented Programming. To put it simply, we can create new objects with small changes based on existing ones. For example, when you create racing car objects, you can inherit such basic properties from normal car object as wheels, handles, axels, and so on. Technically speaking, inheritance happens at class level. It also reduce redundancy and repetition of codes.                 

                          

  • Maintainability: Object-Oriented Program separates the structure (class) of an object and actual objects. It minimizes the effect of changes in the structure on source programs. Therefore, it is easy to mend and maintain programs.   

  • Modularity: Since objects exist independently, it is easy to identify errors or bugs by looking into each of them not the whole program. For example, when you have some problem with graphic card in your computer, you don’t need to check CPU or LAM, but only the graphic card. To make it easier, you can think each part of the computer as objects.  

2.1 Class and Object in Java

Class and Object are the most basic and important concepts to understand and develop Object-Oriented Programs.

Class in Java

Class is a template that defines the type of an object. It contains common traits and behaviors for multiple objects. Let’s say that you are developing video store management program, then you will need to allow it to create new customer which is an object at programming level. What are the common traits and behaviors that the program should store for each customer in relation to video shop? Traits would be name, phone number rental video and address which are attributes and behaviors would be renting and returning videos which are methods. In other words, those common traits and behaviors are declared in a class through which multiple customers can be created with different values for attributes and methods.

class and object

[ Picture 1 ] Class and Object in Java

Object in Java

As I have mentioned above, objects in Object-Oriented programming refer to real world entity such as a book, person, a car and so on. These objects consist of two factors which are state and operations. The state represents the actual values for attributes.

class and attribute

[ Picture 2 ] Values for Attribute

Then the operations declare the actions that objects can do in a program. For example, customer objects in the video management program can rent videos by executing “Rent” method.

class and method
[ Picture 3 ] Method
These objects are also called “Instance of a class”, and creating objects through a class is often called “instantiate objects from a class”.

No comments

Powered by Blogger.