Object-Oriented Programming using Java

Basics of Classes and Objects

1. Introduction

Object-Oriented Programming (OOP) is a programming paradigm that models real-world entities as objects. Java is one of the most popular Object-Oriented Programming languages.

Everything in Java revolves around the concepts of Classes and Objects.

Think of a class as a blueprint and an object as the actual product built using that blueprint.

2. What is a Class?

A Class is a user-defined data type that acts as a blueprint for creating objects. It is defined as group of object with similar properties. Class is a generic concept and does not occupy memory space.

A Class contains

Example Class

Student
Attributes
name
rollNo
mark
Methods
display()
calculateGrade()

3. What is an Object?

An object is an instance of a class.

Objects occupy memory and contain actual values.

Example Objects

Object Name Roll No Mark
s1 Anu 1 95
s2 Rahul 2 88
s3 Arun 3 76

4. Java Program

class Student {

    String name;
    int rollNo;

    void display(){
        System.out.println(name);
        System.out.println(rollNo);
    }
}

public class Main{
    public static void main(String args[]){

        Student s1=new Student();

        s1.name="Anu";
        s1.rollNo=101;

        s1.display();
    }
}