00:00
G02.png
/*01, no argument */ public class G01{ void show(){ System.out.println("1"); } } class Xyz extends G01{ void show(){ System.out.println("2"); } public static void main(String[] args){ G01 t=new G01(); t.show(); Xyz x=new Xyz(); x.show(); } } // 1 // 2
/*02, same sequence of parameters */ public class G02{ void show(String a, int b){ System.out.println("sequence-1"); } } class Xyz extends G02{ void show(String a, int b){ System.out.println("sequence-2"); } public static void main(String[] args){ G02 t=new G02(); t.show("raj",10); Xyz x=new Xyz(); x.show("ram",20); } } // sequence-1 // sequence-2
/*03, same parameters */ public class G03{ void show(int a, int b){ System.out.println("same number of arguments-1"); } } class Xyz extends G03{ void show(int a, int b){ System.out.println("same number of arguments-2"); } public static void main(String[] args){ G03 t=new G03(); t.show(10,20); Xyz x=new Xyz(); x.show(30,40); } } // same number of arguments-1 // same number of arguments-2
/*04, single same type of argument */ public class G04{ void show(String a){ System.out.println("type of arguments-1"); } } class Xyz extends G04{ void show(String a){ System.out.println("type of arguments-2"); } public static void main(String[] args){ G04 t=new G04(); t.show("monika"); Xyz x=new Xyz(); x.show("jyoti"); } } // type of arguments-1 // type of arguments-2
/*05, Return-Type (pt. = cld.), (OBJECT = STRING) */ public class G05{ Object show(String a){ System.out.println("covariant return type-1"); return null; } } class Xyz extends G05{ String show(String a){ System.out.println("covariant return type-2"); return null; } public static void main(String[] args){ G05 t=new G05(); t.show("monika"); Xyz x=new Xyz(); x.show("jyoti"); } } // covariant return type-1 // covariant return type-2
/*06, Return-Type (cld.=pt.), (STRING=OBJECT) */ public class G06{ String show(String a){ System.out.println("covariant return type-1"); return null; } } class Xyz extends G06{ Object show(String a){ System.out.println("covariant return type-2"); return null; } public static void main(String[] args){ G06 t=new G06(); t.show("monika"); Xyz x=new Xyz(); x.show("jyoti"); } } // show(String) in Xyz cannot override show(String) in G06 Object show(String a){ // return type Object is not compatible with String
/*07, pt. & cld. mt A.Mod. is default */ public class G07 { void show(String a){ System.out.println("covariant return type-1"); } } class Xyz extends G07{ void show(String a){ System.out.println("covariant return type-2"); } public static void main(String[] args){ G07 t=new G07(); t.show("monika"); Xyz x=new Xyz(); x.show("jyoti"); } } // covariant return type-1 // covariant return type-2
/*08, pt. mt A.MOD = public, cld. mt A.MOD = default */ public class G08 { public void show(String a){ System.out.println("covariant return type-1"); } } class Xyz extends G08{ void show(String a){ System.out.println("covariant return type-2"); } public static void main(String[] args){ G08 t=new G08(); t.show("monika"); Xyz x=new Xyz(); x.show("jyoti"); } } // show(String) in Xyz cannot override show(String) in G08 void show(String a){ // attempting to assign weaker access privileges; was public
/*09, pt A.MOD = protective cld. A.MOD = public */ public class G09 { protected void show(String a){ System.out.println("protected - public-1"); } } class Xyz extends G09{ public void show(String a){ System.out.println("protected - public-2"); } public static void main(String[] args){ G09 t=new G09(); t.show("monika"); Xyz x=new Xyz(); x.show("jyoti"); } } // protected - public-1 // protected - public-2
/*10, pt m/t not throws any e/x, cld only throws unchecked */ public class G11 { void show(){ System.out.println("parent ok - child only Un checked -1"); } } class Xyz extends G11{ void show() throws ArithmeticException{ System.out.println("parent ok - child only Un checked -2"); } public static void main(String[] args){ G11 t=new G11(); t.show(); Xyz x=new Xyz(); x.show(); } } // parent ok - child only Un checked -1 // parent ok - child only Un checked -2 // ArithmeticException , UN-Checked Exception
/*11, pt m/t not throws e/x, cld m/t throws checked e/x */ public class G12 { void show(){ System.out.println("parent ok - child checked, error -1"); } } class Xyz extends G12{ void show() throws Exception{ System.out.println("parent ok - child checked, error -2"); } public static void main(String[] args){ G12 t=new G12(); t.show(); Xyz x=new Xyz(); x.show(); } } // Exception , Checked Exception // in Xyz cannot override show() in G12 public void show() throws Exception{ // overridden method does not throw Exception
/*12. pt an cld can throws same r/t e/x ( arithmatic )*/ public class G13 { void show() throws RuntimeException{ System.out.println("parent - child same exception -1"); } } class Xyz extends G13{ void show() throws RuntimeException{ System.out.println("parent - child same exception -2"); } public static void main(String[] args){ G13 t=new G13(); t.show(); Xyz x=new Xyz(); x.show(); } } // parent - child same exception -1 // parent - child same exception -2 // Exception , Checked Exception
/*13, pt throws pt e/x , cld throws cld e/x. */ public class G14 { void show() throws RuntimeException{ System.out.println("parent exception to child exception-1"); } } class Xyz extends G14{ void show() throws ArithmeticException{ System.out.println("parent exception to child exception -2"); } public static void main(String[] args){ G14 t=new G14(); t.show(); Xyz x=new Xyz(); x.show(); } } // parent exception to child exception-1 // parent exception to child exception -2 // ArithmeticException child of RuntimeException // child = if no exception , it also works
/*14, p/t throws cld e/x, cld throws pt e/x */ public class G15 { void show() throws RuntimeException{ System.out.println("parent exception to child exception-1"); } } class Xyz extends G15{ void show() throws Exception{ System.out.println("parent exception to child exception -2"); } public static void main(String[] args){ G15 t=new G15(); t.show(); Xyz x=new Xyz(); x.show(); } } // show() in Xyz cannot override show() in G15 void show() throws Exception{ // overridden method does not throw Exception // Exception parent of RuntimeException // in child, equal level or child level exception works
/*15, pt has abs. cls with abs. m/t with concrete m/t , body not defined */ abstract public class G16 { abstract void display(); void show() { System.out.println("parent exception to child exception-1"); } } class Xyz extends G16{ void show() { System.out.println("parent exception to child exception -2"); } public static void main(String[] args){ G16 t=new G16(); // can't create asbtract class m/t t.show(); Xyz x=new Xyz(); x.show(); } } // Xyz is not abstract and does not override abstract method display() in G16 class Xyz extends G16{
/*16, pt has abs, cls with abs. m/t with concrete m/t , body defined */ abstract public class G17 { abstract void display(); void show() { System.out.println("abstract class abstract m/t must oR -1"); } } class Xyz extends G17{ void display(){} // must override the abstract m/t void show() { System.out.println("abstract class abstract m/t must oR -2"); } public static void main(String[] args){ Xyz x=new Xyz(); x.show(); } } // abstract class abstract m/t must oR -2
/*17, pt is itf with abs m/t , body not defined */ interface i1{ void displayi1(); // by default , all m/t are abstract } abstract public class G18 { abstract void display(); void show() { System.out.println("abstract class abstract m/t must oR -1"); } } class Xyz implements i1{ void show() { System.out.println("abstract class, abstract m/t must oR -2"); } public static void main(String[] args){ Xyz x=new Xyz(); x.show(); } } // Xyz is not abstract and does not override abstract method displayi1() in i1 class Xyz implements i1
/*18, pt is itf with abs. m/t , body defined with pulbic a/m*/ interface i1{ void displayi1(); // by default , all m/t are abstract } abstract public class G19 { abstract void display(); void show() { System.out.println("abstract class abstract m/t must oR -1"); } } class Xyz implements i1{ void displayi1(){} // need to add public also void show() { System.out.println("interface m/t -2"); } public static void main(String[] args){ Xyz x=new Xyz(); x.show(); } } // displayi1() in Xyz cannot implement displayi1() in i1 // void displayi1(){} attempting to assign weaker access privileges; was public
/*19, cld cls can call pt cls m/t using super */ public class G20{ void show(){ System.out.println("super-1"); } } class Xyz extends G20{ void show(){ super.show(); // points parent m/t System.out.println("super-2"); } public static void main(String[] args){ Xyz x=new Xyz(); x.show(); } } // super-1 // super-2 // We can call parent class method in overriding method using super keyword.
/*20, Final / Static / Private , Not OR */ public class G21{ final void show(){ System.out.println("final-1"); } } class Xyz extends G21{ void show(){ System.out.println("final-2"); } public static void main(String[] args){ Xyz x=new Xyz(); x.show(); } } // show() in Xyz cannot override show() in G21 // void show(){ overridden method is final
/*21, - Static, not oR */ public class G22{ static void show(){ System.out.println("static-1"); } } class Xyz extends G22{ void show(){ System.out.println("static-2"); } public static void main(String[] args){ Xyz x=new Xyz(); x.show(); } } // show() in Xyz cannot override show() in G22 // void show(){ overridden method is static
/*22, - Private, not oR */ public class G23{ private void show(){ // private accesibility is within class System.out.println("private-1"); } } class Xyz extends G23{ void show(){ System.out.println("private-2"); } public static void main(String[] args){ Xyz x=new Xyz(); x.show(); } } //private-2 // m/t not override
/*23, Pt M/T Synchronized, Acceptable */ public class G24{ synchronized void show(){ System.out.println("synchronized-1"); } } class Xyz extends G24{ void show(){ System.out.println("synchronized-2"); } public static void main(String[] args){ Xyz x=new Xyz(); x.show(); } } // synchronized-2 // The presence of synchronized/strictfp modifier with method have no effect on the rules chronized/stricte, mephosible // override a non synchronized/strictfp one and vice-versa.
/*24, child m/t synchronized, acceptable */ public class G26{ void show(){ System.out.println("synchronized-1"); } } class Xyz extends G26{ synchronized void show(){ System.out.println("synchronized-2"); } public static void main(String[] args){ G26 g1 = new G26(); g1.show(); Xyz x=new Xyz(); x.show(); } } // synchronized-1 // synchronized-2
/*25, Cld Is Strictfp, Version Dependent */ public class G27{ void show(){ System.out.println("strictfp-1"); } } class Xyz extends G27{ strictfp void show(){ System.out.println("strictfp-2"); } public static void main(String[] args){ G27 g1 = new G27(); g1.show(); Xyz x=new Xyz(); x.show(); } } // [strictfp] as of release 17, all floating-point expressions are evaluated // strictly and 'strictfp' is not required strictfp void show(){
/*26, - parent is strictfp, version dependent */ public class G28{ strictfp void show(){ System.out.println("strictfp-1"); } } class Xyz extends G28{ void show(){ System.out.println("strictfp-2"); } public static void main(String[] args){ G28 g1 = new G28(); g1.show(); Xyz x=new Xyz(); x.show(); } } // warning: [strictfp] as of release 17, all floating-point expressions are // evaluated strictly and 'strictfp' is not required strictfp void show(){
/*27, - covarient rule for M/T OR-2 */ class GClass { public void publicdMt() { /* public aMod can OR */ System.out.println("Public super m/t"); } protected void protectedMt() { /* protected and public aMod can OR */ System.out.println("Protected super m/t"); } void defaultMt() { /* protected and public and default aMod can OR */ System.out.println("Default super m/t"); } private void privateMt() { /* can't OR */ System.out.println("Private super m/t"); } } class Subclass extends GClass { @Override public void publicdMt() { /* works fine */ System.out.println("Public sub m/t "); } @Override protected void protectedMt() { /* works fine */ System.out.println("Public sub m/t "); } @Override void defaultMt() { System.out.println("Default sub m/t "); } /* @Override can't use this also */ void privateMt() { System.out.println("Private sub m/t "); } public static void main(String[] args){ GClass g1 = new GClass(); g1.protectedMt(); g1.defaultMt(); g1.publicdMt(); // g1.privateMt(); /* error */ Subclass s1 = new Subclass(); s1.privateMt(); s1.protectedMt(); s1.defaultMt(); s1.publicdMt(); } } // Protected super m/t // Default super m/t // Public super m/t // Private sub m/t // Public sub m/t // Default sub m/t // Public sub m/t
/*28, - covarient rule for M/T OR-1 */ class GClass{ public static void main(String[] args){ System.out.println("just for check"); } } class Animal { Animal reproduce() { System.out.println("Animal is reproducing."); return new Animal(); } } class Dog extends Animal { @Override Dog reproduce() { System.out.println("Dog is reproducing."); return new Dog(); } } // just for check
Home PREVIOUS NEXT