Saturday, May 30, 2015

Overriding


public class Class1 {

// METHOD_OVERRIDING: Let us consider three classes ClassA ClassB and ClassC
// ClassB extends ClassA and then ClassC extends ClassB
// And now again consider a public method abc() is in both ClassA and ClassB
// ok now inside main of ClassC make an object of it and try to
// use the abc() method from the super class
                // <<BOTH SUPER CLASSES HAVE SAME METHOD>>
// As a result we will  get the output from the ClassB syso
//<<A bit like priority based (checkout method from ClassB) >>

public void abcd() {
System.out.println("This the SYSO from CLASS_1");
}

public static void main(String[] args) {

}
}

---------------------------------------------------------------------------------------------------------------------


public class Class2 extends Class1{

public void xyz() {
System.out.println("THIS IS SYSO FROM CLASS_2");

}
public void abcd() {
System.out.println("THIS IS SYSO FROM CLASS_2");

}
}

---------------------------------------------------------------------------------------------


public class Class3 extends Class2 {

public static void main(String[] args) {

Class3 c3 = new Class3();
c3.abcd();
}
}

--------------------------------------------------------------------------------------------------


No comments:

Post a Comment