Click here to Skip to main content
15,881,757 members
Please Sign up or sign in to vote.
1.14/5 (6 votes)
See more:
Hello Experts,

The question was asked me interview. Assuming that I am 10th standard student and you (experts) are my teachers. How you are going to explain about interface to 10th standard student. Give me the example of real time scenarios. Please do not post the link.

Thanks in Advace!!!
Posted
Updated 2-Apr-21 18:09pm
Comments
Sergey Alexandrovich Kryukov 7-Mar-12 1:11am    
Real-time, aha... :-)
--SA

As you know
An interface contains only the signatures of methods, delegates or events. The implementation of the methods is done in the class that implements the interface.
when you implemnt interface You are very much sure ,that the method in the interface is implemented in the class.

You can say Interface is a class which contain all unimplemented methods taht are implemented in Class that implement it.

Use:

Suppose you have a project with lot of classes and each class have no. of methods.
So , How you know which class implement perticuler methods.We make interface for each/some classes and implement these interfaces in that class.

See this
Discussion
 
Share this answer
 
Comments
Kiran Sonawane 7-Mar-12 1:09am    
Everything is correct. You forgot that I am 10th standard student. I dont know methods delegates ..... I don't want technical answer. Forgot about .net/C#
uspatel 7-Mar-12 1:15am    
Ok,
I can try...
Suppose your parents gives you a list of items to purchase,
that list is an Interface that you will implement at time of purchasing.....
Means Before implementing any thing , you list out what you have to done that will be interface......
Shobana George 16-Oct-13 5:03am    
Really good
ravithejag 27-Nov-14 4:51am    
perfect answer
yuvafun 5-Jan-16 3:31am    
This is very very good and perfect answer
I can provide two types of "real world" examples -- one from the business world and onw from the true "real world."
Real World:
You have an Animal class. It is an abstract class, because you cannot instantiate a generic "Animal," but it provides base functionality.
You have many devired classes of Animal. You have HomoSapiens, Platypus, Penguin (which extends Bird, another subclass of Animal), Giraffe, Housefly, etc. Each of these are concrete classes that may be instantiated (of course, there are several levels of abstract classes between these and Animal (like Chordata, etc.)
Now you want to make something fly. What can fly? Birds and Houseflys (among others), so these classes should provide similar functionality, even though they are widely spaced on our inheritance tree. The solution? Make them use interfaces. Bird and Housefly can not both implement the Flyer interface, so whenever we want something to fly, we can use a Flyer object, not caring whether it's a Bird or a Housefly. Likewise, Penguins and Playtpuses can implement the Swimmer interface (and Penguin should throw an IllegalOperationException in any of its Flyer methods, since Penguins cannot fly).
As far as business examples, I have a persistance engine that will work against any data sourcer (XML, ASCII (delimited and fixed-length), various JDBC sources (Oracle, SQL, ODBC, etc.) I created a base, abstract class to provide common functionality in this persistance, but instantiate the appropriate "Port" (subclass) when persisting my objects. (This makes development of new "Ports" much easier, since most of the work is done in the superclasses; especially the various JDBC ones; since I not only do persistance but other things [like table generation], I have to provide the various differences for each database.)
The best business examples of Interfaces are the Collections. I can work with a java.util.List without caring how it is implemented; having the List as an abstract class does not make sense because there are fundamental differences in how an ArrayList works as opposed to a LinkedList. Likewise, Map and Set. And if I am just working with a group of objects and don't care if it's a List, Map, or Set, I can just use the Collection interface.
Hope that this helps
 
Share this answer
 
Comments
Kiran Sonawane 7-Mar-12 1:19am    
My 5+. But check the answer by Uma Shankar Patel. Very simple!!!!
Santosh K. Tripathi 15-Mar-15 0:57am    
5+ from me also.
Interfaces are used to encode similarities which classes share, but do not necessarily constitute a class relationship. For instance, a dog and a lion can both run. We would make them inherit the Animal class and then implement Run via an interface IRun.

A beginner book might help you learn this as well.
 
Share this answer
 
v2
Comments
Kiran Sonawane 7-Mar-12 1:03am    
Thanks for reply. I can do this with simple abstract method within class. For instance add abstract method "Run" in animal class. Then why interface? (Please explain with same example)
Abhinav S 7-Mar-12 1:09am    
Ok don't think about the Run method. Think about something like Howl - a dog can howl but a lion cannot.
An IHowl interface might be required for a dog.
Santosh K. Tripathi 15-Mar-15 1:03am    
good one. 5+
johannesnestler 7-Mar-12 3:23am    
I think your answer is "best" - because simple, no deep programming knowledge required. Other answers might be tecnical correct, but if I tell my mom about abstract classes, she would be lost ;-)
Abhinav S 7-Mar-12 3:27am    
Thank you. :)
Go through the below link you can get a good example of Interface and abstract class in very easy language

http://shivasoft.in/blog/java/difference-between-interfaceinheritance-abstract-class/[^]

You can also go through the below link in which there was a discussion of interface and abstract classes... which will help you to understand concept more

Real world examples of abstract classes and interfaces?[^]
 
Share this answer
 
v2
Best Explanation will be like
Interface is a contract agreement and class that implements is builder.
When you go to buy a house you put an agreement to a builder with requirements you want one window, two bedrooms, two bath rooms etc(these are interface declaration) and the class which inherits interface is builder, failing to implements any of the requirements is violation of agreement (throws compilation error failing to implements any of the interface method.
Hope this help better understanding
 
Share this answer
 
Comments
Richard Deeming 6-Apr-21 10:00am    
Not only nine years late, but incorrect and misleading.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900