SlideShare una empresa de Scribd logo
1 de 18
Java
METHODS & ENCAPSULATION
Topics Covered:
• using methods,
• method arguments and return values
• how arguments are passed to a method
• overloading a method
• access control, encapsulation
• overloading constructor
• Nested classes, inner classes
• static classes, static methods and variables
using methods
class class_name{
return_type method_name(parameter list)
{
//body of the method
}
}
how arguments are passed to a method
• ArraysToMethod(File:ArraysToMethod)
encapsulation
• Wrapping up of data under a single unit.
• Encapsulation can be achieved by declaring all
the variables in the class as private and writing
public methods in the class to set and get the
values of variables.
• Example(File: encapsulationEx)
access control in Java
Access
Modifier
within class within
package
outside
package by
subclass only
outside
package
Private Y N N N
Default Y Y N N
Protected Y Y Y N
Public Y Y Y Y
overloading constructor
• Similar like method overloading, you can also
overload constructors if two or more
constructors are different in parameters.
Example(File:OverloadingConstructor)
• A Java constructor name must exactly match with the
class name (including case).
• A Java constructor must not have a return type.
• If a class doesn't have a constructor, Java compiler
automatically creates a default constructor during
run-time. The default constructor initialize instance
variables with default values. For
example: int variable will be initialized to 0
• Constructors cannot be abstract or static or final.
• Constructor but can not be overridden. can be
overloaded
Constructor
Types:
– No-Arg Constructor - a constructor that does not
accept any arguments
– Default Constructor - a constructor that is
automatically created by the Java compiler if it is
not explicitly defined.
– Parameterized constructor - used to specify
specific values of variables in object
Can a Constructor return any value ?
• The answer is the Constructor cannot
return any explicit value but implicitly it will
be returning the instance of the class.
• Access Modifier:- Java provides both Access Specifier and Access
Modifiers for creating access to your Java code for other classes. Here
modifier is also used to do the same task but there are limitations.
• Class Modifier:
– abstract :- This defines the restriction such that objects cannot be created.
– final:- This restricts a class from being inherited.
– strictfp:- it is related to the checking of floating point values irrespective of OS.
• Variable Modifier:
– static:no object creation required
– final: cannot be reassigned
– transient: it is not serialized
– volatile: the values are liable for change
Nested classes, inner classes
Define a class within another class. Such a class
is called a nested class or inner class.
class OuterClass
{
class NestedClass {
}
}
Nested classes are divided into two categories:
• static nested class : Nested classes that are
declared static are called static nested classes.
• non-static nested class : An inner class is a
non-static nested class.
• Static nestedClass
Example(File:staticNestedClassEx)
• Non-Static or inner Class
Example(File:innerClassEx)
instantiate an inner class
• To instantiate an inner class, you must first
instantiate the outer class. Then, create the
inner object within the outer object with this
syntax:
OuterClass outerObject=new OuterClass();
OuterClass.InnerClass innerObject =
outerObject.new InnerClass();
static methods
• Static methods are the methods in Java that
can be called without creating an object of
class. They are referenced by the class name
itself .
ClassName.methodName(args)
Example(File: staticMethodEx)
Instance method vs Static method
• Instance method can access the instance
methods and instance variables directly.
• Instance method can access static variables
and static methods directly.
• Static methods can access the static variables
and static methods directly.
• Static methods can’t access instance methods
and instance variables directly. They must use
reference to object.
• static method can’t use this keyword

Más contenido relacionado

La actualidad más candente

Java serialization
Java serializationJava serialization
Java serialization
Sujit Kumar
 
Mis Conceptions About The Tiny Java
Mis Conceptions About The Tiny JavaMis Conceptions About The Tiny Java
Mis Conceptions About The Tiny Java
Sultan Jokhio
 

La actualidad más candente (13)

Learn Java Part 10
Learn Java Part 10Learn Java Part 10
Learn Java Part 10
 
Local variables Instance variables Class/static variables
Local variables Instance variables Class/static variablesLocal variables Instance variables Class/static variables
Local variables Instance variables Class/static variables
 
Java serialization
Java serializationJava serialization
Java serialization
 
1. Overview of Java
1. Overview of Java1. Overview of Java
1. Overview of Java
 
[OOP - Lec 18] Static Data Member
[OOP - Lec 18] Static Data Member[OOP - Lec 18] Static Data Member
[OOP - Lec 18] Static Data Member
 
Constructor
ConstructorConstructor
Constructor
 
7. Multithreading
7. Multithreading7. Multithreading
7. Multithreading
 
INTRODUCTION TO JAVA
INTRODUCTION TO JAVAINTRODUCTION TO JAVA
INTRODUCTION TO JAVA
 
