java - methods invoking, code understanding -
i have question in understanding 1 of codes. appreciate if can me understanding it.
what happens if declaration of c:
class c { void process1(char ch) { system.out.println ("inside process1 in c " + ch); } void process2(char ch) { system.out.println ("inside process2 in c " + ch); } void process3(char ch) { system.out.println ("inside process3 in c " + ch); process2(ch); } }
is followed following declaration of extension:
class extc extends c { void processl(int n) { system.out.println ("inside process1 in extc " + n); } void process2(char ch) { system.out.println ("inside process2 in extc " + ch); } void process4(int n) { system.out.println ("inside process4 in extc " + n); } }
which methods invoked if declaration of 3 objects
extc object1 = new extc( ); c object2 = new extc( ), object3 = new extc( );
is followed these statements:
object1.process1(1000); object1.process4(2000); object2.process1(3000); object2.process4(4000); object3.process1(‘p'); object3.process2('q'); object3.process3('r');
question number 3.
i realized first print 1- inside process 1 in extc 1000 print 2- inside process 4 in extc 2000
but then? happen in object2 , object 3 ? should @ extend or class c?
you know when invoking method, compile time there check method in class denoted refference type , in run time creates instance of class denoted object type , check method there. here 3 objects of extc class.extc class have 5 instance methods.process1(char ch) ,process2(char ch) ,process3(char ch) ,process1(int n) ,process4(int n): 3 super class c.the process2(char ch) has got overriden here.
consider object1,refference type & object type both of extc.so there can invoke 5 methods. consider object2,there can invoke methods ,included in both classes i.e.2 classes denoted refference type,'c' & object type ,'extc'.so method process1(int n) & process4(int n) can't invoked. consider object3,there can invoke methods included both in object class(denoted refferece) & extc class .so non of 5 can't invoked. try answers again yourself.
Comments
Post a Comment