Categories: Java

OOPs in Java

What is OOPs?

Object-Oriented Programming is a methodology or paradigm for designing a program using classes and objects.

Many concepts, such as inheritance, data binding, polymorphism, and others, are available in Object-Oriented Programming. It takes a practical approach to solve a problem. As a result, object-oriented programming is a better and easier way to write programs than procedural programming languages like C, ALGOL, and PASCAL.

It simplifies software development and maintenance by providing some concepts:

  • Object
  • Class
  • Inheritance
  • Polymorphism
  • Abstraction
  • Encapsulation
Classes in Java:

A class is a collection of objects. It is a logical entity.

A class can also be thought of as a blueprint for creating an individual object. Class does not take up any space.

A class instance is an object. It’s also known as a logical template class’s physical existence.

A class in Java can contain:

  • Fields
  • Methods
  • Constructors
  • Blocks
  • Nested class and interface
Syntax to declare a class:
class <class_name>{  
    field;  
    method;  
}  

Example:

public class Demo{ 
      String name;
      int roll_no;
      float percent;
      char grade;
}  

Instance variables are the fields declared within the class. When an object is created at runtime, it gets memory.

Methods in a class are similar to functions in that they perform operations and represent an object’s behavior.

Object in Java:

A chair, bike, marker, pen, table, car, or other entity with state and behaviour is known as an object. It could be either physical or logical (tangible and intangible). The banking system is an example of an intangible object.

There are three characteristics of an object:

  • The data (value) of an object is represented by its state.

  • The behaviour (functionality) of an object, such as deposit, withdrawal, and so on.
  • A unique ID is typically used to represent an object’s identity. The value of the ID is hidden from the outside user. However, the JVM uses it internally to uniquely identify each object.
Syntax to create an object:
className variable_name = new className();

We had already seen the various methods to create an object(Java object creation methods).

Object and Class Example: main within the class

We’ve created a Student class in this example, which has two data members: id and name. The object of the Student class is created using the new keyword, and the object’s value is printed.

We’re going to add a main() method to the class.

File: Student.java

class Student{  
 
 int id;//field or data member or instance variable  
 String name;  
 //creating main method inside the Student class  
 public static void main(String args[]){  
  //Creating an object or instance  
  Student s1=new Student();//creating an object of Student  
  //Printing values of the object  
  System.out.println(s1.id);//accessing member through reference variable  
  System.out.println(s1.name);  
 }  
}  

Output:

0 
null

Note: also read about the Command Line Argument in Java

Follow Me

If you like my post please follow me to read my latest post on programming and technology.

https://www.instagram.com/coderz.py/

https://www.facebook.com/coderz.py

Share
Published by
Rabecca Fatima

Recent Posts

Generate Parenthesis | Intuition + Code | Recursion Tree | Backtracking | Java

Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…

3 months ago

Square Root of Integer

Given an integer A. Compute and return the square root of A. If A is…

1 year ago

Build Array From Permutation

Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…

1 year ago

DSA: Heap

A heap is a specialized tree-based data structure that satisfies the heap property. It is…

2 years ago

DSA: Trie

What is a Trie in DSA? A trie, often known as a prefix tree, is…

2 years ago

Trees: Lowest Common Ancestor

What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…

2 years ago