SlideShare a Scribd company logo
object oriented programming using java, second sem BCA,UoM
Constructors
 A special type of method that enables an object to initialize itself when it is
created
 Constructors have the same name as the class itself
 They do not specify a return type, not even void because they return the
instance of the class itself.
 It is called constructor because it constructs the values at the time of object
creation
Example:
class Rectangle
{
int length;
int width;
Rectangle (int x, int y) // Defining constructor
{
length=x;
width=y;
}
int rectArea()
{
return (length * width);
}
}
Class RectangleArea
{
public static void main(string a[ ])
{
Rectangle rect1=new Rectangle(15,10); //calling constructors
int area1=rect1.rectArea( );
System.out.println(“Area1= ” +area1);
}
}
Types of Constructor
 In Java, constructors can be divided into three types:
1. No-Arg Constructor
 If a constructor does not accept any parameters, it is known as
a no-argument constructor.
 Ex: class Data
{
Data()
{
System.out.println("No-Args Constructor");
}
public static void main(String[] args)
{
Data d = new Data();
}
}
2. Parameterized Constructor
 A Java constructor can also accept one or more parameters. Such
constructors are known as parameterized constructors (constructors
with parameters).
class Data
{
int num1, num2;
Data(int m,int n)
{
num1=m;
num2=n;
}
public static void main(String[] args)
{
Data d = new Data(10,20)
}
}
3. Default Constructor
 If we do not create any constructor, the Java compiler automatically creates a no-arg
constructor during the execution of the program.
 This constructor is called the default constructor.
 Ex: class Data
{
public static void main(String[] args)
{
Data d = new Data();
}
}
 Default constructor only role is to initialize the object and return it to the calling code.
 Default constructor is always without argument and provided by java compiler only
when there is no existing constructor defined.
Overloaded constructor
 Two or more constructors having different parameters is called overloaded
constructor.
 Ex:
class Demo
{
int a,b,c;
Demo()
{
a=1; b=2; c=3;
}
Demo(int x, int y)
{
a=x; b=y; c=20;
}
Demo(int x, int y, int z)
{
a=x; b=y; c=z;
}
}
class Main
{
public static void main(String args[])
{
Demo obj1=new Demo();
Demo obj2=new Demo(10,20);
Demo obj3=new Demo(10,20,30);
}
}
object oriented programming using java, second sem BCA,UoM
Java Garbage Collection
 Java garbage collection is the process by which Java programs perform
automatic memory management.
 Java programs compile to bytecode that can be run on a Java Virtual
Machine, or JVM for short.
 When Java programs run on the JVM, objects are created on the heap,
which is a portion of memory dedicated to the program.
 Eventually, some objects will no longer be needed. The garbage collector
finds these unused objects and deletes them to free up memory.
Finalizer
• Finalizer methods are almost the opposite of constructor methods.
• A constructor method is used to initialize an object, while finalizer methods
are called just before the object is garbage-collected and its memory
reclaimed.
• The syntax of the finalizer method is simply finalize().
• The Object class defines a default finalizer method.
• To create a finalizer method, override the finalize() method using the
following signature:
 The body of finalize() method can include several objects including
super.finalize(). This object allows the parent class to finalize an object, if
necessary.
 The finalize() method can be called at any time; however, calling finalize()
does not trigger an object to be garbage-collected. Only removing all
references to an object will cause it to be marked for deleting.
 Finalizer methods are best used for optimizing the removal of an object
(for example, by removing references to other objects) by releasing
external resources that have been acquired (for example, external files), or
for other behaviors that may make it easier for that object to be removed .
Visibility modifiers
 Java provides entities called “Access Modifiers or access specifiers” that help
us to restrict the scope or visibility of a package, class, constructor, methods,
variables, or other data members. These access modifiers are also
called “Visibility Specifiers”.
 The access specifiers also determine which data members (methods or fields)
of a class can be accessed by other data members of classes or packages etc.
 To ensure encapsulation and reusability, these access specifiers/modifiers are
