[HD Pg 313, Sec. 11.4.1 - overriding-methods]

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
OCAJO1
Posts: 221
Joined: Mon Nov 26, 2018 2:43 pm
Contact:

[HD Pg 313, Sec. 11.4.1 - overriding-methods]

Post by OCAJO1 »

I suggest that you write a similar example to validate the above rules in case of hiding of a static method.

Code: Select all

class Account{
    double balance; double rate;
    Account(double balance, double rate){
        this.balance = balance;
        this.rate = rate;
    }
    double getInterest( InterestCalculator ic, double yrs ){
        try{
            Number n = ic.computeInterest(balance, yrs, rate); //1
            return n.doubleValue();
        }catch(Exception e){
            System.out.println(e);
            //e.printStackTrace();
        }
        return 0.0;
    }
}

class AccountManager{
    public static void main(String[] args){
        
        Account a = new Account(100, 0.2);
        
        InterestCalculator sic = new InterestCalculator();
        double sinterest = a.getInterest(sic, 3);
        System.out.println("Straight Interest: "+sinterest);
        
        InterestCalculator cic = new CompoundInterestCalculator();
        double cinterest = a.getInterest(cic, 3);
        System.out.println("Compound Interest: "+cinterest);
     }
}     
Given the last line of 11.4.1 and the above code segment from the section, if //line 1 were calling static methods (per definition both overridden and overriding), passing different references will not cause it to access the methods referenced by the passed reference. It will always access the overridden method.

So I was wondering is there a way that instead of passing a reference to the getInterest method, pass the class name? Of course, Number n =ic.computeInterest(balance, yrs, rate); has to change to something like Number n =className.computeInterest(balance, yrs, rate);

Thanks

admin
Site Admin
Posts: 10386
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: [HD Pg 313, Sec. 11.4.1 - overriding-methods]

Post by admin »

You could create a lambda expression and pass the method reference to the static method that you want to pass. (Not in scope for OCAJP 8).

mcpunjabi
Posts: 9
Joined: Tue Apr 23, 2024 3:03 am
Contact:

Re: [HD Pg 313, Sec. 11.4.1 - overriding-methods]

Post by mcpunjabi »

hello

4. throws clause - An overriding method cannot put a wider exception (i.e. a superclass
exception) in its throws clause than the ones present in the throws clause of the overridden
method. For example, if the overridden method throws IOException, the overriding method
cannot throw Exception because Exception is a superclass of IOException. ✅

The overriding method may throw a subclass exception such as FileNotFoundException. ✅

The overriding method cannot throw a new exception that is not listed in the throws
clause of the overridden method either. ❌

The overriding method may decide to not have a throws clause altogether though. Note that this rule applies only to checked exceptions because these are the only ones the compiler cares about. There is no rule regarding unchecked exceptions ❌

The first error i say because my overriden method throws IOException and my overriding throws IOException and ArithmeticException without errors.
The second you say that the overriding method can omit throws clause only if overriden method throws unchecked as ArithmeticException, but i even tried with checked exceptions and got no error.

Thanks for your lightening

mcpunjabi
Posts: 9
Joined: Tue Apr 23, 2024 3:03 am
Contact:

Re: [HD Pg 313, Sec. 11.4.1 - overriding-methods]

Post by mcpunjabi »

so here is the entire recap over the throws in override (Pls confirm if i got it right)

1. Overriding methode can omit throws clause, no matter of checked or unchecked exception (because i don't get any error doing that)

2. Overriding methode cannot add extra exceptions if they are not subclass of the main exception but unchecked can be added as many as wished

3. If Overriding method use throws than it must be the same exception class as overriden methode or a child or any unchecked exception (here it's interesting because it's like having no throws clause for compile cuz he doesnt care about unchecked)

admin
Site Admin
Posts: 10386
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: [HD Pg 313, Sec. 11.4.1 - overriding-methods]

Post by admin »

The rules regarding the throws clause are only for checked exceptions. Unchecked exception have no rules. That is why they are called unchecked i.e. they are not checked by the compiler for any rules. So, once you take unchecked exceptions out of the picture, you will understand the rules for the throws clause better. Without that even the first point will seem incorrect because you can throw a wider unchecked exception from the overriding method.

This is explained in detail in the previous chapter (on Exception handling). Even in this particular section 11.4.1, the last line clearly says, "There is no rule regarding unchecked exceptions."

admin
Site Admin
Posts: 10386
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: [HD Pg 313, Sec. 11.4.1 - overriding-methods]

Post by admin »

mcpunjabi wrote:
Mon Jun 10, 2024 6:11 am
so here is the entire recap over the throws in override (Pls confirm if i got it right)

1. Overriding methode can omit throws clause, no matter of checked or unchecked exception (because i don't get any error doing that)

2. Overriding methode cannot add extra exceptions if they are not subclass of the main exception but unchecked can be added as many as wished

3. If Overriding method use throws than it must be the same exception class as overriden methode or a child or any unchecked exception (here it's interesting because it's like having no throws clause for compile cuz he doesnt care about unchecked)
All correct.

admin
Site Admin
Posts: 10386
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: [HD Pg 313, Sec. 11.4.1 - overriding-methods]

Post by admin »

Note that this rule applies only to checked exceptions because these are the only ones
the compiler cares about. There is no rule regarding unchecked exceptions.
This is a separate paragraph and the rule that this is talking about is the 4th rule about overriding (which contains multiple subrules) and not just the last subrule of the throws clause.

mcpunjabi
Posts: 9
Joined: Tue Apr 23, 2024 3:03 am
Contact:

Re: [HD Pg 313, Sec. 11.4.1 - overriding-methods]

Post by mcpunjabi »

tnx a lot for quick answears

Post Reply

Who is online

Users browsing this forum: No registered users and 5 guests