Wednesday, 15 May 2013

java - case regarding method overriding of a class by subclass -



java - case regarding method overriding of a class by subclass -

i have developed below class shown below

class ab { int = 10; static void getf() { system.out.println("i good"); } } class hamm extends ab { int = 10; static void getf() { system.out.println("i , bad boy"); } public static void main(string[] args) { //hamm h = new hamm(); // h.getf(); //ham ab = new hamm(); //reference of parent class //a.getf(); // ab class } }

now query there scenario exisits getting variable of class ab method getf() of class hamm, please advise.

declare method non-static. utilize instance method anyway. static methods can not overriden.

here working sample:

class ab { int = 10; void getf() { system.out.println("i good"); } } class main extends ab { int = 10; @override void getf() { system.out.println("i , bad boy"); } public static void main(string[] args) { //hamm h = new hamm(); // h.getf(); //ham ab = new main(); //reference of parent class a.getf(); // ab class } }

and link on ideone.

java

No comments:

Post a Comment