SlideShare a Scribd company logo
1 of 39
Download to read offline
Jagannath Institute of Management Sciences
Vasant Kunj-II, New Delhi - 110070
Subject Name: BVITSD 404 : Fundamentals Of Web
Programming Using JAVA
Department of Information Technology
Created By: Dr. Arpana Chaturvedi
@Dr. Arpana Chaturvedi
Subject: BVITSD 404 : FUNDAMENTALS OF
WEB PROGRAMMING USING JAVA
Topic: Unit II- Constructor Handling
@Dr. Arpana Chaturvedi
Unit-I Constructor Handling
Table of Contents
▰ Constructors
▰ Types of Constructors
▰ Constructor Overloading
▰ Garbage collection
▰ Understand static
▰ Understanding final
▰ Polymorphism (overloading, overriding).
@Dr. Arpana Chaturvedi
Unit-II Constructors
▰ A constructor initializes an object when it is created. It has the same name as its
class and is syntactically similar to a method. However, constructors have no
explicit return type.
▰ Typically, you will use a constructor to give initial values to the instance variables
defined by the class, or to perform any other start-up procedures required to create
a fully formed object.
▰ All classes have constructors, whether you define one or not, because Java
automatically provides a default constructor that initializes all member variables to
zero. However, once you define your own constructor, the default constructor is no
longer used.
▰ Syntax
▰ class ClassName {
▰ ClassName() {
▰ }
▰ } @Dr. Arpana Chaturvedi
Unit-II Rules for creating Java constructor
There are three rules defined for the constructor.
▰ Constructor name must be the same as its class name.
▰ A Constructor must have no explicit return type.
▰ A Java constructor cannot be abstract, static, final, and synchronized.
Note: We can use access modifiers while declaring a constructor. It controls the object
creation. In other words, we can have private, protected, public or default constructor in
Java.
@Dr. Arpana Chaturvedi
Unit-II Types of Constructors
Java allows two types of constructors namely
▰ Default or No argument Constructors
▰ Parameterized Constructors
▰ Dynamic Constructor
▰ Copy Constructor
@Dr. Arpana Chaturvedi
Unit-I Default or No Argument Constructors
Default or No argument Constructors
▰ As the name specifies the no argument constructors of Java does not accept any
parameters instead, using these constructors the instance variables of a method
will be initialized with fixed values for all objects.
Rule: If there is no constructor in a class, compiler automatically creates a default
constructor.
@Dr. Arpana Chaturvedi
Unit-I Example: Default or No Argument
Constructors
Class with Default Constructor
@Dr. Arpana Chaturvedi
Calling Constructors to initialize objects
Output
Unit-I Example 2: Default Constructor
▰ Default constructor that displays the default values
@Dr. Arpana Chaturvedi
Output
Unit-I Example 2: Default Constructor
▰ Default constructor that displays the default values
@Dr. Arpana Chaturvedi
Output
Unit-II Parametrized Constructors
Parameterized Constructors:
▰ A constructor which has a specific number of parameters is called a
parameterized constructor.
Need of Parametrized Constructors:
▰ Most often, you will need a constructor that accepts one or more parameters.
Parameters are added to a constructor in the same way that they are added to a
method, just declare them inside the parentheses after the constructor's name.
▰ The parameterized constructor is used to provide different values to distinct
objects. However, you can provide the same values also.
@Dr. Arpana Chaturvedi
Unit-II Example of Parametrized Constructors
Class with Parametrized Constructor
@Dr. Arpana Chaturvedi
Unit-II Example of Parametrized Constructors
@Dr. Arpana Chaturvedi
Calling constructor to initialize objects
Output
Unit-II Example of Parametrized Constructor
@Dr. Arpana Chaturvedi
Unit-I Constructor Overloading in Java
▰ In Java, a constructor is just like a method but without return type. It can also
be overloaded like Java methods.
▰ Constructor overloading in Java is a technique of having more than one
constructor with different parameter lists. They are arranged in a way that each
constructor performs a different task. They are differentiated by the compiler by
the number of parameters in the list and their types.
@Dr. Arpana Chaturvedi
Unit-I Constructor Overloading
@Dr. Arpana Chaturvedi
Unit-II Difference between constructor and
method in Java
@Dr. Arpana Chaturvedi
Unit-II Java Copy Constructor
▰ There is no copy constructor in Java. However, we can copy the values from
one object to another like copy constructor in C++.
▰ There are many ways to copy the values of one object into another in Java.
They are:
▰ By constructor
▰ By assigning the values of one object into another
▰ By clone() method of Object class
@Dr. Arpana Chaturvedi
Unit-II JAVA Copy Constructor
@Dr. Arpana Chaturvedi
Unit-II Copying values without constructor
We can copy the values of one object into another by assigning the objects values to
another object. In this case, there is no need to create the constructor.
@Dr. Arpana Chaturvedi
Unit-II JAVA Static Keyword
▰ The static keyword in Java is used for memory management mainly. We can
apply static keyword with variables, methods, blocks and nested classes. The
static keyword belongs to the class than an instance of the class.
▰ The static can be:
▰ Variable (also known as a class variable)
▰ Method (also known as a class method)
▰ Block
▰ Nested class
@Dr. Arpana Chaturvedi
Unit-II JAVA Static Variable
▰ If you declare any variable as static, it is known as a static variable.
▰ The static variable can be used to refer to the common property of all objects
(which is not unique for each object), for example, the company name of
employees, college name of students, etc.
▰ The static variable gets memory only once in the class area at the time of class
loading.
▰ Advantages of static variable
▰ It makes your program memory efficient (i.e., it saves memory).
@Dr. Arpana Chaturvedi
Unit-II Problem without static variable
▰ class Student{
▰ int rollno;
▰ String name;
▰ String college="ITS";
▰ }
▰ Suppose there are 500 students in my college, now all instance data members will
get memory each time when the object is created. All students have its unique
rollno and name, so instance data member is good in such case. Here, "college"
refers to the common property of all objects. If we make it static, this field will get
the memory only once.
▰ Java static property is shared to all objects.
@Dr. Arpana Chaturvedi
Unit-II Example of Static Variable
@Dr. Arpana Chaturvedi
Unit-II JAVA Static Variable
@Dr. Arpana Chaturvedi
Unit-II Program of the counter without static
variable
We have created an instance variable named count which is incremented in the
constructor. Since instance variable gets the memory at the time of object creation,
each object will have the copy of the instance variable. If it is incremented, it won't
reflect other objects. So each object will have the value 1 in the count variable.
@Dr. Arpana Chaturvedi
Unit-II Program of counter by Static Variable
@Dr. Arpana Chaturvedi
Static variable will get the memory only once, if any object changes the value
of the static variable, it will retain its value.
Unit-II JAVA Static Method
If you apply static keyword with any method, it is known as static method.
• A static method belongs to the class rather than the object of a class.
• A static method can be invoked without the need for creating an instance of a
class.
• A static method can access static data member and can change the value of it.
@Dr. Arpana Chaturvedi
Unit-I Client Side Technologies- Web 2.0
@Dr. Arpana Chaturvedi
Unit-II Another example of a static method
that performs a normal calculation
@Dr. Arpana Chaturvedi
Unit-II Restrictions for the static method
▰ There are two main restrictions for the static method. They are:
▰ The static method can not use non static data member or call non-static method
directly.
▰ this and super cannot be used in static context.
@Dr. Arpana Chaturvedi
Why is the Java main method
static?
Ans) It is because the object is
not required to call a static
method. If it were a non-static
method, JVM creates an object
first then call main() method that
will lead the problem of extra
memory allocation.
Unit-II JAVA Static Block
▰ Is used to initialize the static data member.
▰ It is executed before the main method at the time of classloading.
@Dr. Arpana Chaturvedi
Unit-I Program without main Function
▰ Can we execute a program without main() method?
▰ Ans) No, one of the ways was the static block, but it was possible till JDK 1.6.
Since JDK 1.7, it is not possible to execute a Java class without the main method.
@Dr. Arpana Chaturvedi
Unit-II Understanding final
The final keyword in java is used to restrict the user. The java final keyword can be
used in many context. Final can be:
▰ variable
▰ method
▰ class
The final keyword can be applied with the variables, a final variable that have no value it
is called blank final variable or uninitialized final variable. It can be initialized in the
constructor only. The blank final variable can be static also which will be initialized in
the static block only. We will have detailed learning of these.
@Dr. Arpana Chaturvedi
Unit-II JAVA Final Variable
If you make any variable as final, you cannot change the value of final variable(It will be
constant).
There is a final variable speed limit, we are going to change the value of this variable,
but It can't be changed because final variable once assigned a value can never be
changed.
@Dr. Arpana Chaturvedi
Unit-II Java final method
If you make any method as final, you cannot override it.
@Dr. Arpana Chaturvedi
Unit-II JAVA Final Class
If you make any class as final, you cannot extend it.
@Dr. Arpana Chaturvedi
Unit-II final method
▰ Is final method inherited?
▰ Yes, final method is inherited but you cannot override it. For Example:
@Dr. Arpana Chaturvedi
Thank You !!

