package methodOverridding;
public class MethodOverRiddingExample5 extends MethodOverRiddingExample4{
public void sum(int a, int b) {
int c = a + b;
System.out.println("sum is from child class with two argument:-" + c);
}
public void sum(int a, int b, int c) {
int d = a + b + c;
System.out.println("sum is from child class with three argument:-" + d);
}
void test4(){
System.out.println(" i am from child class");
}
public static void main(String[] args) {
MethodOverRiddingExample4 obj = new MethodOverRiddingExample5();
/**
* if you will make reference of parent class, then we can access only parent
* class members through reference. at compile time
*/
/**
* At run time method will be called based on the class for which we have
* created object, if method is not available in child class then it will
* check in parent class.
*/
obj.sum(4, 5);
obj.sum(4, 5, 6);
obj.mutiply(5, 6);
MethodOverRiddingExample5 obj1 = new MethodOverRiddingExample5();
obj1.sum(2, 3);
obj1.sum(2, 3, 7);
obj.mutiply(7, 9);
obj1.test4();
/**
* if you will make reference of child class, then we can access parent
* class members as well child class members at compile time
*/
}
}
public class MethodOverRiddingExample5 extends MethodOverRiddingExample4{
public void sum(int a, int b) {
int c = a + b;
System.out.println("sum is from child class with two argument:-" + c);
}
public void sum(int a, int b, int c) {
int d = a + b + c;
System.out.println("sum is from child class with three argument:-" + d);
}
void test4(){
System.out.println(" i am from child class");
}
public static void main(String[] args) {
MethodOverRiddingExample4 obj = new MethodOverRiddingExample5();
/**
* if you will make reference of parent class, then we can access only parent
* class members through reference. at compile time
*/
/**
* At run time method will be called based on the class for which we have
* created object, if method is not available in child class then it will
* check in parent class.
*/
obj.sum(4, 5);
obj.sum(4, 5, 6);
obj.mutiply(5, 6);
MethodOverRiddingExample5 obj1 = new MethodOverRiddingExample5();
obj1.sum(2, 3);
obj1.sum(2, 3, 7);
obj.mutiply(7, 9);
obj1.test4();
/**
* if you will make reference of child class, then we can access parent
* class members as well child class members at compile time
*/
}
}
Good Post! Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge as updated one, keep blogging.
ReplyDeleteCore Java Training in Electronic city