ABAP Object oriented concepts
ABAP Object oriented conceptsABAP Object oriented concepts
ABAP Object oriented concepts
 
[OOP - Lec 04,05] Basic Building Blocks of OOP
[OOP - Lec 04,05] Basic Building Blocks of OOP[OOP - Lec 04,05] Basic Building Blocks of OOP
[OOP - Lec 04,05] Basic Building Blocks of OOP
 
Mis Conceptions About The Tiny Java
Mis Conceptions About The Tiny JavaMis Conceptions About The Tiny Java
Mis Conceptions About The Tiny Java
 
Java
JavaJava
Java
 
Getting Started with Java (TCF 2014)
Getting Started with Java (TCF 2014)Getting Started with Java (TCF 2014)
Getting Started with Java (TCF 2014)
 

Similar a Unit 3

Java lec class, objects and constructors
Java lec class, objects and constructorsJava lec class, objects and constructors
Java lec class, objects and constructors
Jan Niño Acierto
 

Similar a Unit 3 (20)

Java chapter 5
Java chapter 5Java chapter 5
Java chapter 5
 
Java lec class, objects and constructors
Java lec class, objects and constructorsJava lec class, objects and constructors
Java lec class, objects and constructors
 
DAY_1.4.pptx
DAY_1.4.pptxDAY_1.4.pptx
DAY_1.4.pptx
 
PPT Lecture-1.4.pptx
PPT Lecture-1.4.pptxPPT Lecture-1.4.pptx
PPT Lecture-1.4.pptx
 
Java defining classes
Java defining classes Java defining classes
Java defining classes
 
OCP Java (OCPJP) 8 Exam Quick Reference Card
OCP Java (OCPJP) 8 Exam Quick Reference CardOCP Java (OCPJP) 8 Exam Quick Reference Card
OCP Java (OCPJP) 8 Exam Quick Reference Card
 
Core java by amit
Core java by amitCore java by amit
Core java by amit
 
Java Inheritance - sub class constructors - Method overriding
Java Inheritance - sub class constructors - Method overridingJava Inheritance - sub class constructors - Method overriding
Java Inheritance - sub class constructors - Method overriding
 
Java Access Specifier
Java Access SpecifierJava Access Specifier
Java Access Specifier
 
SystemVerilog_Classes.pdf
SystemVerilog_Classes.pdfSystemVerilog_Classes.pdf
SystemVerilog_Classes.pdf
 
Android Training (Java Review)
Android Training (Java Review)Android Training (Java Review)
Android Training (Java Review)
 
Chap1 packages
Chap1 packagesChap1 packages
Chap1 packages
 
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
 
Java
JavaJava
Java
 
Inheritance and Polymorphism
Inheritance and PolymorphismInheritance and Polymorphism
Inheritance and Polymorphism
 
Java session2
Java session2Java session2
Java session2
 
Packages and interfaces
Packages and interfacesPackages and interfaces
Packages and interfaces
 
UNIT - IIInew.pptx
UNIT - IIInew.pptxUNIT - IIInew.pptx
UNIT - IIInew.pptx
 
inheritance.pptx
inheritance.pptxinheritance.pptx
inheritance.pptx
 
Java As an OOP Language,Exception Handling & Applets
Java As an OOP Language,Exception Handling & AppletsJava As an OOP Language,Exception Handling & Applets
Java As an OOP Language,Exception Handling & Applets
 

Más de LOVELY PROFESSIONAL UNIVERSITY

Más de LOVELY PROFESSIONAL UNIVERSITY (19)

Enumerations, structure and class IN SWIFT
Enumerations, structure and class IN SWIFTEnumerations, structure and class IN SWIFT
Enumerations, structure and class IN SWIFT
 
Dictionaries IN SWIFT
Dictionaries IN SWIFTDictionaries IN SWIFT
Dictionaries IN SWIFT
 
Control structures IN SWIFT
Control structures IN SWIFTControl structures IN SWIFT
Control structures IN SWIFT
 
Arrays and its properties IN SWIFT
Arrays and its properties IN SWIFTArrays and its properties IN SWIFT
Arrays and its properties IN SWIFT
 
Array and its functionsI SWIFT
Array and its functionsI SWIFTArray and its functionsI SWIFT
Array and its functionsI SWIFT
 
practice problems on array IN SWIFT
practice problems on array IN SWIFTpractice problems on array IN SWIFT
practice problems on array IN SWIFT
 
practice problems on array IN SWIFT
practice problems on array  IN SWIFTpractice problems on array  IN SWIFT
practice problems on array IN SWIFT
 
practice problems on array IN SWIFT
practice problems on array IN SWIFTpractice problems on array IN SWIFT
practice problems on array IN SWIFT
 
