Object and Class 2
Object and Class 2
Object and Class 2
COMPUTER
PROGRAMMING 2
Prepared By:
Static Methods
https://www.youtube.com/watch?v=
1rdbEUh3R_o
class MethodOverloading {
public static void main(String args[]){
System.out.println("Result: " + aMethod(13, 2.0));
}
public static double aMethod (int a, int b){
return a*b;
}
public static double aMethod (int a, double b){
return a+b;
}
public static double aMethod (double a, int b){
return a-b;
}
public static double aMethod (double a, double b){
return a/b;
}}
public class UberLoading {
public static void main (String []args) {
me.display('a');
me.display(10, 'a');
me.display(5);
}
}
public class UberLoading2 {
public void display (int a) {
System.out.println("Printed: " + a);
}
public void display(char c) {
System.out.println("Printed: " + c);
}
public void display (int a, char c) {
System.out.println("Both Printed: " + a + " " + c);
}
}
DEMONSTRATION
Method Overloading
https://www.youtube.com/watch
?v=_iz6cjXlJfw
A constructor in Java is a special
method that is used to initialize
objects. The constructor is called
when an object of a class is created.
It can be used to set initial values for
object attributes.
public class MyClass2 {
int x;
public MyClass2() {
x = 5;
}
public static void main(String[] args) {
MyClass2 myObj = new MyClass2();
System.out.println(myObj.x);
}
}
Constructors can also take parameters,
which is used to initialize attributes.
This Keyword
https://www.youtube.com/watch
?v=csjfLTt6-io
That ends our Week 5 Session 2!
NEXT MEETING--
WEEK 6 SESSION 1:
INHERITANCE, POLYMORPHISM,
AND ENCAPSULATION
REFERENCES: