Write a program to display the properties of a restaurant using class and object in java

Share Your Love

Hello guys, welcome to lingarajtechhub.com. Today we are going to discuss the display the properties of a restaurant using class and object in java.

Code:

package com.Restaurant_oops_class_obj.java;

public class Restaurant {
	String name,address;
	
	void res_Input() {
		name="xyz";
		address="mv-24";
	}
	void res_Display() {
		System.out.println("Restaurant name:"+name);
		System.out.println("Restaurant address:"+address);
	}

	public static void main(String[] args) {
		 Restaurant s=new  Restaurant();
		 s.res_Input();
         s.res_Display();
	}

}

Line-1:

  • package com.Restaurant_oops_class_obj.java;

 It is a java package, this package created in a java project and it contains classes inside the package.

Line-3:

  • public class Restaurant

 “Restaurant” is the class name, class is a template used to create objects & class-name first letters must be capitalised. It is public means we can use this outside of the package.

Line-4:

  • String name, address

Here the name, Address  are the variables and the String is a class. It is not a data type.

Line-6:

  • void res_Input() {

                name=”xyz”;

                address=”mv-24″;

}

 In this line, we create a method whose name takes res_Input() where store properties values.

Line-10:

  • void res_Display() {

System.out.println(“Restaurant name:”+name);

System.out.println(“Restaurant address:”+address);

}

 In this line  we create a method whose name takes res_Display().system is a predefined  class .Out is a static variable of type printstream present in system class. Println shows the result.

Line-15:

  • public static void main(String[] args) 

It is a main method in which we can execute a program.

  •  public : It is an access modifier. The main() is public because we can access main() outside of the class , if main() can’t be public then JVM(Java Virtual Machine) can’t be called main() & code can’t be executed. 
  •  Static : static is a keyword in which the main method has to be static in which JVM can load the class into memory and call the main method without creating an instance.
  •  Void : Void is a data type. It means no return type. 
  •  main(): Main is a function. 
  •  String [] args : string means collection of characters ,String[] args represents a collection of Strings that are separated by a space and can be typed into the program on the terminal directly.

Line-16:

  • Restaurant s=new  Restaurant()

 Here “s” is the instance variable.It is created to assign the default values to the instance variables of the class when an object is created.

Line-17:

  • s.res_Input()

“s” is the instance variable which is created. “ .” is the connector .res_Input() is a method where our input values will be stored.

Line-18:

  • s.res_Display()

“s” is the instance variable which is created. “ .” is the connector . res_Display() is a method where our result will be stored.

Share Your Love
Avatar photo
SONALIN SAHOO

Myself Sonalin Shaoo. I am quite happy to contribute here some java programming content and my friends Lipsa Mohanty ,Sushil Puhan, Aswin Kumar Nayak also help me to contribute their Opinion on this .

Articles: 38

Newsletter Updates

Enter your email address below to subscribe to our newsletter