I know that java does not support multiple inheritance so we have something called interfaces but I have some doubts about interfaces which i could not clear even after going through google and various textbooks.
1. What is the difference between interfaces and abstarct classes
2. How interfaces serve to multiple inhertance. e.g. if i have class A and B how do i create class C inheritng both A nd B?
I ll be very happy if anyone can address this problems with example codes.
1. What is the difference between interfaces and abstract classes
Multiple interfaces can be implemented, while only one abstract class can be extended.
An example of an abstract class is an Applet.
If you want to make an applet, you have to extend this abstract class. It's abstract because all applets do something different and there isn't a standard applet that can be used for anything. It isn't an interface because you don't want people to make a class extending Applet, so being an applet, and also extending e.g. Exception. That would not make sense at all.
Also the Applet class has some pre-made coding so you can easily make your own applet.
An example of an interface is KeyboardListener.
This is typically implemented by applets, and reacts to keyboard input. In fact, what it does is, if a browser loads an applet implementing KeyboardListener and a user types something (while using the applet), the JVM calls the appropriate function. For example, when a user presses a key, it calls the keyPressed() funtion. But of course, an applet tracking key events can also track mouse events. So it can implement MouseListener too, to receive mouse clicks.
If you implement an interface, you should write all functions, so that's the point of interfaces. If an applet implements the KeyboardListener interface and compiled correctly, the JVM can be sure the keyPressed() function will exist when it tries to call it.
2. How interfaces serve to multiple inhertance. e.g. if i have class A and B how do i create class C inheriting both A and B?
You shouldn't inherit two classes. There's no point in doing so. The purpose of classes is to make multiple classes which are similar without rewriting a lot of code, not just to import some functions like you probably want to do. If you want to do that, you should put them in a seperate class, and call them with their full name, like FunctionLib.createpicture()
IT was a very informative reply and mst of my doubts are almost clear with it.