More Related Content

What's hot

What's hot (18)

Java beans
Java beansJava beans
Java beans
 
Swing api
Swing apiSwing api
Swing api
 
Introduction to Scala
Introduction to ScalaIntroduction to Scala
Introduction to Scala
 
Java Interview Questions For Freshers
Java Interview Questions For FreshersJava Interview Questions For Freshers
Java Interview Questions For Freshers
 
Statics in java | Constructors | Exceptions in Java | String in java| class 3
Statics in java | Constructors | Exceptions in Java | String in java| class 3Statics in java | Constructors | Exceptions in Java | String in java| class 3
Statics in java | Constructors | Exceptions in Java | String in java| class 3
 
Distributed Programming (RMI)
Distributed Programming (RMI)Distributed Programming (RMI)
Distributed Programming (RMI)
 
Hibernate introduction
Hibernate introductionHibernate introduction
Hibernate introduction
 
Java interview question
Java interview questionJava interview question
Java interview question
 
Static keyword ppt
Static keyword pptStatic keyword ppt
Static keyword ppt
 
Final keyword in java
Final keyword in javaFinal keyword in java
Final keyword in java
 
Java questions for interview
Java questions for interviewJava questions for interview
Java questions for interview
 
Java
JavaJava
Java
 
Introduction to Java
Introduction to JavaIntroduction to Java
Introduction to Java
 
