Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Overriding rules... Please confirm!

 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In K&B's book, It's said that.....

Polymorphism lets you use a more abstract supertype (including an interface) reference to refer to one of its subtypes(including interface implementers).

That we can refers the object of a class which implements any of the sun type interface, regarding without the class hierarchy. If wrong, correct it and give a good explaination. Thanks in Advanced!
 
Sheriff
Posts: 9697
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Abimaran its not clear what you want to ask. First of all the book is trying to say you this

Now coming to your question, do you want to ask why this doesn't generate any error
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, Please explain this line. I don't understand the underlined line! Thanks in Advanced!

Polymorphism lets you use a more abstract supertype (including an interface) reference to refer to one of its subtypes(including interface implementers).

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You know what the interfaces are right??
Interface implementers are those classes which implement the interface.
lets give an example


here k is interface and d is the interface implementer
the want to say that in polymorphism we can declare objects as
k obj=new d();
means an object is assigned to a reference variable of supertype
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, Manish A Awasthy, It's a known thing, I think, that is not explained by that underlined line!
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Polymorphism refers to "1 form many ways".

Typically 2 types exist in Java.

Static Polymorphism

- The behavior/method is determined at compile time.
- Overloading is the best example for that.

Dynamic Polymorphism

- The behavior/method is determined at run time. (If the reference and Object are different).



Polymorphism allows us to design more flexible and extensible systems.

 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not this......
 
Ramakrishna Gutha
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What exactly you want ??
 
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Yeah Ankit I cannot understand why the foll works (your e.g) : -

interface MyInterface {}
class MyClass {} //no relation between class and interface
class Main {
public static void main(String[] args) {
MyClass c = new MyClass();
MyInterface i = (MyInterface) c; //allowed
}
}
 
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Simran Dass wrote:

Yeah Ankit I cannot understand why the foll works (your e.g) : -

interface MyInterface {}
class MyClass {} //no relation between class and interface
class Main {
public static void main(String[] args) {
MyClass c = new MyClass();
MyInterface i = (MyInterface) c; //allowed
}
}



It's because the compiler doesn't know the "actual" type, so it can't deny this. Think about it, you might have subclass of MyClass which implements MyInterface. Making MyClass final (and not implementing MyInterface there/any superclass of that) will deny compiling this code..
 
Ranch Hand
Posts: 317
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Then why this is not legal?

 
Ramakrishna Gutha
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Harpreet,

This code is not legal.



This is reason why Java is called strictly typed language because "casting to type should be valid".

In the above code if the MyClass is a subclass of MyTestClass then at the compile time it is valid. But at the runtime it throws ClassCastException.


Hope you got cleared.

Regards
GRK
 
Harpreet Singh janda
Ranch Hand
Posts: 317
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, You are right this is illegal because java is strictly typed language. But why JAVA behave strange in case of interface and why


is legal is above example?

Sorry for bugging you but i did not get the point mentioned above.
reply
    Bookmark Topic Watch Topic
  • New Topic