object-oriented programmingmethod overloading method overloading is one of the ways that java...
TRANSCRIPT
![Page 1: OBJECT-ORIENTED PROGRAMMINGMethod Overloading Method overloading is one of the ways that Java supports polymorphism. Defining two or more methods with the same name in a class is called](https://reader035.vdocuments.us/reader035/viewer/2022081402/5f0be1017e708231d432a9f9/html5/thumbnails/1.jpg)
OBJECT-ORIENTED
PROGRAMMINGLECTURE # 18-22
![Page 2: OBJECT-ORIENTED PROGRAMMINGMethod Overloading Method overloading is one of the ways that Java supports polymorphism. Defining two or more methods with the same name in a class is called](https://reader035.vdocuments.us/reader035/viewer/2022081402/5f0be1017e708231d432a9f9/html5/thumbnails/2.jpg)
Content
Method Overloading
Constructor Overloading
Encapsulation and Access Specifiers
static Keyword
static block
Introducing Math class
final keyword
Nested and Inner Classes
![Page 3: OBJECT-ORIENTED PROGRAMMINGMethod Overloading Method overloading is one of the ways that Java supports polymorphism. Defining two or more methods with the same name in a class is called](https://reader035.vdocuments.us/reader035/viewer/2022081402/5f0be1017e708231d432a9f9/html5/thumbnails/3.jpg)
Method Overloading
Method overloading is one of the ways that Java supports
polymorphism.
Defining two or more methods with the same name in a class is
called method overloading.
The compiler is able to distinguish between the methods
because of their method signatures.
The name of a method together with the types and sequence
of the parameters form the signature of the method.
![Page 4: OBJECT-ORIENTED PROGRAMMINGMethod Overloading Method overloading is one of the ways that Java supports polymorphism. Defining two or more methods with the same name in a class is called](https://reader035.vdocuments.us/reader035/viewer/2022081402/5f0be1017e708231d432a9f9/html5/thumbnails/4.jpg)
Method Overloading
Examples:
System.out.println(“hello”);
System.out.println(23);
System.out.println(58.25);
System.out.println(‘a’);
![Page 5: OBJECT-ORIENTED PROGRAMMINGMethod Overloading Method overloading is one of the ways that Java supports polymorphism. Defining two or more methods with the same name in a class is called](https://reader035.vdocuments.us/reader035/viewer/2022081402/5f0be1017e708231d432a9f9/html5/thumbnails/5.jpg)
Method Overloading
class OverloadDemo {
void test() { System.out.println("No parameters");
}
// Overload test for one integer parameter. void test(int a) { System.out.println("a: " + a);
}
// Overload test for two integer parameters. void test(int a, int b) { System.out.println("a and b: " + a + " " + b);
}
// overload test for a double parameter double test(double a) { System.out.println("double a: " + a);
return a*a; }
}
![Page 6: OBJECT-ORIENTED PROGRAMMINGMethod Overloading Method overloading is one of the ways that Java supports polymorphism. Defining two or more methods with the same name in a class is called](https://reader035.vdocuments.us/reader035/viewer/2022081402/5f0be1017e708231d432a9f9/html5/thumbnails/6.jpg)
Method Overloading
class Overload {
public static void main(String args[]) {
OverloadDemo ob = new OverloadDemo(); double result; ob.test();
ob.test(10);
ob.test(10, 20); result = ob.test(123.2);
System.out.println("Result of ob.test(123.2): " + result);
}
}
![Page 7: OBJECT-ORIENTED PROGRAMMINGMethod Overloading Method overloading is one of the ways that Java supports polymorphism. Defining two or more methods with the same name in a class is called](https://reader035.vdocuments.us/reader035/viewer/2022081402/5f0be1017e708231d432a9f9/html5/thumbnails/7.jpg)
Constructor Overloading
Constructors are methods that can be overloaded, just like
any other method in a class.
When you want to generate objects of a class from different
sets of initial defining data.
![Page 8: OBJECT-ORIENTED PROGRAMMINGMethod Overloading Method overloading is one of the ways that Java supports polymorphism. Defining two or more methods with the same name in a class is called](https://reader035.vdocuments.us/reader035/viewer/2022081402/5f0be1017e708231d432a9f9/html5/thumbnails/8.jpg)
Constructor Overloading
class Student { String name;String rollno;int age;
Student(){System.out.println(“no information provided”);name = “”;rollno = “”; age = 0;}
Student(String n, String r, int a){name = n;rollno = r; age = a;}
void getStudentInfo(){System.out.println(“name = ”+name+”\n roll no. = rollno+”\n age =
”+age);
}}
![Page 9: OBJECT-ORIENTED PROGRAMMINGMethod Overloading Method overloading is one of the ways that Java supports polymorphism. Defining two or more methods with the same name in a class is called](https://reader035.vdocuments.us/reader035/viewer/2022081402/5f0be1017e708231d432a9f9/html5/thumbnails/9.jpg)
Constructor Overloading
class UseStudentClass{
public static void main(){
Student std1 = new Student();Student std2 = new Student(“Al”, “19SW09”, 21);
std1.getStudentInfo();std2.getStudentsInfo();
}}
![Page 10: OBJECT-ORIENTED PROGRAMMINGMethod Overloading Method overloading is one of the ways that Java supports polymorphism. Defining two or more methods with the same name in a class is called](https://reader035.vdocuments.us/reader035/viewer/2022081402/5f0be1017e708231d432a9f9/html5/thumbnails/10.jpg)
Encapsulation and Access Specifiers
Java’s access specifiers are public, private, and protected.
Java also defines a default access level.
Encapsulation, you can control what parts of a program can
access the members of a class.
By controlling access, you can prevent misuse.
![Page 11: OBJECT-ORIENTED PROGRAMMINGMethod Overloading Method overloading is one of the ways that Java supports polymorphism. Defining two or more methods with the same name in a class is called](https://reader035.vdocuments.us/reader035/viewer/2022081402/5f0be1017e708231d432a9f9/html5/thumbnails/11.jpg)
Encapsulation and Access Specifiers
Java provides the following access specifiers:
![Page 12: OBJECT-ORIENTED PROGRAMMINGMethod Overloading Method overloading is one of the ways that Java supports polymorphism. Defining two or more methods with the same name in a class is called](https://reader035.vdocuments.us/reader035/viewer/2022081402/5f0be1017e708231d432a9f9/html5/thumbnails/12.jpg)
Encapsulation and Access Specifiers
class Test {
int a; // default accesspublic int b; // public access
private int c;
void setc(int i) { c = i;
}
int getc() { return c;
}
}
![Page 13: OBJECT-ORIENTED PROGRAMMINGMethod Overloading Method overloading is one of the ways that Java supports polymorphism. Defining two or more methods with the same name in a class is called](https://reader035.vdocuments.us/reader035/viewer/2022081402/5f0be1017e708231d432a9f9/html5/thumbnails/13.jpg)
Encapsulation and Access Specifiers
class AccessTest {
public static void main(String args[]) {
Test ob = new Test();ob.a = 10;ob.b = 20;
// ob.c = 100; // Error!
ob.setc(100); // OKSystem.out.println("a, b, and c: " + ob.a + " " +ob.b + " " + ob.getc());
}
}
![Page 14: OBJECT-ORIENTED PROGRAMMINGMethod Overloading Method overloading is one of the ways that Java supports polymorphism. Defining two or more methods with the same name in a class is called](https://reader035.vdocuments.us/reader035/viewer/2022081402/5f0be1017e708231d432a9f9/html5/thumbnails/14.jpg)
static Keyword
A static class member can be accessed without creating an
object.
You can declare both methods and variables to be static.
main() is a common example.
![Page 15: OBJECT-ORIENTED PROGRAMMINGMethod Overloading Method overloading is one of the ways that Java supports polymorphism. Defining two or more methods with the same name in a class is called](https://reader035.vdocuments.us/reader035/viewer/2022081402/5f0be1017e708231d432a9f9/html5/thumbnails/15.jpg)
static Keyword
Methods declared as static have several restrictions:
They can only call other static methods.
They must only access static data.
They cannot refer to this or super in any way.
![Page 16: OBJECT-ORIENTED PROGRAMMINGMethod Overloading Method overloading is one of the ways that Java supports polymorphism. Defining two or more methods with the same name in a class is called](https://reader035.vdocuments.us/reader035/viewer/2022081402/5f0be1017e708231d432a9f9/html5/thumbnails/16.jpg)
static Keyword
class StaticDemo {
static String name;static int age;
static void getInfo(){System.out.println(“name ”+name);System.out.println(“age ”+age);}
}class UseStaticDemo{public static void main(String args[]){
StaticDemo.getInfo();
}}
![Page 17: OBJECT-ORIENTED PROGRAMMINGMethod Overloading Method overloading is one of the ways that Java supports polymorphism. Defining two or more methods with the same name in a class is called](https://reader035.vdocuments.us/reader035/viewer/2022081402/5f0be1017e708231d432a9f9/html5/thumbnails/17.jpg)
static block
Static blocks are also called Static initialization
blocks .
A static initialization block is a normal block of code
enclosed in braces, { }, and preceded by the static
keyword.
static{
//whatever code is needed for initialization goes here
}
![Page 18: OBJECT-ORIENTED PROGRAMMINGMethod Overloading Method overloading is one of the ways that Java supports polymorphism. Defining two or more methods with the same name in a class is called](https://reader035.vdocuments.us/reader035/viewer/2022081402/5f0be1017e708231d432a9f9/html5/thumbnails/18.jpg)
Introducing Math Class
The java.lang.Math class contains methods for performing basic
numeric operations such as the elementary exponential, logarithm,
square root, and trigonometric functions.
Access these functions without making the object as they are all
declared static.
E.g. Math.method(arg)
![Page 19: OBJECT-ORIENTED PROGRAMMINGMethod Overloading Method overloading is one of the ways that Java supports polymorphism. Defining two or more methods with the same name in a class is called](https://reader035.vdocuments.us/reader035/viewer/2022081402/5f0be1017e708231d432a9f9/html5/thumbnails/19.jpg)
Code it now!
Consider a right-angle triangle like this one. Write a
java class with three instance variables hypotenuse,
perpendicular and base. Create two overloaded
constructor for initializing these lengths including a
default constructor that gives default values.
Create three functions,
getHyp(double , double) returns hyp after calculation,
getPerp(double, double) returns perp after calculation,
getBase(double, double) returns base after calculation.
![Page 20: OBJECT-ORIENTED PROGRAMMINGMethod Overloading Method overloading is one of the ways that Java supports polymorphism. Defining two or more methods with the same name in a class is called](https://reader035.vdocuments.us/reader035/viewer/2022081402/5f0be1017e708231d432a9f9/html5/thumbnails/20.jpg)
Code it now!
Overload the functions in the previous slides:
getHyp(double length, double angle)
getBase(double length, double angle)
getPerp(double length, double angle)
Each function must return the side after taking
length and angle
![Page 21: OBJECT-ORIENTED PROGRAMMINGMethod Overloading Method overloading is one of the ways that Java supports polymorphism. Defining two or more methods with the same name in a class is called](https://reader035.vdocuments.us/reader035/viewer/2022081402/5f0be1017e708231d432a9f9/html5/thumbnails/21.jpg)
final Keyword
Doing so prevents its contents from being modified.
This means that you must initialize a final variable
when it is declared.
It is a common coding convention to choose all
uppercase identifiers for final variables
final double PI = 3.142;
![Page 22: OBJECT-ORIENTED PROGRAMMINGMethod Overloading Method overloading is one of the ways that Java supports polymorphism. Defining two or more methods with the same name in a class is called](https://reader035.vdocuments.us/reader035/viewer/2022081402/5f0be1017e708231d432a9f9/html5/thumbnails/22.jpg)
Nested and Inner Classes
You can put the definition of one class inside the definition of another class.
The inside class is called a nested class.
Syntax:
public class Outside {
// Nested class
public class Inside {
// Details of Inside class...
}
// More members of Outside class...
}
![Page 23: OBJECT-ORIENTED PROGRAMMINGMethod Overloading Method overloading is one of the ways that Java supports polymorphism. Defining two or more methods with the same name in a class is called](https://reader035.vdocuments.us/reader035/viewer/2022081402/5f0be1017e708231d432a9f9/html5/thumbnails/23.jpg)
Nested and Inner Classes
A top-level class is a class that contains a nested
class but is not itself a nested class.
![Page 24: OBJECT-ORIENTED PROGRAMMINGMethod Overloading Method overloading is one of the ways that Java supports polymorphism. Defining two or more methods with the same name in a class is called](https://reader035.vdocuments.us/reader035/viewer/2022081402/5f0be1017e708231d432a9f9/html5/thumbnails/24.jpg)
Nested and Inner Classes
class Outer {int outer_x = 100;void test() {Inner inner = new Inner();inner.display();}// this is an inner classclass Inner {void display() {System.out.println("display: outer_x = " + outer_x);}}}class InnerClassDemo { public static void main(String args[]) {Outer outer = new Outer();outer.test();
} }