JavaScript OOPS Implimentation
JavaScript OOPS ImplimentationJavaScript OOPS Implimentation
JavaScript OOPS Implimentation
 
Java Constructors | Java Course
Java Constructors | Java CourseJava Constructors | Java Course
Java Constructors | Java Course
 
Java basic concept
Java basic conceptJava basic concept
Java basic concept
 
Concurrency
ConcurrencyConcurrency
Concurrency
 
Java Constructors
Java ConstructorsJava Constructors
Java Constructors
 

Similar to Unit 2 Part 1 Constructors.pdf

Unit No 2 Objects and Classes.pptx
Unit No 2 Objects and Classes.pptxUnit No 2 Objects and Classes.pptx
Unit No 2 Objects and Classes.pptxDrYogeshDeshmukh1
 
Object oriented programming in java
Object oriented programming in javaObject oriented programming in java
Object oriented programming in javaElizabeth alexander
 
Java - Basic Concepts
Java - Basic ConceptsJava - Basic Concepts
Java - Basic ConceptsVicter Paul
 
Md02 - Getting Started part-2
Md02 - Getting Started part-2Md02 - Getting Started part-2
Md02 - Getting Started part-2Rakesh Madugula
 
Qb it1301
Qb   it1301Qb   it1301
Qb it1301ArthyR3
 
Dev labs alliance top 20 basic java interview questions for sdet
Dev labs alliance top 20 basic java interview questions for sdetDev labs alliance top 20 basic java interview questions for sdet
Dev labs alliance top 20 basic java interview questions for sdetDevLabs Alliance
 
Object-Oriented Programming with Java UNIT 1
Object-Oriented Programming with Java UNIT 1Object-Oriented Programming with Java UNIT 1
Object-Oriented Programming with Java UNIT 1SURBHI SAROHA
 
Unit 2 Part 1 POLYMORPHISM.pdf
Unit 2 Part 1 POLYMORPHISM.pdfUnit 2 Part 1 POLYMORPHISM.pdf
Unit 2 Part 1 POLYMORPHISM.pdfArpana Awasthi
 
Constructors in Java (2).pdf
Constructors in Java (2).pdfConstructors in Java (2).pdf
Constructors in Java (2).pdfkumari36
 
