- by Rutu Shah
Java is a purely Object Oriented Programming language and Object-Oriented Programming Language means everything is based on a real-world entity.
In Object-Oriented Programming, everything is based on class and objects and the core concept of object-oriented programming is to break larger/complex problems into small chunks of objects.
Java Class
- A Class is defined as a blueprint from which you can create the object.
- It represents the set of properties or methods that are common to all objects of one type.
- Class is the logical entity. A java class is created first before we create an object.
As java class is the template or blueprint from which the objects can be created and it is logical entity not physical entity.
A java class contains follow
Java Object
In Java, a class is a blueprint of the object and therefore object is defined as an instance of the class.
An Object is an entity that has a state and behavior. For example, a dog is an object. It has
Behavior eat, sleep, bark
States name, color, breed
Some formal definitions of the object are as follows
Object is considered to be real-world entity, run-time entity, instance of class, also object has state and behavior.
Let us understand class and objects more with an example.
package com.learnings.objectsClasses;
/*
* Prepared By : Rutu Shah
* Description : This is triangle.java class, where I have defined area, height and base methods.
* */
public class Triangle {
public double base, height;
public double getArea(){
double area = (base*height)/2;
return area;
}
/**
* This method is used to set height.
* @param height
*/
public void setHeight(double height){
this.height = height;
}
/**
* This method is used to set base.
* @param base
*/
public void setBase(double base){
this.base = base;
}
}
package com.learnings.objectsClasses.methodOverloading;
import com.learnings.objectsClasses.Triangle;
import java.util.Scanner;
/*
* Prepared By : Rutu Shah
* Description : This is triangleMain.java class where I have created instance of triangle
* and called getArea() method of the Triangle class.
* */
public class TriangleMain {
public static void main(String[] args) {
Triangle triangle = new Triangle();
Scanner scanner = new Scanner(System.in);
System.out.println("Enter base of the triangle : ");
double base = scanner.nextDouble();
System.out.println("Enter base of the triangle : ");
double height = scanner.nextDouble();
triangle.setBase(base);
triangle.setHeight(height);
System.out.println("Area of triangle : " + triangle.getArea());
}
}