an integral part of object-oriented programming.
Visibility modifiers in Java
1. Default: Whenever a specific access level is not specified, then it is assumed
to be ‘default’. The scope of the default level is within the package.
2. Public: This is the most common access level and whenever the public
access specifier is used with an entity, that particular entity is accessible
throughout from within or outside the class, within or outside the package,
etc.
3. Protected: The protected access level has a scope that is within the package.
A protected entity is also accessible outside the package through inherited
class or child class.
4. Private: When an entity is private, then this entity cannot be accessed
outside the class. A private entity can only be accessible from within the class.
object oriented programming using java, second sem BCA,UoM
Inbuilt classes - String
 The String is a built-in class in Java to store a sequence of characters between
double-quotes.
 It’s under java.lang package so we don’t have to import, it will be imported
automatically for every class.
 Example: class Main
{
public static void main(String[] args)
{
String str = "apple";
System.out.println(str); // apple
String name = new String("John");
System.out.println(name); // John
}
}
 The strings are objects in java of the class ‘String’.
System.out.println(“Welcome to java”);
The string “welcome to java” is automatically converted into a string
object by java.
 A string in java is not a character array and it is not terminated with
“NULL”.

More Related Content

PPTX
Express js
Manav Prasad
 
PPTX
Enterprise java unit-1_chapter-2
sandeep54552
 
PDF
[213]monitoringwithscouter 이건희
NAVER D2
 
PPTX
Servlet Filters
Wings Interactive
 
PDF
Formation Gratuite Total Tests par les experts Java Ippon
Ippon
 
PDF
Introduction to React JS
Bethmi Gunasekara
 
PPT
Applet skelton58
myrajendra
 
PPT
jQuery
Mohammed Arif
 
Express js
Manav Prasad
 
Enterprise java unit-1_chapter-2
sandeep54552
 
[213]monitoringwithscouter 이건희
NAVER D2
 
Servlet Filters
Wings Interactive
 
Formation Gratuite Total Tests par les experts Java Ippon
Ippon
 
Introduction to React JS
Bethmi Gunasekara
 
Applet skelton58
myrajendra
 

What's hot (20)

PPTX
Spring boot
Pradeep Shanmugam
 
PPTX
ASP.NET Core MVC + Web API with Overview
Shahed Chowdhuri
 
PDF
Angular & RXJS: examples and use cases
Fabio Biondi
 
PPTX
Introduction to Node.js
Vikash Singh
 
PPTX
Design Pattern - Factory Method Pattern
Mudasir Qazi
 
PDF
Rediscovering Spring with Spring Boot(1)
Gunith Devasurendra
 
PDF
Introduction to Swift programming language.
Icalia Labs
 
PDF
Exception handling
Anna Pietras
 
PDF
Angular Pipes Workshop
Nir Kaufman
 
PPTX
Spring Boot
Jiayun Zhou
 
PDF
Nest.js Introduction
Takuya Tejima
 
PDF
Operator overloading
Pranali Chaudhari
 
PPTX
Constructors in java
chauhankapil
 
PPTX
Express JS Rest API Tutorial
Simplilearn
 
ODP
Dependency Injection in Spring in 10min
Corneil du Plessis
 
PPTX
Spring 3.x - Spring MVC - Advanced topics
Guy Nir
 
PPTX
Introduction to Spring Boot
Purbarun Chakrabarti
 
PPTX
Constructors in java
sunilchute1
 
PPTX
Introducción a Swing
mrojas_unitec
 
PDF
JAVA THREADS.pdf
Mohit Kumar
 
Spring boot
Pradeep Shanmugam
 
ASP.NET Core MVC + Web API with Overview
Shahed Chowdhuri
 
Angular & RXJS: examples and use cases
Fabio Biondi
 
Introduction to Node.js
Vikash Singh
 
Design Pattern - Factory Method Pattern
Mudasir Qazi
 
Rediscovering Spring with Spring Boot(1)
Gunith Devasurendra
 
Introduction to Swift programming language.
Icalia Labs
 
Exception handling
Anna Pietras
 
Angular Pipes Workshop
Nir Kaufman
 
