We help IT Professionals succeed at work.

class subclassing

gudii9
gudii9 asked
on
375 Views
Last Modified: 2012-05-11
>>What will happen when you attempt to compile and run the following code?

class MyCalc extends Math{
public int random(){
        double iTemp;
        iTemp=super();
        return super.round(iTemp);
        }
}

public class MyRand{
public static void main(String argv[]){
        MyCalc m = new MyCals();
        System.out.println(m.random());
        }

Answer


4) Runtime Exception "IllegalMonitorStateException"

The wait/notify protocol can only be used within code that is synchronized. In this case calling code does not have a lock on the object and will thus cause an Exception at runtime.




Answer:

1) Compile time error

The math class is final and thus cannot be subclassed (MyCalc is defined as extending Math). This code is a mess of errors, you can only use super in a constructor but this code uses it in the random method.)



http://www.jchq.net/certkey/0801certkey.htm

It is not clear to me on why we cannot subclass MyCalc class.

Any ideas, resources, links, sample code highly appreciated. thanks in advance.
Comment
Watch Question

Java Developer
CERTIFIED EXPERT
Top Expert 2010
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
Awarded 2011
Awarded 2011

Commented:
It deos not say you cannot subclass MyCalc
it says you cannot subclass Math because Math is declared final


Author

Commented:
where are we subclassiing Math class in the code. please advise
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
class MyCalc extends Math{
Awarded 2011
Awarded 2011

Commented:
subclasing in fact means extending or creating class which inherits from the given class

Author

Commented:
f we cannot extend/subclass how do we use those final classes like Math. please advise.
Awarded 2011
Awarded 2011

Commented:
we use Math class as it is - t has lots
of useful satic methods like
Math.sqrt() , etc,

We dont have need to sublclass it
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
You just use the Math class directly


class MyCalc {
public int random(){
        double iTemp = Math.random();
        return Math.round(iTemp);
        }
}
Awarded 2011
Awarded 2011
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
just import the class and then use its methods right.
Awarded 2011
Awarded 2011

Commented:
yes thats how you do it
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
> just import the class and then use its methods right.

yes see my post above
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.