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
Structure & Union in C++
PDF
Chapter 7 - Constructors.pdf
PDF
Java variable types
PPTX
inheritance c++
PPTX
Unit 5 java-awt (1)
PPTX
Java features
PPTX
C# classes objects
PPTX
Constructor and Destructor in c++
Structure & Union in C++
Chapter 7 - Constructors.pdf
Java variable types
inheritance c++
Unit 5 java-awt (1)
Java features
C# classes objects
Constructor and Destructor in c++

What's hot (20)

PPTX
03. Operators Expressions and statements
PPTX
C# Arrays
PPTX
Control Statements in Java
PPTX
String Handling in c++
PPTX
Introduction to C# Programming
PPTX
Java interfaces
PDF
Java arrays
PPTX
C# language
PPTX
Common language runtime clr
PPTX
04. Console Input Output
PPTX
Inheritance in c++
PPS
Introduction to class in java
PPT
Abstract class in java
PDF
Strings in java
PPTX
INHERITANCE IN JAVA.pptx
PDF
Java 8 Default Methods
PPT
Introduction to C++
PPT
Methods in C#
PDF
Generics
PPTX
05. Conditional Statements
03. Operators Expressions and statements
C# Arrays
Control Statements in Java
String Handling in c++
Introduction to C# Programming
Java interfaces
Java arrays
C# language
Common language runtime clr
04. Console Input Output
Inheritance in c++
Introduction to class in java
Abstract class in java
Strings in java
INHERITANCE IN JAVA.pptx
Java 8 Default Methods
Introduction to C++
Methods in C#
Generics
05. Conditional Statements
Ad

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

PDF
Java Programming - 04 object oriented in java
PDF
OOPs & Inheritance Notes
PPTX
Unit3 packages & interfaces
PPT
C++ classes
DOCX
Lecture22.23.07.2014
PPTX
Unit3 part1-class
PPTX
Classes, Inheritance ,Packages & Interfaces.pptx
PPTX
Unit3 part3-packages and interfaces
PPTX
UNIT 3- Java- Inheritance, Multithreading.pptx
PDF
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
PDF
CHAPTER 3 part1.pdf
PPTX
UNIT - IIInew.pptx
PPT
packages and interfaces
PPT
object oriented programming language by c++
PPTX
Java basic part 2 : Datatypes Keywords Features Components Security Exceptions
PPTX
classes-objects in oops java-201023154255.pptx
PPTX
Chap-2 Classes & Methods.pptx
PPT
Sonu wiziq
PPT
Object Oriented Programming Concept.Hello
PPT
Classes & objects new
Java Programming - 04 object oriented in java
OOPs & Inheritance Notes
Unit3 packages & interfaces
C++ classes
Lecture22.23.07.2014
Unit3 part1-class
Classes, Inheritance ,Packages & Interfaces.pptx
Unit3 part3-packages and interfaces
UNIT 3- Java- Inheritance, Multithreading.pptx
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
CHAPTER 3 part1.pdf
UNIT - IIInew.pptx
packages and interfaces
object oriented programming language by c++
Java basic part 2 : Datatypes Keywords Features Components Security Exceptions
classes-objects in oops java-201023154255.pptx
Chap-2 Classes & Methods.pptx
Sonu wiziq
Object Oriented Programming Concept.Hello
Classes & objects new
Ad

More from ambikavenkatesh2 (19)

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

Recently uploaded (20)

PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PDF
Pre independence Education in Inndia.pdf
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PPTX
Cell Types and Its function , kingdom of life
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PPTX
Institutional Correction lecture only . . .
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PPTX
Lesson notes of climatology university.
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
RMMM.pdf make it easy to upload and study
PDF
VCE English Exam - Section C Student Revision Booklet
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PPTX
Pharma ospi slides which help in ospi learning
PDF
Sports Quiz easy sports quiz sports quiz
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
Pre independence Education in Inndia.pdf
Pharmacology of Heart Failure /Pharmacotherapy of CHF
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
Cell Types and Its function , kingdom of life
2.FourierTransform-ShortQuestionswithAnswers.pdf
Institutional Correction lecture only . . .
Renaissance Architecture: A Journey from Faith to Humanism
Lesson notes of climatology university.
Final Presentation General Medicine 03-08-2024.pptx
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
RMMM.pdf make it easy to upload and study
VCE English Exam - Section C Student Revision Booklet
human mycosis Human fungal infections are called human mycosis..pptx
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
Pharma ospi slides which help in ospi learning
Sports Quiz easy sports quiz sports quiz

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