Skip to main content

OBJECT ORIENTED PROGRAMMING SYSTEMS JAVA | OOPJ | OOP's in java

OBJECT ORIENTED PROGRAMMING SYSTEMS

JAVA


Object-Oriented Programming is a methodology or paradigm to design a program using classes and objects. It simplifies the software development and maintenance by providing some concepts defined below : 

Class is a user-defined data type which defines its properties and its functions. Class is the only logical representation of the data. For example, Human being is a class. The body parts of a human being are its properties, and the actions performed by the body parts are known as functions. The class does not occupy any memory space till the time an object is instantiated. 

Object is a run-time entity. It is an instance of the class. An object can represent a person, place or any other item. An object can operate on both data members and member functions. 

Example 1:

class Student {

   String name;

   int age;

  

   public void getInfo() {

       System.out.println("The name of this Student is " + this.name);

       System.out.println("The age of this Student is " + this.age);

   }

}

 

public class OOPS {

   public static void main(String args[]) {

       Student s1 = new Student();

       s1.name = "Aman";

       s1.age = 24;

       s1.getInfo();

 

       Student s2 = new Student();

       s2.name = "Shradha";

       s2.age = 22;

       s2.getInfo();

   }

}

Example 2:

class Pen {

   String color;

  

   public void printColor() {

       System.out.println("The color of this Pen is " + this.color);

   }

}

 

public class OOPS {

   public static void main(String args[]) {

       Pen p1 = new Pen();

       p1.color = blue;

 

       Pen p2 = new Pen();

       p2.color = black;

 

       Pen p3 = new Pen();

       p3.color = red;

 

       p1.printColor();

       p2.printColor();

       p3.printColor();

   }

}

Note : When an object is created using a new keyword, then space is allocated for the variable in a heap, and the starting address is stored in the stack memory.

‘this’ keyword :  ‘this’ keyword in Java that refers to the current instance of the class. In OOPS it is used to: 

  1. pass the current object as a parameter to another method 

  2. refer to the current class instance variable




Constructor : Constructor is a special method which is invoked automatically at the time of object creation. It is used to initialize the data members of new objects generally. 

  • Constructors have the same name as class or structure. 

  • Constructors don’t have a return type. (Not even void)

  • Constructors are only called once, at object creation.

There can be three types of constructors in Java. 

1. Non-Parameterized constructor : A constructor which has no argument is known as non-parameterized constructor(or no-argument constructor). It is invoked at the time of creating an object. If we don’t create one then it is created by default by Java.

class Student {

   String name;

   int age;

  

   Student() {

       System.out.println("Constructor called");

   }

}


2. Parameterized constructor : Constructor which has parameters is called a parameterized constructor. It is used to provide 

different values to distinct objects. 

class Student {

   String name;

   int age;

  

   Student(String name, int age) {

       this.name = name;

       this.age = age;

   }

}


3. Copy Constructor : A Copy constructor is an overloaded 

constructor used to declare and initialize an object from another object. There is only a user defined copy constructor in Java(C++ has a default one too).

class Student {

   String name;

   int age;

  

   Student(Student s2) {

       this.name = s2.name;

       this.age = s2.age;

   }

}




Note : Unlike languages like C++, Java has no Destructor. Instead, Java has an efficient  garbage collector that deallocates memory automatically.




 

Comments

Popular posts from this blog

Enter 3 numbers from the user & make a function to print their average in java, c++, python, java.

Enter 3 numbers from the user & make a function to print their average. To find the average of three numbers, you need to add the three numbers together and then divide the sum by three. Here's how you can write a function in C++, C, Python, and Java to take three numbers from the user and calculate their average: JAVA import java . util .*; public class Solutions {     public static void main ( String args []) {        Scanner sc = new Scanner ( System . in );        int a = sc . nextInt ();        int b = sc . nextInt ();        int c = sc . nextInt ();          int average = ( a + b + c ) / 3 ;        System . out . println ( average );    }    }   C++: // c++ #include <iostream> using namespace std ;...

2D Arrays | Java Complete Placement Course | Lecture 11

Java - Introduction to Programming Lecture 11 2D Arrays In Java It is similar to 2D matrices that we studied in 11th and 12th class. Creating a 2D Array - with new keyword int [][] marks = new int [ 3 ][ 3 ] ; Taking a matrix as an input and printing its elements. import java . util .*;   public class TwoDArrays {     public static void main ( String args []) {         Scanner sc = new Scanner ( System . in );         int rows = sc . nextInt ();         int cols = sc . nextInt ();           int [][] numbers = new int [ rows ][ cols ];           //input         //rows         for ( int i = 0 ; i < rows ; i ++) {             //columns  ...

Conditional Statements | if, If-else, "if" and "if else", Switch Break | Complete Java Placement Course | Lecture 3

                                    Java - Introduction to Programming Lecture 3 Conditional Statements Conditional statements are a fundamental concept in programming. They are used to make decisions based on certain conditions, allowing the program to perform different actions depending on the input or data it receives. In this article, we'll take a closer look at conditional statements in programming and how they can be used effectively. What are conditional statements? Conditional statements, also known as "if-then" statements, are used in programming to execute certain code if a condition is met. In other words, the program will perform a particular action if a particular condition is true. For example, a program may check if a user has ent...