practice problems on functions IN SWIFT
practice problems on functions IN SWIFTpractice problems on functions IN SWIFT
practice problems on functions IN SWIFT
 
10. funtions and closures IN SWIFT PROGRAMMING
10. funtions and closures IN SWIFT PROGRAMMING10. funtions and closures IN SWIFT PROGRAMMING
10. funtions and closures IN SWIFT PROGRAMMING
 
Variables and data types IN SWIFT
 Variables and data types IN SWIFT Variables and data types IN SWIFT
Variables and data types IN SWIFT
 
Soft skills. pptx
Soft skills. pptxSoft skills. pptx
Soft skills. pptx
 
JAVA
JAVAJAVA
JAVA
 
Unit 5
Unit 5Unit 5
Unit 5
 
Unit 4
Unit 4Unit 4
Unit 4
 
STRINGS IN JAVA
STRINGS IN JAVASTRINGS IN JAVA
STRINGS IN JAVA
 
Unit 1
Unit 1Unit 1
Unit 1
 
COMPLETE CORE JAVA
COMPLETE CORE JAVACOMPLETE CORE JAVA
COMPLETE CORE JAVA
 
Data wrangling IN R LANGUAGE
Data wrangling IN R LANGUAGEData wrangling IN R LANGUAGE
Data wrangling IN R LANGUAGE
 

Último

Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
PECB
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
fonyou31
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
heathfieldcps1
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
kauryashika82
 

Último (20)

Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writing
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdf
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 

Unit 3

  • 2. Topics Covered: • using methods, • method arguments and return values • how arguments are passed to a method • overloading a method • access control, encapsulation • overloading constructor • Nested classes, inner classes • static classes, static methods and variables
  • 3. using methods class class_name{ return_type method_name(parameter list) { //body of the method } }
  • 4. how arguments are passed to a method • ArraysToMethod(File:ArraysToMethod)
  • 5. encapsulation • Wrapping up of data under a single unit. • Encapsulation can be achieved by declaring all the variables in the class as private and writing public methods in the class to set and get the values of variables. • Example(File: encapsulationEx)
  • 6. access control in Java Access Modifier within class within package outside package by subclass only outside package Private Y N N N Default Y Y N N Protected Y Y Y N Public Y Y Y Y
  • 7. overloading constructor • Similar like method overloading, you can also overload constructors if two or more constructors are different in parameters. Example(File:OverloadingConstructor)
  • 8. • A Java constructor name must exactly match with the class name (including case). • A Java constructor must not have a return type. • If a class doesn't have a constructor, Java compiler automatically creates a default constructor during run-time. The default constructor initialize instance variables with default values. For example: int variable will be initialized to 0 • Constructors cannot be abstract or static or final. • Constructor but can not be overridden. can be overloaded
  • 9. Constructor Types: – No-Arg Constructor - a constructor that does not accept any arguments – Default Constructor - a constructor that is automatically created by the Java compiler if it is not explicitly defined. – Parameterized constructor - used to specify specific values of variables in object
  • 10. Can a Constructor return any value ? • The answer is the Constructor cannot return any explicit value but implicitly it will be returning the instance of the class.
  • 11. • Access Modifier:- Java provides both Access Specifier and Access Modifiers for creating access to your Java code for other classes. Here modifier is also used to do the same task but there are limitations. • Class Modifier: – abstract :- This defines the restriction such that objects cannot be created. – final:- This restricts a class from being inherited. – strictfp:- it is related to the checking of floating point values irrespective of OS. • Variable Modifier: – static:no object creation required – final: cannot be reassigned – transient: it is not serialized – volatile: the values are liable for change
  • 12. Nested classes, inner classes Define a class within another class. Such a class is called a nested class or inner class. class OuterClass { class NestedClass { } }
  • 13. Nested classes are divided into two categories: • static nested class : Nested classes that are declared static are called static nested classes. • non-static nested class : An inner class is a non-static nested class.
  • 14.
  • 15. • Static nestedClass Example(File:staticNestedClassEx) • Non-Static or inner Class Example(File:innerClassEx)
  • 16. instantiate an inner class • To instantiate an inner class, you must first instantiate the outer class. Then, create the inner object within the outer object with this syntax: OuterClass outerObject=new OuterClass(); OuterClass.InnerClass innerObject = outerObject.new InnerClass();
  • 17. static methods • Static methods are the methods in Java that can be called without creating an object of class. They are referenced by the class name itself . ClassName.methodName(args) Example(File: staticMethodEx)
  • 18. Instance method vs Static method • Instance method can access the instance methods and instance variables directly. • Instance method can access static variables and static methods directly. • Static methods can access the static variables and static methods directly. • Static methods can’t access instance methods and instance variables directly. They must use reference to object. • static method can’t use this keyword