SlideShare uma empresa Scribd logo
1 de 14
Baixar para ler offline
Java Methods
New Wave Analytica
We Find Solutions in Data
Outline
▪ Class Instance Methods
▪ Static Methods
▪ Methods with no Parameters
▪ Methods with Parameters
▪ Methods with Return Value
▪ Method Overloading
▪ Method Overriding
New Wave Analytica
Class Instance Methods
▪ Instance methods require an object of its class to be created
before it can be called.
▪ To invoke an instance method, create an object of the class
within which it is defined.
New Wave Analytica
Class Instance Methods
▪ Instance methods require an object of its class to be created before it can
be called.
▪ To invoke an instance method, create an object of the class within which it
is defined.
▪ Instance methods belong to the object of the class not to the class, they
can be called after creating the object of the class.
▪ Every individual object create from the class has its own copy of the
instance methods of that class.
▪ Every individual Object created from the class has its own copy of the
instance method(s) of that class.
New Wave Analytica
Example
public class Student {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public class StudentRunner {
public static void main(String[] args) {
Student student = new Student();
student.setName("Me");
student.getName();
}
}
New Wave Analytica
Static Method
▪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 or reference to the object of
that class.
▪ Static methods are associated to the class in which they reside i.e. they can
be called even without creating an instance of the class, i.e
ClassName.methodName(args).
▪ They are designed with aim to be shared among all objects created from
the same class.
New Wave Analytica
Example
class Student{
public static String studentName = "";
public static void getName(String name){
studentName = name;
}
}
public class StudentRunner {
public static void main (String[] args) {
// Accessing the static method getName() and
// field by class name itself.
Student.getName("Me");
System.out.println(Student.studentName);
// Accessing the static method getName() by using Object's
reference.
Student student = new Student();
student.getName("You");
System.out.println(student.studentName);
}
}
New Wave Analytica
Method with no Parameter
▪ Method that does not accept any arguments.
public class MathDemo {
void areaOfcircle() {
System.out.print("enter the radius :");
Scanner s = new Scanner(System.in);
float r = s.nextFloat();
float ar;
ar = (r * r) * 22 / 7;
System.out.println("area of the circle is :
"+ar+" sq units.");
} }
public class MathDemoRunner {
public static void main(String[] args) {
MathDemo obj = new MathDemo();
obj.areaOfCircle();
} }
New Wave Analytica
Method with Parameters
▪ Method that accepts arguments.
public class MathDemo {
void areaOfCircle(float r, float ar)
{
ar = (r * r) * 22 / 7;
System.out.println("area of the circle is : "+ar+"
sq units.");
}
}
public static void main(String[] args) {
MathDemo obj = new MathDemo();
obj.areaOfCircle(12, 12);
}
New Wave Analytica
Methods with Return Value
▪ Java requires that a method declare the data type of the value that it returns.
▪ If a method does not return a value, it must be declared to return void.
public class MathDemo {
static int myMethod(int x) {
return 5 + x;
}
public static void main(String[] args) {
System.out.println(myMethod(3));
}
}
New Wave Analytica
Method Overloading
▪ Method overloading
▪ allows a method with the same name but different parameters, to have different
implementations and return values of different types
▪ can be used when the same operation has different implementations.
▪ Overloaded methods have the following properties:
▪ the same name
▪ different parameters
▪ return types can be different or the same
New Wave Analytica
Method Overloading
public class MathDemo {
// Overloaded sum().
// This sum takes two int parameters
public int sum(int x, int y) {
return (x + y);
}
// Overloaded sum(). This sum takes three int
parameters
public int sum(int x, int y, int z) {
return (x + y + z);
}
// Overloaded sum(). This sum takes two double
parameters
public double sum(double x, double y) {
return (x + y); }
// Driver code
public static void main(String args[]) {
MathDemo s = new MathDemo();
System.out.println(s.sum(10, 20));
System.out.println(s.sum(10, 20, 30));
System.out.println(s.sum(10.5, 20.5));
}
}
New Wave Analytica
Method Overriding
▪ Method Overriding in Java is a condition when a subclass has the same method as
declared in the parent class.
▪ A parent class can be called an overridden method.
▪ In object-oriented programming, the feature of overriding is used to provide a class,
subclass or a child class to use a method that is already used by parent class to have a
specific implementation.
▪ Method overriding in Java programming occurs when the method in the subclass has the
same return type, or parameters, name or signature as the parent class.
▪ Method overriding is the method by which Java can support runtime polymorphism.
▪ Basically, the method to execute is chosen on the basis of the type of object and not on
the type of reference variable.
New Wave Analytica
Method Overriding
public class Parent {
void methodOfParentClass() {
System.out.println("Parent's method()");
} }
public class Child extends Parent {
@Override
void methodOfParentClass() {
System.out.println("Child's method()");
} }
public class MethodOverriding {
public static void main(String[] args) {
Parent obj1 = new Parent();
obj1.methodOfParentClass();
Parent obj2 = new Child();
obj2.methodOfParentClass();
}
}
New Wave Analytica

Mais conteúdo relacionado

Mais procurados

Chapter 02: Classes Objects and Methods Java by Tushar B Kute
Chapter 02: Classes Objects and Methods Java by Tushar B KuteChapter 02: Classes Objects and Methods Java by Tushar B Kute
Chapter 02: Classes Objects and Methods Java by Tushar B KuteTushar B Kute
 
Lect 1-java object-classes
Lect 1-java object-classesLect 1-java object-classes
Lect 1-java object-classesFajar Baskoro
 
Class and Objects in Java
Class and Objects in JavaClass and Objects in Java
Class and Objects in JavaSpotle.ai
 
Pi j3.2 polymorphism
Pi j3.2 polymorphismPi j3.2 polymorphism
Pi j3.2 polymorphismmcollison
 
Java lec class, objects and constructors
Java lec class, objects and constructorsJava lec class, objects and constructors
Java lec class, objects and constructorsJan Niño Acierto
 
Classes, objects in JAVA
Classes, objects in JAVAClasses, objects in JAVA
Classes, objects in JAVAAbhilash Nair
 
11 Using classes and objects
11 Using classes and objects11 Using classes and objects
11 Using classes and objectsmaznabili
 
Logic and Coding of Java Interfaces & Swing Applications
Logic and Coding of Java Interfaces & Swing ApplicationsLogic and Coding of Java Interfaces & Swing Applications
Logic and Coding of Java Interfaces & Swing Applicationskjkleindorfer
 
Introduction to class in java
Introduction to class in javaIntroduction to class in java
Introduction to class in javakamal kotecha
 
Class and object in C++
Class and object in C++Class and object in C++
Class and object in C++rprajat007
 
9781439035665 ppt ch08
9781439035665 ppt ch089781439035665 ppt ch08
9781439035665 ppt ch08Terry Yoast
 
Python Class | Python Programming | Python Tutorial | Edureka
Python Class | Python Programming | Python Tutorial | EdurekaPython Class | Python Programming | Python Tutorial | Edureka
Python Class | Python Programming | Python Tutorial | EdurekaEdureka!
 
Object and Classes in Java
Object and Classes in JavaObject and Classes in Java
Object and Classes in Javabackdoor
 
Week10 packages using objects in objects
Week10 packages using objects in objectsWeek10 packages using objects in objects
Week10 packages using objects in objectskjkleindorfer
 

Mais procurados (20)

Class or Object
Class or ObjectClass or Object
Class or Object
 
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
Chapter 02: Classes Objects and Methods Java by Tushar B KuteChapter 02: Classes Objects and Methods Java by Tushar B Kute
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
 
Lect 1-java object-classes
Lect 1-java object-classesLect 1-java object-classes
Lect 1-java object-classes
 
Class and Objects in Java
Class and Objects in JavaClass and Objects in Java
Class and Objects in Java
 
Class introduction in java
Class introduction in javaClass introduction in java
Class introduction in java
 
Pi j3.2 polymorphism
Pi j3.2 polymorphismPi j3.2 polymorphism
Pi j3.2 polymorphism
 
6. static keyword
6. static keyword6. static keyword
6. static keyword
 
Java lec class, objects and constructors
Java lec class, objects and constructorsJava lec class, objects and constructors
Java lec class, objects and constructors
 
Classes, objects in JAVA
Classes, objects in JAVAClasses, objects in JAVA
Classes, objects in JAVA
 
Classes&objects
Classes&objectsClasses&objects
Classes&objects
 
CLASS & OBJECT IN JAVA
CLASS & OBJECT  IN JAVACLASS & OBJECT  IN JAVA
CLASS & OBJECT IN JAVA
 
11 Using classes and objects
11 Using classes and objects11 Using classes and objects
11 Using classes and objects
 
Logic and Coding of Java Interfaces & Swing Applications
Logic and Coding of Java Interfaces & Swing ApplicationsLogic and Coding of Java Interfaces & Swing Applications
Logic and Coding of Java Interfaces & Swing Applications
 
Introduction to class in java
Introduction to class in javaIntroduction to class in java
Introduction to class in java
 
Class and object in C++
Class and object in C++Class and object in C++
Class and object in C++
 
9781439035665 ppt ch08
9781439035665 ppt ch089781439035665 ppt ch08
9781439035665 ppt ch08
 
Python Class | Python Programming | Python Tutorial | Edureka
Python Class | Python Programming | Python Tutorial | EdurekaPython Class | Python Programming | Python Tutorial | Edureka
Python Class | Python Programming | Python Tutorial | Edureka
 
Object and Classes in Java
Object and Classes in JavaObject and Classes in Java
Object and Classes in Java
 
Week10 packages using objects in objects
Week10 packages using objects in objectsWeek10 packages using objects in objects
Week10 packages using objects in objects
 
Core java complete notes - Contact at +91-814-614-5674
Core java complete notes - Contact at +91-814-614-5674Core java complete notes - Contact at +91-814-614-5674
Core java complete notes - Contact at +91-814-614-5674
 

Semelhante a Java Methods

Semelhante a Java Methods (20)

OOPs & Inheritance Notes
OOPs & Inheritance NotesOOPs & Inheritance Notes
OOPs & Inheritance Notes
 
Java Method, Static Block
Java Method, Static BlockJava Method, Static Block
Java Method, Static Block
 
C h 04 oop_inheritance
C h 04 oop_inheritanceC h 04 oop_inheritance
C h 04 oop_inheritance
 
Lecture 4_Java Method-constructor_imp_keywords
Lecture   4_Java Method-constructor_imp_keywordsLecture   4_Java Method-constructor_imp_keywords
Lecture 4_Java Method-constructor_imp_keywords
 
Defining classes-and-objects-1.0
Defining classes-and-objects-1.0Defining classes-and-objects-1.0
Defining classes-and-objects-1.0
 
Explain Classes and methods in java (ch04).ppt
Explain Classes and methods in java (ch04).pptExplain Classes and methods in java (ch04).ppt
Explain Classes and methods in java (ch04).ppt
 
Java OO Revisited
Java OO RevisitedJava OO Revisited
Java OO Revisited
 
Assignment 7
Assignment 7Assignment 7
Assignment 7
 
Java Basic day-2
Java Basic day-2Java Basic day-2
Java Basic day-2
 
Core java concepts
Core java  conceptsCore java  concepts
Core java concepts
 
Object oriented concepts
Object oriented conceptsObject oriented concepts
Object oriented concepts
 
Second chapter-java
Second chapter-javaSecond chapter-java
Second chapter-java
 
Computer programming 2 Lesson 15
Computer programming 2  Lesson 15Computer programming 2  Lesson 15
Computer programming 2 Lesson 15
 
Corejava Training in Bangalore Tutorial
Corejava Training in Bangalore TutorialCorejava Training in Bangalore Tutorial
Corejava Training in Bangalore Tutorial
 
Core Java
Core JavaCore Java
Core Java
 
Best Core Java Training In Bangalore
Best Core Java Training In BangaloreBest Core Java Training In Bangalore
Best Core Java Training In Bangalore
 
Java Concepts
Java ConceptsJava Concepts
Java Concepts
 
itft-Inheritance in java
itft-Inheritance in javaitft-Inheritance in java
itft-Inheritance in java
 
Lecture 6.pptx
Lecture 6.pptxLecture 6.pptx
Lecture 6.pptx
 
Static variable
Static  variableStatic  variable
Static variable
 

Último

The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfThe Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfkalichargn70th171
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...soniya singh
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideChristina Lin
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEOrtus Solutions, Corp
 
Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...aditisharan08
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio, Inc.
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)OPEN KNOWLEDGE GmbH
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...OnePlan Solutions
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfjoe51371421
 
Asset Management Software - Infographic
Asset Management Software - InfographicAsset Management Software - Infographic
Asset Management Software - InfographicHr365.us smith
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...ICS
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantAxelRicardoTrocheRiq
 
chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptkotipi9215
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...MyIntelliSource, Inc.
 
What is Binary Language? Computer Number Systems
What is Binary Language?  Computer Number SystemsWhat is Binary Language?  Computer Number Systems
What is Binary Language? Computer Number SystemsJheuzeDellosa
 
Project Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationProject Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationkaushalgiri8080
 
Introduction to Decentralized Applications (dApps)
Introduction to Decentralized Applications (dApps)Introduction to Decentralized Applications (dApps)
Introduction to Decentralized Applications (dApps)Intelisync
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...gurkirankumar98700
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Modelsaagamshah0812
 

Último (20)

The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfThe Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
 
Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdf
 
Asset Management Software - Infographic
Asset Management Software - InfographicAsset Management Software - Infographic
Asset Management Software - Infographic
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service Consultant
 
chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.ppt
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
 
Exploring iOS App Development: Simplifying the Process
Exploring iOS App Development: Simplifying the ProcessExploring iOS App Development: Simplifying the Process
Exploring iOS App Development: Simplifying the Process
 
What is Binary Language? Computer Number Systems
What is Binary Language?  Computer Number SystemsWhat is Binary Language?  Computer Number Systems
What is Binary Language? Computer Number Systems
 
Project Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationProject Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanation
 
Introduction to Decentralized Applications (dApps)
Introduction to Decentralized Applications (dApps)Introduction to Decentralized Applications (dApps)
Introduction to Decentralized Applications (dApps)
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 

Java Methods