Java interview questions and answers for cognizant By Data Council Pune
Java interview questions and answers for cognizant By Data Council PuneJava interview questions and answers for cognizant By Data Council Pune
Java interview questions and answers for cognizant By Data Council PunePankaj kshirsagar
 
Top 20 basic java interview questions for SDET
Top 20 basic java interview questions for SDETTop 20 basic java interview questions for SDET
Top 20 basic java interview questions for SDETDevLabs Alliance
 
Dev labs alliance top 20 basic java interview question for sdet
Dev labs alliance top 20 basic java interview question for sdetDev labs alliance top 20 basic java interview question for sdet
Dev labs alliance top 20 basic java interview question for sdetdevlabsalliance
 

Similar to Unit 2 Part 1 Constructors.pdf (20)

Unit No 2 Objects and Classes.pptx
Unit No 2 Objects and Classes.pptxUnit No 2 Objects and Classes.pptx
Unit No 2 Objects and Classes.pptx
 
Object oriented programming in java
Object oriented programming in javaObject oriented programming in java
Object oriented programming in java
 
Java - Basic Concepts
Java - Basic ConceptsJava - Basic Concepts
Java - Basic Concepts
 
Java unit 7
Java unit 7Java unit 7
Java unit 7
 
java basic .pdf
java basic .pdfjava basic .pdf
java basic .pdf
 
Viva file
Viva fileViva file
Viva file
 
Java basics
Java basicsJava basics
Java basics
 
Md02 - Getting Started part-2
Md02 - Getting Started part-2Md02 - Getting Started part-2
Md02 - Getting Started part-2
 
Java static keyword
Java static keywordJava static keyword
Java static keyword
 
Qb it1301
Qb   it1301Qb   it1301
Qb it1301
 
JAVA PPT -2 BY ADI.pdf
JAVA PPT -2 BY ADI.pdfJAVA PPT -2 BY ADI.pdf
JAVA PPT -2 BY ADI.pdf
 
Dev labs alliance top 20 basic java interview questions for sdet
Dev labs alliance top 20 basic java interview questions for sdetDev labs alliance top 20 basic java interview questions for sdet
Dev labs alliance top 20 basic java interview questions for sdet
 
Java notes
Java notesJava notes
Java notes
 
PPT Lecture-1.4.pptx
PPT Lecture-1.4.pptxPPT Lecture-1.4.pptx
PPT Lecture-1.4.pptx
 
Object-Oriented Programming with Java UNIT 1
Object-Oriented Programming with Java UNIT 1Object-Oriented Programming with Java UNIT 1
Object-Oriented Programming with Java UNIT 1
 
Unit 2 Part 1 POLYMORPHISM.pdf
Unit 2 Part 1 POLYMORPHISM.pdfUnit 2 Part 1 POLYMORPHISM.pdf
Unit 2 Part 1 POLYMORPHISM.pdf
 
Constructors in Java (2).pdf
Constructors in Java (2).pdfConstructors in Java (2).pdf
Constructors in Java (2).pdf
 
Java interview questions and answers for cognizant By Data Council Pune
Java interview questions and answers for cognizant By Data Council PuneJava interview questions and answers for cognizant By Data Council Pune
Java interview questions and answers for cognizant By Data Council Pune
 
Top 20 basic java interview questions for SDET
Top 20 basic java interview questions for SDETTop 20 basic java interview questions for SDET
Top 20 basic java interview questions for SDET
 
Dev labs alliance top 20 basic java interview question for sdet
Dev labs alliance top 20 basic java interview question for sdetDev labs alliance top 20 basic java interview question for sdet
Dev labs alliance top 20 basic java interview question for sdet
 

More from Arpana Awasthi

Unit 2 Part 1.2 Data Types.pdf
Unit 2 Part 1.2 Data Types.pdfUnit 2 Part 1.2 Data Types.pdf
Unit 2 Part 1.2 Data Types.pdfArpana Awasthi
 
Introduction To Python.pdf
Introduction To Python.pdfIntroduction To Python.pdf
Introduction To Python.pdfArpana Awasthi
 
Eclipse - GUI Palette
Eclipse - GUI Palette Eclipse - GUI Palette
Eclipse - GUI Palette Arpana Awasthi
 
