![]() ![]() So, that means that when you want to imply many interactions at one time then interface is your thing. ![]() A Java class can implement many interfaces but can inherit from a single abstract class. When we talk about interface, on the contrary then the relation or communication between interface itself and the class implying the interface is not as strong as it is in abstract classes. When we talk about abstract classes, there is a strong connection between two classes, because theses classes tend to gain or inherit from one another. The main point behind this argument is, the abstract class is strictly object oriented, although it has a method heading but no body. Abstract class cannot be relied upon completely because it lacks some parts. They are useful for modelling a class grouping or ranking and while while this is being carried out, the programmer get the idea of what has to be build after seeing the requirements. An abstract class may have one or more abstract methods. But both eat.(computer science) computer circuit consisting of the hardware and associated circuitry that links one device with another (especially a computer and a hard disk drive or other peripherals) What is Abstract Class?Īn abstract class is not completely implied by the programmer. Like Lion eats by hunting other animals where as cow eats grass. But the way each animal can eat something or sleep may differ. Now we know all animals do eat something and sleep. Like Lion is an animal, monkey is an animal, elephant is an animal, cow is an animal and so on. Let me explain with an example, we all know what animals are. In other words we can say we implement interfaces so that our class starts behaving in a particular way. Interfaces are made in situations when we know that some task must be done, but how it should be done can vary. Interfaces are collection of final static fields and abstract methods (Newly Java 8 added support of having static methods in an interface). What you are looking for are trait (like in Scala), unfortunately unavailable in Java. Serializable and Cloneable are examples of this. Marker interfaces: not adding any methods, but easily accessible via instanceof to discover object capabilities or wishes. Simple responsibilities: an instance of Tank object in a game is also Runnable to let you execute it in a thread and an ActionListener to respond to mouse events.Ĭallback interfaces: if object implements given callback interface, it is being notified about its life-cycle or other events. This is rarely a case in real-life and is actually a valid example where multiple inheritance would be better (or traits). You can use an instance of Tank where either the former or the latter is expected (polymorphism). Object effectively has two identities: a Tank is both a Vehicle and a Weapon. Interfaces are kind-of multiple inheritance with no problems that the latter introduces (like the Diamond problem). A class can be Runnable and Callable at the same time, while both methods are effectively doing the same thing. With interfaces a single class can have several " views", different APIs or capabilities. Note the keyword implements rather than extends. They replace it with safer, although slightly less powerful construct. Interfaces aren't used to achieve multiple inheritance. Then what are the exact benefits of using interfaces? Is it really Multiple-Inheritance that we achieve using Interfaces? But this thing also can be achieved using abstract classes and all. So we just need to code what to do in thread, Rest is pre-defined. One example of it is like in Runnable interface we have public void run() method in which we define functionality of thread and there is built in coding that this method will be run as a separate thread. NOTE : I have found one case in which interfaces are helpful. We need to write code again and again in all classes we implement it. Anyhow what is the benefit of using Interfaces ? They are not having any code. If implementing an interface is not inheritance then How interfaces are used to achieve multiple inheritance ? As interfaces are having only abstract methods (no code) so how can we say that if we are implementing any interface then it is inheritance ? We are not using its code. May be this is the most important thing for inheritance to be there. One Strong point of Inheritance is that We can use the code of base class in derived class without writing it again. Interfaces can be used to achieve multiple inheritance in Java. There is no multiple inheritance in Java. Interface is collection of ONLY abstract methods and final fields. ![]() (Similar questions have already been asked here but I was having some other points.) I still have some confusion about this thing. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |