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

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

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
 
PPT
Core_java_ppt.ppt
SHIBDASDUTTA
 
PPTX
Lecture 5
talha ijaz
 
PPT
Md03 - part3
Rakesh Madugula
 
PPTX
Object Oriented Programming unit 1 content for students
ASHASITTeaching
 
PPTX
Ch-2ppt.pptx
ssuser8347a1
 
PDF
03_Objects and Classes in java.pdf
Parameshwar Maddela
 
PPTX
Classes, Inheritance ,Packages & Interfaces.pptx
DivyaKS18
 
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
 
Core_java_ppt.ppt
SHIBDASDUTTA
 
Lecture 5
talha ijaz
 
Md03 - part3
Rakesh Madugula
 
Object Oriented Programming unit 1 content for students
ASHASITTeaching
 
Ch-2ppt.pptx
ssuser8347a1
 
03_Objects and Classes in java.pdf
Parameshwar Maddela
 
Classes, Inheritance ,Packages & Interfaces.pptx
DivyaKS18
 

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
 
Ad

Recently uploaded (20)

PPTX
GRADE-3-PPT-EVE-2025-ENG-Q1-LESSON-1.pptx
EveOdrapngimapNarido
 
PPTX
PPT-Q1-WEEK-3-SCIENCE-ERevised Matatag Grade 3.pptx
reijhongidayawan02
 
PDF
QNL June Edition hosted by Pragya the official Quiz Club of the University of...
Pragya - UEM Kolkata Quiz Club
 
PPTX
How to Handle Salesperson Commision in Odoo 18 Sales
Celine George
 
PDF
Geographical Diversity of India 100 Mcq.pdf/ 7th class new ncert /Social/Samy...
Sandeep Swamy
 
PDF
The History of Phone Numbers in Stoke Newington by Billy Thomas
History of Stoke Newington
 
PDF
The Constitution Review Committee (CRC) has released an updated schedule for ...
nservice241
 
PPTX
I AM MALALA The Girl Who Stood Up for Education and was Shot by the Taliban...
Beena E S
 
PPTX
Cultivation practice of Litchi in Nepal.pptx
UmeshTimilsina1
 
PPTX
Unit 2 COMMERCIAL BANKING, Corporate banking.pptx
AnubalaSuresh1
 
PPTX
MENINGITIS: NURSING MANAGEMENT, BACTERIAL MENINGITIS, VIRAL MENINGITIS.pptx
PRADEEP ABOTHU
 
PDF
CONCURSO DE POESIA “POETUFAS – PASSOS SUAVES PELO VERSO.pdf
Colégio Santa Teresinha
 
PPTX
Growth and development and milestones, factors
BHUVANESHWARI BADIGER
 
PPTX
ASRB NET 2023 PREVIOUS YEAR QUESTION PAPER GENETICS AND PLANT BREEDING BY SAT...
Krashi Coaching
 
PPT
Talk on Critical Theory, Part II, Philosophy of Social Sciences
Soraj Hongladarom
 
PPTX
How to Set Up Tags in Odoo 18 - Odoo Slides
Celine George
 
PPTX
A PPT on Alfred Lord Tennyson's Ulysses.
Beena E S
 
PPTX
How to Manage Large Scrollbar in Odoo 18 POS
Celine George
 
PPTX
Neurodivergent Friendly Schools - Slides from training session
Pooky Knightsmith
 
PPTX
PATIENT ASSIGNMENTS AND NURSING CARE RESPONSIBILITIES.pptx
PRADEEP ABOTHU
 
GRADE-3-PPT-EVE-2025-ENG-Q1-LESSON-1.pptx
EveOdrapngimapNarido
 
PPT-Q1-WEEK-3-SCIENCE-ERevised Matatag Grade 3.pptx
reijhongidayawan02
 
QNL June Edition hosted by Pragya the official Quiz Club of the University of...
Pragya - UEM Kolkata Quiz Club
 
How to Handle Salesperson Commision in Odoo 18 Sales
Celine George
 
Geographical Diversity of India 100 Mcq.pdf/ 7th class new ncert /Social/Samy...
Sandeep Swamy
 
The History of Phone Numbers in Stoke Newington by Billy Thomas
History of Stoke Newington
 
The Constitution Review Committee (CRC) has released an updated schedule for ...
nservice241
 
I AM MALALA The Girl Who Stood Up for Education and was Shot by the Taliban...
Beena E S
 
Cultivation practice of Litchi in Nepal.pptx
UmeshTimilsina1
 
Unit 2 COMMERCIAL BANKING, Corporate banking.pptx
AnubalaSuresh1
 
MENINGITIS: NURSING MANAGEMENT, BACTERIAL MENINGITIS, VIRAL MENINGITIS.pptx
PRADEEP ABOTHU
 
CONCURSO DE POESIA “POETUFAS – PASSOS SUAVES PELO VERSO.pdf
Colégio Santa Teresinha
 
Growth and development and milestones, factors
BHUVANESHWARI BADIGER
 
ASRB NET 2023 PREVIOUS YEAR QUESTION PAPER GENETICS AND PLANT BREEDING BY SAT...
Krashi Coaching
 
Talk on Critical Theory, Part II, Philosophy of Social Sciences
Soraj Hongladarom
 
How to Set Up Tags in Odoo 18 - Odoo Slides
Celine George
 
A PPT on Alfred Lord Tennyson's Ulysses.
Beena E S
 
How to Manage Large Scrollbar in Odoo 18 POS
Celine George
 
Neurodivergent Friendly Schools - Slides from training session
Pooky Knightsmith
 
PATIENT ASSIGNMENTS AND NURSING CARE RESPONSIBILITIES.pptx
PRADEEP ABOTHU
 
Ad

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”.