Spring Boot
Jiayun Zhou
 
Nest.js Introduction
Takuya Tejima
 
Operator overloading
Pranali Chaudhari
 
Constructors in java
chauhankapil
 
Express JS Rest API Tutorial
Simplilearn
 
Dependency Injection in Spring in 10min
Corneil du Plessis
 
Spring 3.x - Spring MVC - Advanced topics
Guy Nir
 
Introduction to Spring Boot
Purbarun Chakrabarti
 
Constructors in java
sunilchute1
 
Introducción a Swing
mrojas_unitec
 
JAVA THREADS.pdf
Mohit Kumar
 
Ad

Similar to object oriented programming using java, second sem BCA,UoM (20)

PPTX
UNIT 3- Java- Inheritance, Multithreading.pptx
shilpar780389
 
PPTX
Java basics
Shivanshu Purwar
 
PPTX
Chap-2 Classes & Methods.pptx
chetanpatilcp783
 
PDF
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
Tushar B Kute
 
PPTX
CJP Unit-1 contd.pptx
RAJASEKHARV10
 
PPTX
unit 2 java.pptx
AshokKumar587867
 
PPTX
Java
nirbhayverma8
 
PPTX
2- Introduction to java II
Ghadeer AlHasan
 
PDF
Java Programming - 04 object oriented in java
Danairat Thanabodithammachari
 
PPTX
Getters_And_Setters.pptx
Kavindu Sachinthe
 
PPTX
Object oriented programming in java
Elizabeth alexander
 
PPTX
Nitish Chaulagai Java1.pptx
NitishChaulagai
 
PDF
Constructors In Java – Unveiling Object Creation
Geekster
 
PPTX
Chapter 8 java
Ahmad sohail Kakar
 
PPTX
Module 1.pptx
YakaviBalakrishnan
 
DOCX
Lecture11.docx
ASADAHMAD811380
 
PDF
Java - Basic Concepts
Victer Paul
 
PDF
Class and Object JAVA PROGRAMMING LANG .pdf
sameer2543ynr
 
PPTX
Class and Object.pptx from nit patna ece department
om2348023vats
 
PPTX
PPT Lecture-1.4.pptx
HimanshuPandey957216
 
UNIT 3- Java- Inheritance, Multithreading.pptx
shilpar780389
 
Java basics
Shivanshu Purwar
 
Chap-2 Classes & Methods.pptx
chetanpatilcp783
 
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
Tushar B Kute
 
CJP Unit-1 contd.pptx
RAJASEKHARV10
 
unit 2 java.pptx
AshokKumar587867
 
2- Introduction to java II
Ghadeer AlHasan
 
Java Programming - 04 object oriented in java
Danairat Thanabodithammachari
 
Getters_And_Setters.pptx
Kavindu Sachinthe
 
Object oriented programming in java
Elizabeth alexander
 
Nitish Chaulagai Java1.pptx
NitishChaulagai
 
Constructors In Java – Unveiling Object Creation
Geekster
 
Chapter 8 java
Ahmad sohail Kakar
 
Module 1.pptx
YakaviBalakrishnan
 
Lecture11.docx
ASADAHMAD811380
 
Java - Basic Concepts
Victer Paul
 
Class and Object JAVA PROGRAMMING LANG .pdf
sameer2543ynr
 
Class and Object.pptx from nit patna ece department
om2348023vats
 
PPT Lecture-1.4.pptx
HimanshuPandey957216
 
Ad

More from ambikavenkatesh2 (19)

PPTX
CN(BCS502) Module-4 _Transport Layer.pptx
ambikavenkatesh2
 
PPTX
Module-3 Deadlocks.pptx BCS303 Operating system
ambikavenkatesh2
 
PPTX
V semester, computer networks BCS502 Module-2_DataLinkLayer
ambikavenkatesh2
 
PPTX
Module-1_Introduction to Data Communications.pptx
ambikavenkatesh2
 
PPTX
computer networks lab program Bellman Ford.pptx
ambikavenkatesh2
 
PPTX
Module-1.pptx Computer Networks BCS502 module-1 ppt
ambikavenkatesh2
 