  • 1. Java Methods New Wave Analytica We Find Solutions in Data
  • 2. Outline ▪ Class Instance Methods ▪ Static Methods ▪ Methods with no Parameters ▪ Methods with Parameters ▪ Methods with Return Value ▪ Method Overloading ▪ Method Overriding New Wave Analytica
  • 3. Class Instance Methods ▪ Instance methods require an object of its class to be created before it can be called. ▪ To invoke an instance method, create an object of the class within which it is defined. New Wave Analytica
  • 4. Class Instance Methods ▪ Instance methods require an object of its class to be created before it can be called. ▪ To invoke an instance method, create an object of the class within which it is defined. ▪ Instance methods belong to the object of the class not to the class, they can be called after creating the object of the class. ▪ Every individual object create from the class has its own copy of the instance methods of that class. ▪ Every individual Object created from the class has its own copy of the instance method(s) of that class. New Wave Analytica
  • 5. Example public class Student { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } } public class StudentRunner { public static void main(String[] args) { Student student = new Student(); student.setName("Me"); student.getName(); } } New Wave Analytica
  • 6. Static Method ▪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 or reference to the object of that class. ▪ Static methods are associated to the class in which they reside i.e. they can be called even without creating an instance of the class, i.e ClassName.methodName(args). ▪ They are designed with aim to be shared among all objects created from the same class. New Wave Analytica
  • 7. Example class Student{ public static String studentName = ""; public static void getName(String name){ studentName = name; } } public class StudentRunner { public static void main (String[] args) { // Accessing the static method getName() and // field by class name itself. Student.getName("Me"); System.out.println(Student.studentName); // Accessing the static method getName() by using Object's reference. Student student = new Student(); student.getName("You"); System.out.println(student.studentName); } } New Wave Analytica
  • 8. Method with no Parameter ▪ Method that does not accept any arguments. public class MathDemo { void areaOfcircle() { System.out.print("enter the radius :"); Scanner s = new Scanner(System.in); float r = s.nextFloat(); float ar; ar = (r * r) * 22 / 7; System.out.println("area of the circle is : "+ar+" sq units."); } } public class MathDemoRunner { public static void main(String[] args) { MathDemo obj = new MathDemo(); obj.areaOfCircle(); } } New Wave Analytica
  • 9. Method with Parameters ▪ Method that accepts arguments. public class MathDemo { void areaOfCircle(float r, float ar) { ar = (r * r) * 22 / 7; System.out.println("area of the circle is : "+ar+" sq units."); } } public static void main(String[] args) { MathDemo obj = new MathDemo(); obj.areaOfCircle(12, 12); } New Wave Analytica
  • 10. Methods with Return Value ▪ Java requires that a method declare the data type of the value that it returns. ▪ If a method does not return a value, it must be declared to return void. public class MathDemo { static int myMethod(int x) { return 5 + x; } public static void main(String[] args) { System.out.println(myMethod(3)); } } New Wave Analytica
  • 11. Method Overloading ▪ Method overloading ▪ allows a method with the same name but different parameters, to have different implementations and return values of different types ▪ can be used when the same operation has different implementations. ▪ Overloaded methods have the following properties: ▪ the same name ▪ different parameters ▪ return types can be different or the same New Wave Analytica
  • 12. Method Overloading public class MathDemo { // Overloaded sum(). // This sum takes two int parameters public int sum(int x, int y) { return (x + y); } // Overloaded sum(). This sum takes three int parameters public int sum(int x, int y, int z) { return (x + y + z); } // Overloaded sum(). This sum takes two double parameters public double sum(double x, double y) { return (x + y); } // Driver code public static void main(String args[]) { MathDemo s = new MathDemo(); System.out.println(s.sum(10, 20)); System.out.println(s.sum(10, 20, 30)); System.out.println(s.sum(10.5, 20.5)); } } New Wave Analytica
  • 13. Method Overriding ▪ Method Overriding in Java is a condition when a subclass has the same method as declared in the parent class. ▪ A parent class can be called an overridden method. ▪ In object-oriented programming, the feature of overriding is used to provide a class, subclass or a child class to use a method that is already used by parent class to have a specific implementation. ▪ Method overriding in Java programming occurs when the method in the subclass has the same return type, or parameters, name or signature as the parent class. ▪ Method overriding is the method by which Java can support runtime polymorphism. ▪ Basically, the method to execute is chosen on the basis of the type of object and not on the type of reference variable. New Wave Analytica
  • 14. Method Overriding public class Parent { void methodOfParentClass() { System.out.println("Parent's method()"); } } public class Child extends Parent { @Override void methodOfParentClass() { System.out.println("Child's method()"); } } public class MethodOverriding { public static void main(String[] args) { Parent obj1 = new Parent(); obj1.methodOfParentClass(); Parent obj2 = new Child(); obj2.methodOfParentClass(); } } New Wave Analytica