New Tutorials:   JAVASCRIPT    SASS/SCSS    PL/SQL  
See the Tutorial List

What is OOPS

OOPS is a programming approach which provides solution to real life problems with the help of algorithms based on real world. It uses real world approach to solve a problem. So object oriented technique offers better and easy way to write program then procedural programming languages such as C, ALGOL, PASCAL etc. Click here to watch video on OOPS concept in Java

Java is an object oriented language which supports object oriented concepts like: class and object. In OOPS data is treated important and encapsulated within the class, object then use to access that data during runtime.

OOPS provides advantages over the other programming paradigm and include following features.


Main Features of OOPS

  • Inheritence
  • Polymorphism
  • Encapsulation
  • Abstraction

As an object oriented language Java supports all the features given above. We will discuss all these features in detail later.


Java Class

In Java everything is encapsulated under classes. Class is the core of Java language. It can be defined as a template that describe the behaviors and states of a particular entity.

A class defines new data type. Once defined this new type can be used to create object of that type.

Object is an instance of class. You may also call it as physical existence of a logical template class.

In Java, to declare a class class keyword is used. A class contain both data and methods that operate on that data. The data or variables defined within a class are called instance variables and the code that operates on this data is known as methods.

Thus, the instance variables and methods are known as class members.


Rules for Java Class

  • A class can have only public or default(no modifier) access specifier.
  • It can be either abstract, final or concrete (normal class).
  • It must have the class keyword, and class must be followed by a legal identifier.
  • It may optionally extend only one parent class. By default, it extends Object class.
  • The variables and methods are declared within a set of curly braces.

A Java class can contains fields, methods, constructors, and blocks. Lets see a general structure of a class.

Java class Syntax

	
class  class_name {
     field;
     method;
}
	

A simple class example

Suppose, Student is a class and student's name, roll number, age are its fields and info() is a method. Then class will look like below.

class Student.
{
 String name;
 int rollno;
 int age;
void info(){
 // some code
}
}

This is how a class look structurally. It is a blueprint for an object. We can call its fields and methods by using the object.

The fields declared inside the class are known as instance variables. It gets memory when an object is created at runtime.

Methods in the class are similar to the functions that are used to perform operations and represent behavior of an object.

After learning about the class, now lets understand what is an object.


Java Object

Object is an instance of a class while class is a blueprint of an object. An object represents the class and consists of properties and behavior.

Properties refer to the fields declared with in class and behavior represents to the methods available in the class.

In real world, we can understand object as a cell phone that has its properties like: name, cost, color etc and behavior like calling, chatting etc.

So we can say that object is a real world entity. Some real world objects are: ball, fan, car etc.

There is a syntax to create an object in the Java.


Java Object Syntax


className variable_name = new className();

Here, className is the name of class that can be anything like: Student that we declared in the above example.

variable_name is name of reference variable that is used to hold the reference of created object.

The new is a keyword which is used to allocate memory for the object.

Lets see an example to create an object of class Student that we created in the above class section.

Although there are many other ways by which we can create object of the class. we have covered this section in details in a separate topics.


Example: Object creation


Student std = new Student();

Here, std is an object that represents the class Student during runtime.

The new keyword creates an actual physical copy of the object and assign it to the std variable. It will have physical existence and get memory in heap area. The new operator dynamically allocates memory for an object.

creation of object in java

In this image, we can get idea how the an object refer to the memory area.

Now lets understand object and class combinally by using a real example.


Example: Creating a Class and its object


	
public class Student{      
 
	 String name;
	 int rollno;
	 int age;
	 
	void info(){
	  System.out.println("Name: "+name);
	  System.out.println("Roll Number: "+rollno);
	  System.out.println("Age: "+age);
	}  
	
	
	public static void main(String[] args) {
		Student student = new Student();
		
		// Accessing and property value
		student.name = "Ramesh";
		student.rollno = 253;
		student.age = 25;
		
		// Calling method
		student.info();
		
	}
}  
	

Name: Ramesh Roll Number: 253 Age: 25

In this example, we created a class Student and an object. Here you may be surprised of seeing main() method but don’t worry it is just an entry point of the program by which JVM starts execution.

Here, we used main method to create object of Student class and access its fields and methods.


Example: Class fields


	
public class Student{      
 
	 String name;
	 int rollno;
	 int age;
	 
	void info(){
	  System.out.println("Name: "+name);
	  System.out.println("Roll Number: "+rollno);
	  System.out.println("Age: "+age);
	}  
	
	
	public static void main(String[] args) {
		Student student = new Student();
		
		// Calling method
		student.info();
		
	}
}
	

Name: null Roll Number: 0 Age: 0

In case, if we don’t initialized values of class fields then they are initialized with their default values.


Default values of instance variables

int, byte, short, long -> 0

float, double → 0.0

string or any reference = null

boolean → false

These values are initialized by the default constructor of JVM during object creation at runtime.