PDF Google Drive Downloader v1.1


Report a problem

Content text OOPS 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.

Related document

x
Report download errors
Report content



Download file quality is faulty:
Full name:
Email:
Comment
If you encounter an error, problem, .. or have any questions during the download process, please leave a comment below. Thank you.