2. Basics of Java Programming

2.1. Classes

In object-oriented programming, a class is a programming language construct that is used as a blueprint to create objects. This blueprint includes attributes and methods that the created objects all share.

Usually, a class represents a person, place, or thing - it is an abstraction of a concept within a computer program. Fundamentally, it encapsulates the state and behavior of that which it conceptually represents.

So how is state and behavior of a Class represented?

State: A state is represented by the member variables aka fields in Java. A field in a class is a variable which can store a value that represents a particular property of an object.

Behavior: A behavior is a reusable code known as operations, defined by methods in Java.

It encapsulates state through data placeholders called member variables; it encapsulates behavior through reusable code called methods.

Please pay close attention to Car, Employee and Student classes in the next section as we'll be using those as base to learn various concepts in Java.

			package javatutorial;

			/**
			 * Example of a class
			 * 
			 * @author f5java.com
			 */
			public class Car {
				// Fields : value in fields represent the state of the class
				private boolean ignition;
				private String gear;
				private String fuelTankStatus;
				private boolean musicPlayer;				
				
				// Methods: represent the behavior of the class
				
				/**
				 * Displays car is moving forward message
				 */
				public void moveforward(){
					System.out.println("Moving the car in forward direction");
				}
				
				/**
				 * Displays car is moving backwards message
				 */
				public void moveBackwards(){
					System.out.println("Moving the car in reverse direction");
				}
				
				/**
				 * Displays car is turning right message
				 */
				public void turnRight(){
					System.out.println("Turning car right");
				}
				
				/**
				 * Displays car is turning left message
				 */
				public void turnLeft(){
					System.out.println("Turning car left");
				}

				/**
				 * Displays car status
				 */
				public void displayCarStatus(){
					System.out.println("Ignition: " + ignition);
					System.out.println("Gear: " + gear);
					System.out.println("Fuel Tank: " + fuelTankStatus);
					System.out.println("musicPlayer " + musicPlayer);
				}
			}
		

Another Example

			package javatutorial;

			/**
			 * Example of a class
			 * 
			 * @author f5java.com
			 */
			public class Employee {
				// Fields : value in fields represent the state of the class
			   private String empName;
			   private int age;
			   private String designation;
			   private double salary;
			   private String projectAssigned;	   
			   
			   // Methods: represent the behavior of the class
			   
			   /**
				* Displays employee details 
				*/
			   public void displayEmployeeDetails(){
				   System.out.println("Employee Name: " + empName);
				   System.out.println("Employee Age: " + age);
				   System.out.println("Employee Designation : " + designation);
				   System.out.println("Employee Salary : " + salary);
				   System.out.println("Employee Project : " + projectAssigned);
			   }   

			}