Object and Class In Java

What is an Object?

Object are key to understanding Object Oriented Technology. Object is an real world entity when you look around you at anything. It can be laptop you are reading through, books, your desk, chair etc.


Real world object has two characteristics : state and behavior.  Ask following question to any of your object in real world.
Suppose, you are using laptop to read this blog, then laptop is an object.


You can outline or create an class just by asking following simple questions.


1. What does it have?
2. what doest it do?

This two simple question will give you characteristics of your object.


Suppose, this is your laptop..and if you ask above questions





This would be the answers

What does it have? (Properties/state)

What does it do? (Actions/behavior)

width ; width=24.4 runGames()
color , color = black browseInternet()
price ; price = $1000 storeFiles()
brand; brand = Sony; watchVideos()


From above answers, you can say that, your laptop object comprised of these many properties and actions it can perform.
So if you have understood, you can create an class from this states and behaviors of any object.
  
class Laptop {

//Properties
double widthOfLaptop=24.4;
String brandName ="Sony";
String colorOfLaptop="black";
double priceOfLaptop="$1000";


//Actions

public void runGames(){
...
}

public void browseInternet(){
...
}

public void storeFiles(){
...
}

public void watchVideos(){
...
}

}




So now you guys can understand how easy it is to make a class from properties and actions.
You can pick any real world object and create a class from that.





Class : can be only considered as drawing or outline of your object on a piece of paper. It specifies what your car has and what your car will do!!


Object : is the real world existing, running and working entity according to your class drawing.


For example : in workshop, you give manufacturer a design telling that car would look like this and this this it would be able to perform.
now class is one and workshop will produce many version of that class. suppose you gave a contract of 100 cars, so workshop will give you 100 cars of same properties of class drawing you specified. So, class is drawing and objects are working, real time entity. Hope you guys have understood with this example.


Following is the most easiest example that can clear your understanding..




package examples;

public class CarManufacturer {

 public static void main(String[] args){
  
  
  //Create an Object
  // Workshop created first car from outline of class 
  Car firstCar = new Car(); 
  
  //firstCar can do this
  firstCar.carSpeed();
  firstCar.carFeatures();
  firstCar.carfacility();
  firstCar.carMileage();
  
  //accessing datavalue (properties) of outline of car
  System.out.println(firstCar.speed);
  
  //--------------------------------------------------------
  System.out.println("------------------------------------");
  System.out.println("Creating Second car in Workshop");
  //----------------------------------------------------
  //Workshop created second car from outline of class (drawing=class)
  
  Car secondCar = new Car();
  //firstCar can do this
  secondCar.carSpeed();
  secondCar.carFeatures();
  secondCar.carfacility();
  secondCar.carMileage();
    
  //accessing datavalue (properties) of outline of car
  System.out.println(secondCar.speed);
  
  //So on.....
  //you can create as many object as you want from given class car.
  //Thus you can say, class is a drawing/outline of object.
  //Object is a real time, existing, working, running entity.
 }
 
 
 
}

//Drawing or design of a car
//What does it do?  = Actions
//What does it has? = Properties
class Car {
 
 //Car's properties - What does it has? 
 //known as data 
 String carName;
 String carCompany;
 double carPrice;
 String colorName;
 double speed = 20;
 
 //What does it do? - Actions
 //known as methods
 public void carSpeed(){
  
  System.out.println("Cars speed can be changed");
  
 }
 
public void carFeatures(){
  
  System.out.println("Cars have stylish look");
  
 }
 
public void carfacility(){
 
 System.out.println("Car has wide space for use");
 
}

public void carMileage(){
 
 System.out.println("Car can go upto 20km/liter");
 
}
 
}



Above example shows how you can create an Object.

Simply if you have car class, then

Construction is :
ClassName yourDesiredObjName = New ClassName();

Example : 

 Car bmwCar = new Car();
 Car audiCar = new Car();


if you want to call method of Car class you can access it from an Object.
so in other words you can say.. tell me what is the speed of firstCar i manufactured in workshop?


firstCar.carSpeed();
secondCar.carSpeed();


Suppose it is a laptop class, then
Example : Laptop sonyLaptop = new Laptop();






Here is another example which would help you to understand more




public class ExampleOfObject(){

 public static void main(String[] args){

 //Creating an object of circle
 
 Circle c = new Circle();
 
 // c is object reference to access the circle class.
 
 System.out.println(c.radius); 
 //0.0 because it is a float datatype. 
 System.out.println(c.myVar);  
 // it will print 50 
 
 c.radius = 3.5; 
        //c is the reference/object of Circle class , so by doing this, it will assign the 3.5 in radius of Circle class's c object.
 c.area = 3.14 * c.radius * c.radius;
 System.out.println(" The area of c object of circle is " + c.area);
 
 
 //remember its just c reference, you can have as many object as you want and calculate area with different values.
  //Creating second object
 
 
 Circle c2 = new Circle();
 c2.radius = 5.3;
 c2.area = 3.14 * c2.radius * c2.radius;
 System.out.println("The area of c2 object is" + c2.area);

 //now if you want to use methods(actions of class) then you can do as follows
 
 Circle c3 = new Circle();
 c3.findArea(3.4);  //this is much more faster, we don't need to write more lines each time to calculate area
 //because method is defined in class.
 
 Circle c4 = new Circle();
 c4.findArea(5.4);

 }

}

class Circle(){

 //what does it has? - properties - datatypes
 double radius;
 double area;
 double myVar = 50;
 //In java when you don't define anything, it actually gives default to 0 to integer and 0.0 to double.
 
 //Actions 
 
 public void findArea(double r){
  
 radius  = r;
 area = 3.14 * radius * radius;
 System.out.println("The area of circle is: " + area); 

 }
}





i hope you guys have understood clearly the difference between class and objects and how you can create them very easily.
If you guys have any questions, please leave your comment below, will reach to you as soon as possible.
Thank you guys! Happy Coding!!





Post a Comment

0 Comments