PPTX
Module-1_Introduction to Data Communications.pptx
ambikavenkatesh2
 
PPTX
Concurrency Control in Databases.Database management systems
ambikavenkatesh2
 
PPTX
Operating systems Lab program: to develop C program to implement process mana...
ambikavenkatesh2
 
PPTX
MODULE-1_Operating System Services - ppt
ambikavenkatesh2
 
PPTX
Module1_Decision Support and Business Intelligence.pptx
ambikavenkatesh2
 
PPTX
Transactions and concurrency control mechanisms in database management system
ambikavenkatesh2
 
PPTX
data base management system notes on concurrency control
ambikavenkatesh2
 
PDF
Unit1_Fundamentals of Information Technlogy
ambikavenkatesh2
 
PPTX
Module-1 Data base management systems chap1-Introduction to database.pptx
ambikavenkatesh2
 
PPTX
data structures using C 2 sem BCA univeristy of mysore
ambikavenkatesh2
 
PPTX
Tableau.pptx
ambikavenkatesh2
 
PPTX
ICT.pptx
ambikavenkatesh2
 
PPTX
unit-1_Introduction to e-commerce.pptx
ambikavenkatesh2
 
CN(BCS502) Module-4 _Transport Layer.pptx
ambikavenkatesh2
 
Module-3 Deadlocks.pptx BCS303 Operating system
ambikavenkatesh2
 
V semester, computer networks BCS502 Module-2_DataLinkLayer
ambikavenkatesh2
 
Module-1_Introduction to Data Communications.pptx
ambikavenkatesh2
 
computer networks lab program Bellman Ford.pptx
ambikavenkatesh2
 
Module-1.pptx Computer Networks BCS502 module-1 ppt
ambikavenkatesh2
 
Module-1_Introduction to Data Communications.pptx
ambikavenkatesh2
 
Concurrency Control in Databases.Database management systems
ambikavenkatesh2
 
Operating systems Lab program: to develop C program to implement process mana...
ambikavenkatesh2
 
MODULE-1_Operating System Services - ppt
ambikavenkatesh2
 
Module1_Decision Support and Business Intelligence.pptx
ambikavenkatesh2
 
Transactions and concurrency control mechanisms in database management system
ambikavenkatesh2
 
data base management system notes on concurrency control
ambikavenkatesh2
 
Unit1_Fundamentals of Information Technlogy
ambikavenkatesh2
 
Module-1 Data base management systems chap1-Introduction to database.pptx
ambikavenkatesh2
 
data structures using C 2 sem BCA univeristy of mysore
ambikavenkatesh2
 
Tableau.pptx
ambikavenkatesh2
 
unit-1_Introduction to e-commerce.pptx
ambikavenkatesh2
 

Recently uploaded (20)

PPTX
TEF & EA Bsc Nursing 5th sem.....BBBpptx
AneetaSharma15
 
PPTX
NOI Hackathon - Summer Edition - GreenThumber.pptx
MartinaBurlando1
 
PPTX
family health care settings home visit - unit 6 - chn 1 - gnm 1st year.pptx
Priyanshu Anand
 
PPTX
Strengthening open access through collaboration: building connections with OP...
Jisc
 
PDF
UTS Health Student Promotional Representative_Position Description.pdf
Faculty of Health, University of Technology Sydney
 
PDF
The Minister of Tourism, Culture and Creative Arts, Abla Dzifa Gomashie has e...
nservice241
 
PPTX
How to Manage Global Discount in Odoo 18 POS
Celine George
 
PPTX
CARE OF UNCONSCIOUS PATIENTS .pptx
AneetaSharma15
 
PPTX
vedic maths in python:unleasing ancient wisdom with modern code
mistrymuskan14
 
PDF
3.The-Rise-of-the-Marathas.pdfppt/pdf/8th class social science Exploring Soci...
Sandeep Swamy
 
PPTX
Five Point Someone – Chetan Bhagat | Book Summary & Analysis by Bhupesh Kushwaha
Bhupesh Kushwaha
 