Introduction to Eclipse
Introduction to Eclipse Introduction to Eclipse
Introduction to Eclipse Arpana Awasthi
 
Arrays 2 Dimensional Unit 2 Part 1.pdf
Arrays 2 Dimensional Unit 2 Part 1.pdfArrays 2 Dimensional Unit 2 Part 1.pdf
Arrays 2 Dimensional Unit 2 Part 1.pdfArpana Awasthi
 
Arrays Fundamentals Unit II
Arrays  Fundamentals Unit IIArrays  Fundamentals Unit II
Arrays Fundamentals Unit IIArpana Awasthi
 
Machine Learning: Need of Machine Learning, Its Challenges and its Applications
Machine Learning: Need of Machine Learning, Its Challenges and its ApplicationsMachine Learning: Need of Machine Learning, Its Challenges and its Applications
Machine Learning: Need of Machine Learning, Its Challenges and its ApplicationsArpana Awasthi
 
File Handling in C Part I
File Handling in C Part IFile Handling in C Part I
File Handling in C Part IArpana Awasthi
 
Role of machine learning in detection, prevention and treatment of cancer
Role of machine learning in detection, prevention and treatment of cancerRole of machine learning in detection, prevention and treatment of cancer
Role of machine learning in detection, prevention and treatment of cancerArpana Awasthi
 

More from Arpana Awasthi (11)

Unit 5 Part 1 Macros
Unit 5 Part 1 MacrosUnit 5 Part 1 Macros
Unit 5 Part 1 Macros
 
Unit 2 Part 1.2 Data Types.pdf
Unit 2 Part 1.2 Data Types.pdfUnit 2 Part 1.2 Data Types.pdf
Unit 2 Part 1.2 Data Types.pdf
 
Introduction To Python.pdf
Introduction To Python.pdfIntroduction To Python.pdf
Introduction To Python.pdf
 
Eclipse - GUI Palette
Eclipse - GUI Palette Eclipse - GUI Palette
Eclipse - GUI Palette
 
Introduction to Eclipse
Introduction to Eclipse Introduction to Eclipse
Introduction to Eclipse
 
Arrays 2 Dimensional Unit 2 Part 1.pdf
Arrays 2 Dimensional Unit 2 Part 1.pdfArrays 2 Dimensional Unit 2 Part 1.pdf
Arrays 2 Dimensional Unit 2 Part 1.pdf
 
Arrays Fundamentals Unit II
Arrays  Fundamentals Unit IIArrays  Fundamentals Unit II
Arrays Fundamentals Unit II
 
Machine Learning: Need of Machine Learning, Its Challenges and its Applications
Machine Learning: Need of Machine Learning, Its Challenges and its ApplicationsMachine Learning: Need of Machine Learning, Its Challenges and its Applications
Machine Learning: Need of Machine Learning, Its Challenges and its Applications
 
File Handling in C Part I
File Handling in C Part IFile Handling in C Part I
File Handling in C Part I
 
Programming language
Programming languageProgramming language
Programming language
 
Role of machine learning in detection, prevention and treatment of cancer
Role of machine learning in detection, prevention and treatment of cancerRole of machine learning in detection, prevention and treatment of cancer
Role of machine learning in detection, prevention and treatment of cancer
 

Recently uploaded

Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Paola De la Torre
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure servicePooja Nehwal
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxOnBoard
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhisoniya singh
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersThousandEyes
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
Google AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAGGoogle AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAGSujit Pal
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...gurkirankumar98700
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 

Recently uploaded (20)

Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptx
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
Google AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAGGoogle AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAG
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 

