Wow, This is Awesome and the best reply from [email protected] i have
ever seen .....very Much True in this case.....

Let me add one more thing here.
*
Coding to an interface is the best "design" Approach as against to coding
wrt to a Class.*

You need to change the implementation at only one place as against at 2
places, and the implementations as defined in RHS side in below example can
be changed at any given pointof time

//See below code
//PersonInterface has only method definitions as shown below.
Interface PersonInterface{
           public void nameOfPerson(String name); // some dummy method
}

Different implementations for same are Student,VicePresident,ViceChairman.

So in my code the below statements are perfectly valid.

PersonInterface pi = new Student();
(or)
PersonInterface pi = new VicePresident();
(or)
PersonInterface pi = new ViceChairman();

Hope i have made things clearer.
If Student is my class then the below practize of writing the code is NOT
Recommended EVEN though it is PERFECTLY VALID.

Student st = new Student();

(or)

VicePresident vp = new VicePresident() ;

(or)

ViceChairman vc = new ViceChairman();

---
Thanks and Regards,
Deepak Lalchandani
Bangalore
India

On Fri, Mar 13, 2009 at 4:16 PM, Anton Shaykin <[email protected]>wrote:

>
> > Why is using an interface as type is recommended over class? I cannot
> > see the advantage of doing so.
>
> Because, doing so, you can achieve run-time polymorphism which is
> quite powerful, and your classes don't have to be extended.
> It's like assume you have an interface for a stack, which has abstract
> methods push() and pop(). And you have 2 different stack classes, say,
> FixedStack and DynamicStack (which is growable). And each of them has
> different implementation of your interface (that is different
> implementation of pop() and push() methods). Now, in your main class,
> you can create a reference of interface type and then assign it
> FixedStack and DynamicStack objects, and doing so, you will get
> different implementation of methods declared in your interface
> depending on type of object your interface reference currently refers
> to.
> So, after you assign PersonInterface pi reference to Person object,
> you can use the version of methods declared in Person class, then you
> can assign the same pi reference to Student object, and you will get
> different implementation of the same methods, and so on.
> Sorry if my explanation is too obscure. Maybe someone else will clear
> things up better :)
>
>
> On Mar 12, 8:39 pm, [email protected] wrote:
> > @ page: 21
> >
> > @ quote
> > ● Interfaces and classes are both types
> > - This means that an interface can be used in places
> > where a class can be used
> > - For example:
> > // Recommended practice
> > PersonInterface pi = new Person();
> > // Not recommended practice
> > Person pc = new Person();
> >
> > Question:
> > Why is using an interface as type is recommended over class? I cannot
> > see the advantage of doing so.
> >
> > Thank you.
>
> >
>

--~--~---------~--~----~------------~-------~--~----~
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/javaprogrammingwithpassion?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to