Monday, April 28, 2025

Java OO Tutorial - Classes and Objects

Object-oriented programming (OOP) is a programming paradigm that is used in Java and other programming languages. It is based on the concept of objects, which can contain data and code to manipulate that data.

In Java, OOP is implemented using classes and objects.

A class is a blueprint or template that defines the characteristics and behavior of a particular type of object.

An object is an instance of a class, created using the "new" keyword, which has its own state and behavior.

In OOP, objects interact with each other by sending messages or calling methods. A method is a block of code that performs a specific task and can be called by an object. Methods can have parameters and return values, which allow them to take input and produce output. 

public class Main {	
	int x = 10;
	String someString = "Some Value";	
	
	public static void main(String[] args) {
		Main firstObj = new Main();
		Main secondObj = new Main();
		Main thirdObj = new Main();
		
		System.out.println(firstObj.x);
		
		System.out.println(secondObj.x);
		
		firstObj.x = 555;
		
		System.out.println(firstObj.x);
		
		System.out.println(thirdObj.someString);					
	}	
}

This is a small Java program that defines a class named "Main" and creates three objects of that class. Let's break it down block by block: 

public class Main {
    int x = 10;
    String someString = "Some Value";

In the first block, we define the Main class and declare two instance variables: "x" and "someString". "x" is an integer with a default value of 10, and "someString" is a string with a default value of "Some Value". 

public static void main(String[] args) {
    Main firstObj = new Main();
    Main secondObj = new Main();
    Main thirdObj = new Main();

In the second block, we define the main method which is the entry point for the program. Inside this method, we create three objects of the Main class: firstObj, secondObj, and thirdObj using the "new" keyword. 

System.out.println(firstObj.x);

System.out.println(secondObj.x);

In the third block, we print out the value of "x" for the firstObj and secondObj. Since "x" is an instance variable, each object has its own value of "x". The output of this block would be: 

10
10
firstObj.x = 555;

System.out.println(firstObj.x);

In the fourth block, we set the value of "x" for firstObj to 555 and print out its new value. The output of this block would be: 

555
System.out.println(thirdObj.someString);

In the fifth block, we print out the value of "someString" for thirdObj. Since "someString" is an instance variable, each object has its own value of "someString". The output of this block would be: 

Some Value

Overall, this program demonstrates the concept of object-oriented programming by creating multiple objects of a class and manipulating their instance variables.

No comments:

Post a Comment

Tkinter Introduction - Top Widget, Method, Button

First, let's make shure that our tkinter module is working ok with simple  for loop that will spawn 5 instances of blank Tk window .  ...