FahmidasClassroom

Learn by easy steps

Object Oriented Programming Using Java 04 08 2023 1

Java is a very popular object-oriented programming language. It is one of the best language to learn object-oriented programming from the basic. The way of creating class and object in Java by using single and multiple classes has been shown in this tutorial.

Example-1: Create a single class and object inside the class

Create a Java file with the following code that will contain a string variable and two methods to set and get the values of the variable. The main() method has been declared in the same class here.


public class Basic1 {
	//Define a private class variable
	private String Name;
	//Returns the value of the variable
	public String getName( ) {
		return Name;
	}
	//Set the value of the variable
	public void setName(String name) {
		Name = name;
	}
	public static void main(String[] args) {
		//Create object of the class 
		Basic1 obj = new Basic1();
		//Set the value
		obj.setName("OOP Lab class-1");
		//Read the value
		System.out.println("Class: " + obj.getName());
	}
}

Output:

Example-2: Create a single class and object in another class

Create a Java file with the following code that will contain a double variable and two methods to set and get the values of the variable. The object of this class will be created inside the main() method of the another class.

class BasicPart2 {
	//Define a private class variable
	private double Credit_hour;
	//Returns the value of the variable
	public double getCH( ) {
		return Credit_hour;
	}
	//Set the value of the variable
	public void setCH(double CH) {
		Credit_hour = CH;
	}
}
class Basic2
{
	public static void main(String[] args) {
		//Create object of the class 
		BasicPart2 obj = new BasicPart2();
		//Set the value
		obj.setCH(1.0);
		//Read the value
		System.out.println("Credit Hour: " + obj.getCH());
	}
}

Output:

Example-3: Create multiple classes and object in another class

Create a Java file with the following code that will contain two classes and the object of these classes have been created inside the main() method of another class.


class CourseName {
	//Define a private class variable
	private String Name;
	//Returns the value of the variable
	public String getName( ) {
		return Name;
	}
	//Set the value of the variable
	public void setName(String name) {
		Name = name;
	}
}
class CreditHour {
	//Define a the credit hour
	private double Credit_hour;
	//Returns the credit hour
	public double getCH( ) {
		return Credit_hour;
	}
	//Set the value of the variable
	public void setCH(double CH) {
		Credit_hour = CH;
	}
}
class Basic3
{
	public static void main(String[] args) {
		//Create object of the CourseName class 
		CourseName obj1 = new CourseName();
		//Create object of the CourseName class 
		CreditHour obj2 = new CreditHour();
		
		//Set the class name
		obj1.setName("Basic Java Programming");
		//Set the value
		obj2.setCH(1.0);
		//Read the values
		System.out.println("Course Name: " + obj1.getName());
		System.out.println("Credit Hour: " + obj2.getCH());
	}
}

Output:

Exercise:

Create a Java class named library with the following variables and methods and create another class to initialize the variables of the library class and print the values of the variables.

Class variables:

  • bool_id
  • book_name
  • author_name

Methods:

  • add_book()
  • add_member()
  • asssign_book()
  • display_assign_values()