PDF
PG-BPSDMP 2 TAHUN 2025PG-BPSDMP 2 TAHUN 2025.pdf
AshifaRamadhani
 
PPTX
Skill Development Program For Physiotherapy Students by SRY.pptx
Prof.Dr.Y.SHANTHOSHRAJA MPT Orthopedic., MSc Microbiology
 
DOCX
SAROCES Action-Plan FOR ARAL PROGRAM IN DEPED
Levenmartlacuna1
 
PDF
1.Natural-Resources-and-Their-Use.ppt pdf /8th class social science Exploring...
Sandeep Swamy
 
PPTX
HISTORY COLLECTION FOR PSYCHIATRIC PATIENTS.pptx
PoojaSen20
 
PPTX
Software Engineering BSC DS UNIT 1 .pptx
Dr. Pallawi Bulakh
 
PPTX
Odoo 18 Sales_ Managing Quotation Validity
Celine George
 
PPTX
Information Texts_Infographic on Forgetting Curve.pptx
Tata Sevilla
 
PPTX
Tips Management in Odoo 18 POS - Odoo Slides
Celine George
 
TEF & EA Bsc Nursing 5th sem.....BBBpptx
AneetaSharma15
 
NOI Hackathon - Summer Edition - GreenThumber.pptx
MartinaBurlando1
 
family health care settings home visit - unit 6 - chn 1 - gnm 1st year.pptx
Priyanshu Anand
 
Strengthening open access through collaboration: building connections with OP...
Jisc
 
UTS Health Student Promotional Representative_Position Description.pdf
Faculty of Health, University of Technology Sydney
 
The Minister of Tourism, Culture and Creative Arts, Abla Dzifa Gomashie has e...
nservice241
 
How to Manage Global Discount in Odoo 18 POS
Celine George
 
CARE OF UNCONSCIOUS PATIENTS .pptx
AneetaSharma15
 
vedic maths in python:unleasing ancient wisdom with modern code
mistrymuskan14
 
3.The-Rise-of-the-Marathas.pdfppt/pdf/8th class social science Exploring Soci...
Sandeep Swamy
 
Five Point Someone – Chetan Bhagat | Book Summary & Analysis by Bhupesh Kushwaha
Bhupesh Kushwaha
 
PG-BPSDMP 2 TAHUN 2025PG-BPSDMP 2 TAHUN 2025.pdf
AshifaRamadhani
 
Skill Development Program For Physiotherapy Students by SRY.pptx
Prof.Dr.Y.SHANTHOSHRAJA MPT Orthopedic., MSc Microbiology
 
SAROCES Action-Plan FOR ARAL PROGRAM IN DEPED
Levenmartlacuna1
 
1.Natural-Resources-and-Their-Use.ppt pdf /8th class social science Exploring...
Sandeep Swamy
 
HISTORY COLLECTION FOR PSYCHIATRIC PATIENTS.pptx
PoojaSen20
 
Software Engineering BSC DS UNIT 1 .pptx
Dr. Pallawi Bulakh
 
Odoo 18 Sales_ Managing Quotation Validity
Celine George
 
Information Texts_Infographic on Forgetting Curve.pptx
Tata Sevilla
 
Tips Management in Odoo 18 POS - Odoo Slides
Celine George
 

