• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

CoVariant Returns, and other returns

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do i have this essentially right? (too much studying I am not second guessing myself)

Let's say

Class A {}
Class B extends A{}
Class C extends B{}

In function arguments you can pass to the method doIt(B b) either a B or a C, not an A

If you're returning an object in a function
public B doIt()
you can return a C, but not an A

If when you are using variables you can do:
A a = (A) new B() //need a cast?
B b = new B()
C c =new B()

also if (lets say all the classes had the same functions) there are any static methods, or if there are variables in the functions, then when you call a.getStaticMethod you actually use the values in class a, not b, or c not B, but if the overridden methods are not static, then we would use the b functions and all three would return the same value?

Access modifiers can only go "more public"
but exceptions you can only through teh same exception of a subclass correct? nothing new?

Thanks, it's getting late
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ben Harrison:
Do i have this essentially right? (too much studying I am not second guessing myself)

Let's say

Class A {}
Class B extends A{}
Class C extends B{}

In function arguments you can pass to the method doIt(B b) either a B or a C, not an A



right

If you're returning an object in a function
public B doIt()
you can return a C, but not an A



right

If when you are using variables you can do:
A a = (A) new B() //need a cast?
B b = new B()
C c =new B()



almost.

you cannot call a B object C because C is a subclass of B.

also if (lets say all the classes had the same functions) there are any static methods, or if there are variables in the functions, then when you call a.getStaticMethod you actually use the values in class a, not b, or c not B, but if the overridden methods are not static, then we would use the b functions and all three would return the same value?



remember that static methods are not overridden, they are redefined. So if a static method is called, the type of reference calling the method determines which version is used. If an instance method is overridden, then the runtime type of the object determines which method is called.

Access modifiers can only go "more public"
but exceptions you can only through teh same exception of a subclass correct? nothing new?



actually you can include additional exceptions if they are RuntimeException or one of its subclasses.

also you can include a subset of the exceptions listed in the superclass or subclsses of the exceptions listed.
[ March 11, 2007: Message edited by: Keith Lynn ]
 
Ben Harrison
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Keith, could you just explain the last two sections on the exceptions and

remember that static methods are not overridden, they are redefined. So if a static method is called, the type of reference calling the method determines which version is used. If an instance method is overridden, then the runtime type of the object determines which method is called.

 
Keith Lynn
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Consider this example.



The output of this program is



We have an instance of type A and an instance of type B, but both with A reference types.

In the first two method calls, despite the instance the reference refers to, the type of the reference determines which method is called since method() is static.

In the second two calls, method1() is an instance method, so the type of object referred to determines which method is called.

Also note that in the throws clause of method() in B we include ArithmeticException even though it was not listed in the throws clause of the overridden method.

In method1() in B, we don't list SQLException even though it's listed in the throws clause of the overridden method.
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The explcit cast in

A a =(A)new B();

is not required.Since sub clas with a super class reference is allowed.

Also in overiding SQLException ,ArithmeticException are both run time exceptions(unchecked).Is this the reason why they work fine in the above code?
 
Keith Lynn
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
SQLException is a checked exception.

However, you're not required to list it in the throws clause of the overridding method even though it's listed in the throws clause of the overridden method.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic