aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Overriding rules... Please confirm! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Overriding rules... Please confirm!" Watch "Overriding rules... Please confirm!" New topic
Author

Overriding rules... Please confirm!

Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

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!


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9293
    
  17

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


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

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

Joined: Jan 18, 2010
Posts: 4
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


SCJP 1.6 98%
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Hi, Manish A Awasthy, It's a known thing, I think, that is not explained by that underlined line!
Ramakrishna Gutha
Greenhorn

Joined: Jan 18, 2010
Posts: 16
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

Joined: Nov 04, 2009
Posts: 2066

Not this......
Ramakrishna Gutha
Greenhorn

Joined: Jan 18, 2010
Posts: 16
What exactly you want ??
Simran Dass
Ranch Hand

Joined: Jan 09, 2010
Posts: 183


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

Joined: Jan 06, 2006
Posts: 77
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

Joined: Jan 14, 2010
Posts: 317

Then why this is not legal?

Ramakrishna Gutha
Greenhorn

Joined: Jan 18, 2010
Posts: 16
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

Joined: Jan 14, 2010
Posts: 317

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.
 
jQuery in Action, 2nd edition
 
subject: Overriding rules... Please confirm!