You might have already heard many times JAVA is an Object Oriented Programming which simply means coding in JAVA constantly involve classes and objects. In other words coding in JAVA is not possible without object and class. Even the smallest Hello world program requires declaration of class and method work on object. So let’s understand these two concepts which are really very important in JAVA.
Table of Contents
Class: The concept of class comes into role when we see certain type of objects or things around us and the common idea or a blueprint behind this type of objects is called Class. In other words class is a properties behind each of the objects or things possess.
For example: Consider you have iPhone, Samsung and Sony devices and you want to represent them in JAVA. To do that you first need to find out what can be the blueprint behind these devices. And here blueprint can be a Mobile because they all are a type of Mobile. So Mobile is a class which can represent iPhone, Samsung and Sony devices here.
How To Declare Class in JAVA:
class Mobile{ /**ToDo Code Here*/ }
Important Note: Class is a representation of similar types of objects or it is a implementation of encapsulation.
class
when you want to define it.Object: Object is an instance of class. Understanding the concept of object is lot easier when considering real life examples around us because the concept is actually based on real life objects. So just look around yourself and you will find yourself surrounded with lots of objects which has a certain characteristics and behaviors.
For example: Your mobile, it’s an example of object which has a lots of characteristics like color, RAM, camera etc and behaviors like calling, messaging etc.
How To Create Object:
Suppose Mobile is the class for which we want to create object name abhi. Below is the code:
//Object abhi is declared Mobile abhi; //Object is created using new keyword abhi = new Mobile();
Alternatively you can create object in one line:
Mobile abhi = new Mobile();
new
keyword in java. Ex: Mobile abhi = new Mobile();Important Note: Objects are run time entity. They are always getting a memory at run time.
Important Note: Object are created from a class and methods or actions are performed on object.
Here in this example we will display the details of Mobile which three person Abhishek, Rahul and Ravi own. To do that in JAVA first we will create a class Mobile, declare fields for mobile specs (i.e brand, color, camera) and initialized constructor.
Then in main method we will three object for Mobile class. Each object will have the specification details of Mobile which he owns. And finally using System.out.println() method we display the details:
//Class Mobile class Mobile{ String brand, color; int camera; //Constructor initialized public Mobile(String brand, String color, int camera){ this.brand = brand; this.color = color; this.camera = camera; } public static void main(String args[]){ //Object created Mobile abhishek = new Mobile("iPhone","Gold",8); Mobile rahul = new Mobile("Samsung","White",13); Mobile ravi = new Mobile("Nexus","Black",8); //Smartphone details displayed for each user System.out.println("Abhishek own " + abhishek.brand +" "+ abhishek.color + " color smartphone having "+ abhishek.camera+ "MP"+ " camera"); System.out.println("Rahul own " + rahul.brand +" "+ rahul.color + " color smartphone having "+ rahul.camera+ "MP"+ " camera"); System.out.println("Ravi own " + ravi.brand +" "+ ravi.color + " color smartphone having "+ ravi.camera+ "MP"+ " camera"); } }
Output:
Abhishek own iPhone Gold color smartphone having 8MP camera Rahul own Samsung White color smartphone having 13MP camera Ravi own Nexus Black color smartphone having 8MP camera
Here in this program class ClassInJava is taken to create data type of student who is having Name, Roll number, Fathers’ Name, Contact Number, Address as fields. As from the name it is clear that these are of different primitive data type and all these are taken together in a class.
Here declaration of constructor of the class is used which initialize the object with predefined values which are passed at the creation of object.
public class ClassInJava { String name; int rollno; String fathername; String contactno; String address; //Constructor ClassInJava(String name,int rollno,String fathername,String contactno,String address){ this.name=name; this.rollno=rollno; this.fathername=fathername; this.contactno=contactno; this.address=address; } }
And next class is ObjectOfClass which is used to create the object of first class. So the main method with the static written in ObjectOfClass. And this is also good prevention to declare first letter of each word of class name as capital.
public class ObjectOfClass { /**Simple illustration of how to create an object of given class and how it works */ public static void main(String[] args) { //Object of class ClassInJava ClassInJava object=new ClassInJava("Mr. Abhishek",123,"Mr. Sulekh", "+1-8745733445","#321, South Street, No-3, Ontario"); System.out.println("Student Name is: " + object.name); System.out.println("Roll Number is: " + object.rollno); System.out.println("Fathers' Name is: "+ object.fathername); System.out.println("Contact Number is: "+ object.contactno); System.out.print("Student Address is: "+ object.address); } }
Output:
Student Name is: Mr. Abhishek Roll Number is: 123 Fathers' Name is: Mr. Sulekh Contact Number is: +1-8745733445 Student Address is: #321, South Street, No-3, Ontario
History about word “Class”: Aristotle was initially consider deeply the concept of type. Simula is language used for simulation of “bank teller problem”. There were lots of object like customer account, bank detail, employee detail and many more. So all these objects were put together in “classes of objects”. And hence the word class was invented for programming languages. That was main origin of word class.
Premium Project Source Code:
Leave a Reply