Unit 2 Part 1 Constructors.pdf

  • 1. Jagannath Institute of Management Sciences Vasant Kunj-II, New Delhi - 110070 Subject Name: BVITSD 404 : Fundamentals Of Web Programming Using JAVA Department of Information Technology Created By: Dr. Arpana Chaturvedi @Dr. Arpana Chaturvedi
  • 2. Subject: BVITSD 404 : FUNDAMENTALS OF WEB PROGRAMMING USING JAVA Topic: Unit II- Constructor Handling @Dr. Arpana Chaturvedi
  • 3. Unit-I Constructor Handling Table of Contents ▰ Constructors ▰ Types of Constructors ▰ Constructor Overloading ▰ Garbage collection ▰ Understand static ▰ Understanding final ▰ Polymorphism (overloading, overriding). @Dr. Arpana Chaturvedi
  • 4. Unit-II Constructors ▰ A constructor initializes an object when it is created. It has the same name as its class and is syntactically similar to a method. However, constructors have no explicit return type. ▰ Typically, you will use a constructor to give initial values to the instance variables defined by the class, or to perform any other start-up procedures required to create a fully formed object. ▰ All classes have constructors, whether you define one or not, because Java automatically provides a default constructor that initializes all member variables to zero. However, once you define your own constructor, the default constructor is no longer used. ▰ Syntax ▰ class ClassName { ▰ ClassName() { ▰ } ▰ } @Dr. Arpana Chaturvedi
  • 5. Unit-II Rules for creating Java constructor There are three rules defined for the constructor. ▰ Constructor name must be the same as its class name. ▰ A Constructor must have no explicit return type. ▰ A Java constructor cannot be abstract, static, final, and synchronized. Note: We can use access modifiers while declaring a constructor. It controls the object creation. In other words, we can have private, protected, public or default constructor in Java. @Dr. Arpana Chaturvedi
  • 6. Unit-II Types of Constructors Java allows two types of constructors namely ▰ Default or No argument Constructors ▰ Parameterized Constructors ▰ Dynamic Constructor ▰ Copy Constructor @Dr. Arpana Chaturvedi
  • 7. Unit-I Default or No Argument Constructors Default or No argument Constructors ▰ As the name specifies the no argument constructors of Java does not accept any parameters instead, using these constructors the instance variables of a method will be initialized with fixed values for all objects. Rule: If there is no constructor in a class, compiler automatically creates a default constructor. @Dr. Arpana Chaturvedi
  • 8. Unit-I Example: Default or No Argument Constructors Class with Default Constructor @Dr. Arpana Chaturvedi Calling Constructors to initialize objects Output
  • 9. Unit-I Example 2: Default Constructor ▰ Default constructor that displays the default values @Dr. Arpana Chaturvedi Output
  • 10. Unit-I Example 2: Default Constructor ▰ Default constructor that displays the default values @Dr. Arpana Chaturvedi Output
  • 11. Unit-II Parametrized Constructors Parameterized Constructors: ▰ A constructor which has a specific number of parameters is called a parameterized constructor. Need of Parametrized Constructors: ▰ Most often, you will need a constructor that accepts one or more parameters. Parameters are added to a constructor in the same way that they are added to a method, just declare them inside the parentheses after the constructor's name. ▰ The parameterized constructor is used to provide different values to distinct objects. However, you can provide the same values also. @Dr. Arpana Chaturvedi
  • 12. Unit-II Example of Parametrized Constructors Class with Parametrized Constructor @Dr. Arpana Chaturvedi
  • 13. Unit-II Example of Parametrized Constructors @Dr. Arpana Chaturvedi Calling constructor to initialize objects Output
  • 14. Unit-II Example of Parametrized Constructor @Dr. Arpana Chaturvedi
  • 15. Unit-I Constructor Overloading in Java ▰ In Java, a constructor is just like a method but without return type. It can also be overloaded like Java methods. ▰ Constructor overloading in Java is a technique of having more than one constructor with different parameter lists. They are arranged in a way that each constructor performs a different task. They are differentiated by the compiler by the number of parameters in the list and their types. @Dr. Arpana Chaturvedi
  • 17. Unit-II Difference between constructor and method in Java @Dr. Arpana Chaturvedi
  • 18. Unit-II Java Copy Constructor ▰ There is no copy constructor in Java. However, we can copy the values from one object to another like copy constructor in C++. ▰ There are many ways to copy the values of one object into another in Java. They are: ▰ By constructor ▰ By assigning the values of one object into another ▰ By clone() method of Object class @Dr. Arpana Chaturvedi
  • 19. Unit-II JAVA Copy Constructor @Dr. Arpana Chaturvedi
  • 20. Unit-II Copying values without constructor We can copy the values of one object into another by assigning the objects values to another object. In this case, there is no need to create the constructor. @Dr. Arpana Chaturvedi
  • 21. Unit-II JAVA Static Keyword ▰ The static keyword in Java is used for memory management mainly. We can apply static keyword with variables, methods, blocks and nested classes. The static keyword belongs to the class than an instance of the class. ▰ The static can be: ▰ Variable (also known as a class variable) ▰ Method (also known as a class method) ▰ Block ▰ Nested class @Dr. Arpana Chaturvedi
  • 22. Unit-II JAVA Static Variable ▰ If you declare any variable as static, it is known as a static variable. ▰ The static variable can be used to refer to the common property of all objects (which is not unique for each object), for example, the company name of employees, college name of students, etc. ▰ The static variable gets memory only once in the class area at the time of class loading. ▰ Advantages of static variable ▰ It makes your program memory efficient (i.e., it saves memory). @Dr. Arpana Chaturvedi
  • 23. Unit-II Problem without static variable ▰ class Student{ ▰ int rollno; ▰ String name; ▰ String college="ITS"; ▰ } ▰ Suppose there are 500 students in my college, now all instance data members will get memory each time when the object is created. All students have its unique rollno and name, so instance data member is good in such case. Here, "college" refers to the common property of all objects. If we make it static, this field will get the memory only once. ▰ Java static property is shared to all objects. @Dr. Arpana Chaturvedi
  • 24. Unit-II Example of Static Variable @Dr. Arpana Chaturvedi
  • 25. Unit-II JAVA Static Variable @Dr. Arpana Chaturvedi
  • 26. Unit-II Program of the counter without static variable We have created an instance variable named count which is incremented in the constructor. Since instance variable gets the memory at the time of object creation, each object will have the copy of the instance variable. If it is incremented, it won't reflect other objects. So each object will have the value 1 in the count variable. @Dr. Arpana Chaturvedi
  • 27. Unit-II Program of counter by Static Variable @Dr. Arpana Chaturvedi Static variable will get the memory only once, if any object changes the value of the static variable, it will retain its value.
  • 28. Unit-II JAVA Static Method If you apply static keyword with any method, it is known as static method. • A static method belongs to the class rather than the object of a class. • A static method can be invoked without the need for creating an instance of a class. • A static method can access static data member and can change the value of it. @Dr. Arpana Chaturvedi
  • 29. Unit-I Client Side Technologies- Web 2.0 @Dr. Arpana Chaturvedi
  • 30. Unit-II Another example of a static method that performs a normal calculation @Dr. Arpana Chaturvedi
  • 31. Unit-II Restrictions for the static method ▰ There are two main restrictions for the static method. They are: ▰ The static method can not use non static data member or call non-static method directly. ▰ this and super cannot be used in static context. @Dr. Arpana Chaturvedi Why is the Java main method static? Ans) It is because the object is not required to call a static method. If it were a non-static method, JVM creates an object first then call main() method that will lead the problem of extra memory allocation.
  • 32. Unit-II JAVA Static Block ▰ Is used to initialize the static data member. ▰ It is executed before the main method at the time of classloading. @Dr. Arpana Chaturvedi
  • 33. Unit-I Program without main Function ▰ Can we execute a program without main() method? ▰ Ans) No, one of the ways was the static block, but it was possible till JDK 1.6. Since JDK 1.7, it is not possible to execute a Java class without the main method. @Dr. Arpana Chaturvedi
  • 34. Unit-II Understanding final The final keyword in java is used to restrict the user. The java final keyword can be used in many context. Final can be: ▰ variable ▰ method ▰ class The final keyword can be applied with the variables, a final variable that have no value it is called blank final variable or uninitialized final variable. It can be initialized in the constructor only. The blank final variable can be static also which will be initialized in the static block only. We will have detailed learning of these. @Dr. Arpana Chaturvedi
  • 35. Unit-II JAVA Final Variable If you make any variable as final, you cannot change the value of final variable(It will be constant). There is a final variable speed limit, we are going to change the value of this variable, but It can't be changed because final variable once assigned a value can never be changed. @Dr. Arpana Chaturvedi
  • 36. Unit-II Java final method If you make any method as final, you cannot override it. @Dr. Arpana Chaturvedi
  • 37. Unit-II JAVA Final Class If you make any class as final, you cannot extend it. @Dr. Arpana Chaturvedi
  • 38. Unit-II final method ▰ Is final method inherited? ▰ Yes, final method is inherited but you cannot override it. For Example: @Dr. Arpana Chaturvedi