DEV Community

Gowtham
Gowtham

Posted on

Java - OOP, Class, Data Types

1.OOP in Java:

OOP stands for Object-Oriented Programming.

It is a programming approach based on classes and objects. It helps us organize code, reuse functionality, and manage complex programs.

Java supports four main OOP principles:

Encapsulation: Combining data and methods in a class and controlling access to the data.

Inheritance: Creating a new class based on an existing class.

Polymorphism: Allowing the same interface or method name to have different behaviors.

Abstraction: Hiding implementation details and exposing essential functionality.

2.Class in Java:

A class is a blueprint for creating objects.

It defines the data and behaviors that its objects can have.

Example

`class Student {
String name = "Gowtham";
int age = 20;

void display() {
    System.out.println(name);
    System.out.println(age);
}
Enter fullscreen mode Exit fullscreen mode

}

`In this example:

Student is the class name.

name and age are instance variables.

display() is a method.

We can create an object of the class and call its method:

public class Main {
public static void main(String[] args) {
Student s1 = new Student();
s1.display();
}
}

Output:

Gowtham
20

Here, s1 refers to the Student object.

3.Data Types in Java:

Top comments (0)