• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

urgent! Need help for interview on super and super in polymorphism!

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have an interview tomorrow and I am still confused about what happens exactly when you call super. I know that you are calling the base class constructor and it initializes the members/parameterds, but what does this imply? Does this mean that I will have access to all methods that are not private in the base class and I don't need to redefine them? I have looked in many books and websites, yet I have not really found a solid, complete way of describing what super does. What would you answer to an interviewer when he would ask you this question? I am sure he will not just be impressed by me answering, "IT calls the base class constructor, initializes parameters and it is used for inheritance purposes." I also noticed that some functions in the derived class that override the same method in the base class have the following line in the method:
return super.toString() + "This is" + name + " a" + whatevervar;
What does super.toString to as compared to if it were done a different way?
Polymorphism,
1) What is a good DEFINITION of polymorphism, not an example, to answer in an interview?
2) I noticed in a multiple level of inhertitance topic, that in a derived class that will be used by polymorphism, ex class Animal is the base class and has a method called sound. This sound method will be used polymorphically by all derived classes, ie. class Dog, class Cat, etc. I noticed in an example I have here that in class Dog (as well as the other derived classes), they have
The say that there is nothing that prevents a derived class to be used as a base class.
Before the multiple level of inheritance was discussed, they defined the sound method of class Dog as follows:
public void sound()
{
System.out.println("Woof Woof");
}
After they discussed the multiple level of inheritance, they defined both the base class Animal and the sound method in the base class as abstract and this method seemed to disappear elsewhere: ie.
public abstract class Animal
{
public abstract void sound();
....

Now how is this sound method defined in the derived classes? Same as before or not?

In this case, why did they use abstract class and not interface? I know this topic was discussed often in these forums, but here I have an example and I would like to know from this one why abstract?
Thanks for all your help!
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have an interview tomorrow and I am still confused about what happens exactly when you call super. I know that you are calling the base class constructor and it initializes the members/parameters.

Depends on where you are calling super. When you call super() in the constructor (which is done whether or not you do it explicitly), this is what it does.

...but what does this imply? Does this mean that I will have access to all methods that are not private in the base class and I don't need to redefine them?

Yes, because you have extended a class your class will inherit the methods of the super (base) class that are not private. Whether or not you redefine them is another question altogether.

I also noticed that some functions in the derived class that override the same method in the base class have the following line in the method:

return super.toString() + "This is" + name + " a" + whatevervar;

What does super.toString to as compared to if it were done a different way?


return BaseClass.toString() + "This is" + name + " a" + whatevervar;

Polymorphism,

1) What is a good DEFINITION of polymorphism, not an example, to answer in an interview?


In object-oriented programming, polymorphism (from the Greek meaning "having multiple forms") is the characteristic of being able to assign a different meaning or usage to something in different contexts - specifically, to allow an entity such as a variable, a function, or an object to have more than one form. There are several different kinds of polymorphism.

2) class Animal is the base class and has a method called sound. This sound method will be used polymorphically by all derived classes, ie. class Dog, class Cat, etc. In class Dog (as well as the other derived classes), they say that there is nothing that prevents a derived class to be used as a base class.

True.

Before the multiple level of inheritance was discussed, they defined the sound method of class Dog as follows:

<pre>public void sound()
{
System.out.println("Woof Woof");
}
</pre>
After they discussed the multiple level of inheritance, they defined both the base class Animal and the sound method in the base class as abstract and this method seemed to disappear elsewhere: ie.

<pre>public abstract class Animal
{
public abstract void sound();
}
</pre>
Now how is this sound method defined in the derived classes? Same as before or not?


Yes, same as before. If the derived class fails to redefine the sound() method, the derived class must also be declared abstract.

[This message has been edited by Marilyn deQueiroz (edited December 02, 2001).]
 
this is supposed to be a surprise, but it smells like a tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic