OOPS Concepts in Java
Table of Contents
OOPS Concepts in Java
OOPS Concepts
With the aid of algorithms derived from the real world, the OOPS programming approach offers solutions to problems encountered in everyday life. It approaches problem-solving in the real world. Consequently, object-oriented techniques provide a better and simpler way to write programs than procedural programming languages like C, ALGOL, PASCAL, etc.
The concepts of class and object are supported by Java, an object-oriented language. Data is considered important in OOPS and is encapsulated within the class; objects then use that data at runtime.
OOPS Concepts in Java provides advantages over the other programming paradigm and include following features.
Main Features of OOPS Concepts in Java
- 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
Everything in Java is contained within classes. The foundation of Java is the class. It can be described as a template that specifies the actions and conditions of a specific entity. New data types are defined by classes. This new type can be used to create objects of that type once it has been defined. An instance of a class is an object. You could also refer to it as the physical existence of a class of logical template.
In Java, the class keyword is used to declare a class. A class includes both data and methods for working with that data. Instance variables and methods, respectively, are terms used to describe the data or variables defined within a class.
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 must have the class keyword, and class must be followed by a legal identifier.
- It can be either abstract, final or concrete (normal class).
- The variables and methods are declared within a set of curly braces.
- It may optionally extend only one parent class. By default, it extends Object class.
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 the structural layout of a class. It is an object’s blueprint. Using the object, we can call its fields and methods. Instance variables are the fields that have been declared inside the class. When an object is created at runtime, memory is provided. The class’s methods are similar to the functions that are used to carry out tasks and represent an object’s behaviour. Let’s examine what an object is now that we have learned about the class.
Java Object
An object is an instance of a class, whereas a class is an object’s blueprint. A class is represented by an object, which has both properties and behaviour. The terms “properties” and “behaviour” refer to the fields that are declared with the class and the available methods, respectively. We can think of an object in the real world as a cell phone, which has properties such as name, price, and colour as well as behaviours such as calling and chatting. Therefore, we can affirm that the object is a physical thing. Ball, fan, car, etc. are a few examples of real-world objects. In Java, there is a syntax for creating objects.
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 std variable is assigned a brand-new, physical copy of the object when the new keyword is used. It will possess a physical existence and acquire heap area memory. The new operator allocates memory for an object in a dynamic manner.
So 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.
So 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.