• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Overriding rules... Please confirm!

 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • 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!
 
Ankit Garg
Sheriff
Posts: 9528
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • 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
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • 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).

 
Manish A Awasthy
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • 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
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, Manish A Awasthy, It's a known thing, I think, that is not explained by that underlined line!
 
Ramakrishna Gutha
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • 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
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not this......
 
Ramakrishna Gutha
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What exactly you want ??
 
Simran Dass
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
  • 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
}
}
 
Tapio Niemela
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • 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..
 
Harpreet Singh janda
Ranch Hand
Posts: 317
Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then why this is not legal?

 
Ramakrishna Gutha
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic