Public class EqTest{ public static void main(String argv[]){ EqTest e=new EqTest(); } EqTest(){ String s=”Java”; String s2=”java”; //place test here { System.Out.Println(“Equal”); }else { System.Out.Println(“Not
Method overloading
•In Java it is possible to define two or more methods within the same class that share the same name, as long as their parameter declarations are different.
•Overloaded methods must differ in the type and/or number of their parameters
•When an overloaded method is invoked, Java uses the type and/or number of arguments as its guide to determine which version of the overloaded method to actually call.
• return type do not play any role in overload resolution
class OverloadDemo {
void test() {
System.Out.
println(“No parameters”);
}
void test(int a) { // Overload test for one integer parameter.
System.Out.Println(“a: ” + a);
}
void test(int a, int b) {
System.Out.Println(“a and b: ” + a + ” ” + b);
}
double test(double a) { // overload test for a double parameter
System.Out.Println(“double a: ” + a);
return a*a;
}}class Overload {
public static void main(String args[]) {
OverloadDemo ob = new OverloadDemo();
double result;
// call all versions of test()
ob.Test();
ob.Test(10);
ob.Test(10, 20);
result = ob.Test(123.25);
System.Out.Println(“Result is ” + result);
Using Objects as Parameters (call by reference)
// Objects may be passed to methods.
class Test {
int a, b;
Test(int i, int j) {
a = i;
b = j;
}
void meth(Test o) {
o.A=o.A *2;
o.B=o.B *2;
} }
class PassOb {
public static void main(String args[]) {
Test ob1 = new Test(100, 22);
System.Out.Println(Before call: ” + ob1.A + ” ” + ob1.B);
ob1.Meth(ob1);
System.Out.Println(After call: ” + ob1.A + ” ” + ob1.B);
}
}
Call by value
class Test {
void meth(int i, int j)
{
i *= 2;
j /= 2;
}
}
class CallByValue {
public static void main(String args[]) {
Test ob = new Test();
int a = 15, b = 20;
System.Out.Println(“Before call: ” + a + ” ” + b);
ob.Meth(a, b);
System.Out.Println(After call: ” + a + ” ” + b);
}
}
Java Recursion
•Recursion is the technique of making a function call itself. This technique provides a way to break complicated problems down into simple problems which are easier to solve.
Example
Use recursion to add all of the numbers up to 10.
public class Main {
public static void main(String[] args) {
int result = sum(10);
System.Out.Println(result);
}
Static vs. Non-Static Methods
•Java programs have either static or public attributes and methods.
•Static method can be accessed without creating an object of the class
•Public methods can only be accessed by objects
Override existing values
Final variable
•If you don’t want the ability to override existing values, declare the attribute as final
class OuterClass {
int x = 10;
class InnerClass {
int y = 5;
} }
public class Main {
public static void main(String[] args) {
OuterClass myOuter = new OuterClass();
OuterClass.InnerClass myInn = myOuter.New InnerClass();
System.Out.Println(myInn.Y + myOuter.X);
}}
The process by which one class acquires the properties (data members) and functionalities (methods) of another class is called inheritance.
•The aim of inheritance is to provide the reusability of code so that a class has to write only the unique features and rest of the common properties and functionalities can be inherited from the another class.
class ABC // base class or super class or parent class
{
int i,j;
void set()
{ //body of set method
}}
class XYZ extends ABC
{
int k;
void get()
{ // body of get method
super keyword
•The super keyword in Java is a reference variable which is used to refer immediate parent class object.
1.Super can be used to refer immediate parent class instance variable
2.Super can be used to invoke immediate parent class method
3.Super() can be used to invoke immediate parent class constructor
class Animal{
Animal(){
System.Out.Println(“animal is created”);}
}
class Dog extends Animal{
Dog(){
super();
System.Out.Println(“dog is created”);
} }
Method Overriding
• When a method in a subclass has the same name, 8same parameters or signature, and same return type as a method in its super-class, then the method in the subclass is said to override the method in the super-class
