Class - OOPS Concept

Classes are fundamental in the OOPS programming language. It's also known as a blueprint or template in Object Oriented Programming language and without class program doesn't exist.

Syntax:  

class <class-name>{

};


class is the keyword to create a class in Java or other OOPS languages. A class can contains any number of data variables(attributes) and functions(methods). This data variable and methods can be accessed using the instance of the class which is known as an object. Objects are the instances of the class and it's represent the class.

Class basic terms,

Data variables: What the class looks like

Method: What the class can perform

Object: Represent the class

Constructor: Initialize the global vairable

To Understand class concepts in a better way here is an example, let's consider human as a class and you all know class contains variable, methods, and objects (used to access the methods and data variables). In the human class, the data variables are ears, hands, legs, and mouth and the methods are earing(), handshake(), and walk()

Now the object uses the data variable and methods to act like human anywhere just by calling. Simply class is a recipe that gives characteristics to an object. This is how programmers implement real-world entities as programs.

Java Class Example:


class Human{

Ear ears;

Hand hand;

Leg legs;

void earing(){

//method1

};

void handshake(){

//method2

};

void walk(){

//method3

};

public static void main(String args[]){

Human humanObject = new Human(); // object creation

humanObject.ears();

}


Class Data Variables

Java class variable is a class member that can hold different types of values. In Java, data variables are strictly typed and it can hold another variable type. For example, an integer variable can contain only an integer value not another type of value. Below are the common data variable types in Java,
  • Integer
  • String
  • Float
  • Character
  • Boolean
When you create a variable you should specify the type, and assign the value,
int num = 10 //integer variable "num" with value 10

Class Method

Java method is a block of code that executes only when it is called. A method can be called either through object creation or directly by using a class name( the method should be static). The method is a class member and no duplicate methods are allowed in a class.

boolean findEvenOrOdd(int num){
if(num%2 == 0){
System.out.println("The given number is even "+ num);
}
else{
System.out.println("The given number is odd "+ num);

}

}

Class Object

Java class object is an instance of class and a class can have any number of instances. An object represents a class means an object can access its class members from anywhere inside the project. The new keyword is used to create an object,

Human hmObject = new Human(); //hmObject is created

Class Constructor

Java class constructor name is a special member function and it has a name similar to the class name. Constructor is used to initialize the instance variable of the class. A constructor without any parameter is a default constructor and a constructor with parameters is known as a parameterized constructor. A parameterized constructor is used to assign the user-provided value to the instance variable when this class executes.

Post a Comment

Previous Post Next Post
B