Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 350
  • Last Modified:

class subclassing

>>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.
0
gudii9
Asked:
gudii9
  • 5
  • 4
  • 3
2 Solutions
 
objectsCommented:
The Math class is defined as final
http://download.oracle.com/javase/6/docs/api/java/lang/Math.html

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


0
 
gudii9Author Commented:
where are we subclassiing Math class in the code. please advise
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
objectsCommented:
class MyCalc extends Math{
0
 
for_yanCommented:
subclasing in fact means extending or creating class which inherits from the given class
0
 
gudii9Author Commented:
f we cannot extend/subclass how do we use those final classes like Math. please advise.
0
 
for_yanCommented:
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
0
 
objectsCommented:
You just use the Math class directly


class MyCalc {
public int random(){
        double iTemp = Math.random();
        return Math.round(iTemp);
        }
}
0
 
for_yanCommented:
By the way in most of the cases we use classes without subclassing them - jsut like they
are.
When you write your own code - in many cases you'll creater a class and never want to clreate
another class which inherits from the first one.

As they are mostly not final - they can be subckassed - but nit necessarily need to be suvbclassed
0
 
gudii9Author Commented:
just import the class and then use its methods right.
0
 
for_yanCommented:
yes thats how you do it
0
 
objectsCommented:
> just import the class and then use its methods right.

yes see my post above
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 5
  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now