• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

overriding

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
package inheritance;

class B {
String str = "Parent";
static void print() {
System.out.println("Parent Class");
}
void exec() {
print();
System.out.println(str);
}
}


public class Acls extends B {
String str = "Child";
static void print() {
System.out.println("Child Class");
}
public static void main(String[] args) {
Acls a = new Acls();
a.exec();
B b = new Acls();
b.exec();
}
}

the ouput is parent class
parent
parent class
parent

can anyone explain the above code clearly
 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yours is the case of static method. Static methods are not overriden so you get those outputs

When you call a.exec() as Acls doesn't have exec it will call the exec() in parent and print() call in exec will also be called from B as call from parent cannot go back to child. For the second case you are assigning child to parent and calling exec() and same way it works.

I hope this is correct.

Thanks.
 
challa
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
kalyani,
Iam sorry i didnt get you.Moreover what about the fields why it is printing parent in both the cases.

can anyone please explain me the above code.
 
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI Purnima

When derived class object is assign to base class references....

Variables and Static method use references so the base class variables and methods are displayed.Because it is resolved at compile time.

if public methods R in ur program then it use objects so derived class methods are called.Because it is known in run time only.

Hope uR clear now.....
 
challa
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sagar,

In the code we are calling a.exec(); ,here a is a reference of subclass only then why it is printing parent.

anyone please explain.

Purnima.
 
vidya sagar
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Purnima

Since we call exec() method, which is not overridden in derived class. So copy of that method will be available to dervied class.In which, call to print method is changed by compiler as super.print() while inheriting. therfore base class method is called

I hope u r clear now
 
vidya sagar
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi purnima

This is applicable only in the case of call to static methods
reply
    Bookmark Topic Watch Topic
  • New Topic