On Mar 15, 3:53 am, Anton Shaykin <[email protected]> wrote: > Well, thanks for the compliment :) > Now, there is something, I feel a bit unsure about... > What if we want to call some Student class methods, which are not > declared by PersonInterface, then will we need to create an instance > of Student class like the following: > Student stud = new Student(); Consider this. An abstract class can invoke methods of its subclass, therefore, it can be a good type candidate too. Example: AbstractPerson student=new Student(); // Valid. Even though calculateAverage() is not declared in AbstractPerson. student.calculateAverage();
Therefore, should not this recommendation be reserved for case by case basis? > Or is it a strict requirement for PersonInterface to declare all the > methods which could be used in superclass and its subclasses that > implement it? > Thanks. > > On Mar 14, 10:14 pm, Deepak A L <[email protected]> wrote: > > > 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 -~----------~----~----~----~------~----~------~--~---