object oriented programming using java, second sem BCA,UoM

  • 2. Constructors  A special type of method that enables an object to initialize itself when it is created  Constructors have the same name as the class itself  They do not specify a return type, not even void because they return the instance of the class itself.  It is called constructor because it constructs the values at the time of object creation
  • 3. Example: class Rectangle { int length; int width; Rectangle (int x, int y) // Defining constructor { length=x; width=y; } int rectArea() { return (length * width); } }
  • 4. Class RectangleArea { public static void main(string a[ ]) { Rectangle rect1=new Rectangle(15,10); //calling constructors int area1=rect1.rectArea( ); System.out.println(“Area1= ” +area1); } }
  • 5. Types of Constructor  In Java, constructors can be divided into three types: 1. No-Arg Constructor  If a constructor does not accept any parameters, it is known as a no-argument constructor.  Ex: class Data { Data() { System.out.println("No-Args Constructor"); } public static void main(String[] args) { Data d = new Data(); } }
  • 6. 2. Parameterized Constructor  A Java constructor can also accept one or more parameters. Such constructors are known as parameterized constructors (constructors with parameters). class Data { int num1, num2; Data(int m,int n) { num1=m; num2=n; } public static void main(String[] args) { Data d = new Data(10,20) } }
  • 7. 3. Default Constructor  If we do not create any constructor, the Java compiler automatically creates a no-arg constructor during the execution of the program.  This constructor is called the default constructor.  Ex: class Data { public static void main(String[] args) { Data d = new Data(); } }  Default constructor only role is to initialize the object and return it to the calling code.  Default constructor is always without argument and provided by java compiler only when there is no existing constructor defined.
  • 8. Overloaded constructor  Two or more constructors having different parameters is called overloaded constructor.  Ex: class Demo { int a,b,c; Demo() { a=1; b=2; c=3; } Demo(int x, int y) { a=x; b=y; c=20; }
  • 9. Demo(int x, int y, int z) { a=x; b=y; c=z; } } class Main { public static void main(String args[]) { Demo obj1=new Demo(); Demo obj2=new Demo(10,20); Demo obj3=new Demo(10,20,30); } }
  • 11. Java Garbage Collection  Java garbage collection is the process by which Java programs perform automatic memory management.  Java programs compile to bytecode that can be run on a Java Virtual Machine, or JVM for short.  When Java programs run on the JVM, objects are created on the heap, which is a portion of memory dedicated to the program.  Eventually, some objects will no longer be needed. The garbage collector finds these unused objects and deletes them to free up memory.
  • 12. Finalizer • Finalizer methods are almost the opposite of constructor methods. • A constructor method is used to initialize an object, while finalizer methods are called just before the object is garbage-collected and its memory reclaimed. • The syntax of the finalizer method is simply finalize(). • The Object class defines a default finalizer method. • To create a finalizer method, override the finalize() method using the following signature:
  • 13.  The body of finalize() method can include several objects including super.finalize(). This object allows the parent class to finalize an object, if necessary.  The finalize() method can be called at any time; however, calling finalize() does not trigger an object to be garbage-collected. Only removing all references to an object will cause it to be marked for deleting.  Finalizer methods are best used for optimizing the removal of an object (for example, by removing references to other objects) by releasing external resources that have been acquired (for example, external files), or for other behaviors that may make it easier for that object to be removed .
  • 14. Visibility modifiers  Java provides entities called “Access Modifiers or access specifiers” that help us to restrict the scope or visibility of a package, class, constructor, methods, variables, or other data members. These access modifiers are also called “Visibility Specifiers”.  The access specifiers also determine which data members (methods or fields) of a class can be accessed by other data members of classes or packages etc.  To ensure encapsulation and reusability, these access specifiers/modifiers are an integral part of object-oriented programming.
  • 15. Visibility modifiers in Java 1. Default: Whenever a specific access level is not specified, then it is assumed to be ‘default’. The scope of the default level is within the package. 2. Public: This is the most common access level and whenever the public access specifier is used with an entity, that particular entity is accessible throughout from within or outside the class, within or outside the package, etc. 3. Protected: The protected access level has a scope that is within the package. A protected entity is also accessible outside the package through inherited class or child class. 4. Private: When an entity is private, then this entity cannot be accessed outside the class. A private entity can only be accessible from within the class.
  • 17. Inbuilt classes - String  The String is a built-in class in Java to store a sequence of characters between double-quotes.  It’s under java.lang package so we don’t have to import, it will be imported automatically for every class.  Example: class Main { public static void main(String[] args) { String str = "apple"; System.out.println(str); // apple String name = new String("John"); System.out.println(name); // John } }
  • 18.  The strings are objects in java of the class ‘String’. System.out.println(“Welcome to java”); The string “welcome to java” is automatically converted into a string object by java.  A string in java is not a character array and it is not terminated with “NULL”.