About Question enthuware.ocajp.i.v8.2.1477 :

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

Moderator: admin

Post Reply
FrodoBaggins
Posts: 14
Joined: Tue Jun 02, 2015 8:32 am
Contact:

About Question enthuware.ocajp.i.v8.2.1477 :

Post by FrodoBaggins »

This question confused me. Doesn't a class implementing an interface have to implement its methods as well?

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

Re: About Question enthuware.ocajp.i.v8.2.1477 :

Post by admin »

Not if the class is abstract!
If you like our products and services, please help us by posting your review here.

FrodoBaggins
Posts: 14
Joined: Tue Jun 02, 2015 8:32 am
Contact:

Re: About Question enthuware.ocajp.i.v8.2.1477 :

Post by FrodoBaggins »

You mean the Getaddress() method is abstract?

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

Re: About Question enthuware.ocajp.i.v8.2.1477 :

Post by admin »

No, I was just answering your question about the requirement of a class implementing an interface.
getAddress is a "default" method in the interface. In other words, it is already implemented by the interface so a non-abstract class implementing that interface doesn't necessarily have to implement it.
If you like our products and services, please help us by posting your review here.

islam.amin099
Posts: 4
Joined: Mon Jul 20, 2015 1:47 pm
Contact:

Re: About Question enthuware.ocajp.i.v8.2.1477 :

Post by islam.amin099 »

Hi there,
Why is the Bungalow's getAddress method is called rather than that of House since MyHouse implements both? And shouldn't this be an ambiguous call to getAddress method?
Thanks in advance.
EDIT: Ok now I understood the issue thanks anyway.

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

Re: About Question enthuware.ocajp.i.v8.2.1477 :

Post by admin »

The implementation provided by Bungalow overrides the implementation provided by House. That is what the explanation is trying to explain. A subinterface can override a default method. So MyHouse doesn't have two versions on getAddress. It gets only one, which is the one provided by Bungalow.
If you like our products and services, please help us by posting your review here.

rachnagoel18
Posts: 1
Joined: Thu Feb 25, 2016 12:47 am
Contact:

Re: About Question enthuware.ocajp.i.v8.2.1477 :

Post by rachnagoel18 »

class MyHouse implements Bungalow, House{ }
this class will not compile until it provides implementation of getAddress() method. This rule is applicable since Java SE 8. I have tried same code snippet at my end.

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

Re: About Question enthuware.ocajp.i.v8.2.1477 :

Post by admin »

Not sure how you are trying but the code given in the question compiles fine.

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

JaredTse
Posts: 20
Joined: Sat Apr 23, 2016 2:52 pm
Contact:

Re: About Question enthuware.ocajp.i.v8.2.1477 :

Post by JaredTse »

How is this question different from

postingaskquestion.php?mode=post&f=2&qi ... .v8.2.1479

Which says that this will not compile:

JaredTse
Posts: 20
Joined: Sat Apr 23, 2016 2:52 pm
Contact:

Re: About Question enthuware.ocajp.i.v8.2.1477 :

Post by JaredTse »

The code

Code: Select all


interface I1{   
    public default void m1() {      
        System.out.println("in I1.m1");   
    }
}


interface I2{   
    public default void m1(){      
        System.out.println("in I2.m1");   
    }
}

class CI implements I1, I2{//This class will not compile.
}

class C2 implements I1, I2{ //This class will compile because it provides its own implementation of m1.   
   public void m1(){      
       System.out.println("in C2.m1");   
   }
}


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

Re: About Question enthuware.ocajp.i.v8.2.1477 :

Post by admin »

Both are different situations. Bungalow extends House but I2 doesn't extend I1.
-Paul.
If you like our products and services, please help us by posting your review here.

vidyats
Posts: 5
Joined: Wed Jul 12, 2017 11:58 pm
Contact:

Re: About Question enthuware.ocajp.i.v8.2.1477 :

Post by vidyats »

In this question, is there any way to get Houses default method to be used rather than overridden method?
say I want to print "101 Main Str";

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

Re: About Question enthuware.ocajp.i.v8.2.1477 :

Post by admin »

No, that is not possible from any Bungalow instance.
If you like our products and services, please help us by posting your review here.

asenevtimov
Posts: 2
Joined: Mon Oct 16, 2017 5:48 am
Contact:

Re: About Question enthuware.ocajp.i.v8.2.1477 :

Post by asenevtimov »

What about System.out.pirntln(House.super.getAddress());

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

Re: About Question enthuware.ocajp.i.v8.2.1477 :

Post by admin »

What happened when you tried it out?
If you like our products and services, please help us by posting your review here.

pinnacle28188
Posts: 2
Joined: Thu Dec 14, 2017 6:13 pm
Contact:

Re: About Question enthuware.ocajp.i.v8.2.1477 :

Post by pinnacle28188 »

I thought that interfaces could only be method signatures with out any code?

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

Re: About Question enthuware.ocajp.i.v8.2.1477 :

Post by admin »

pinnacle28188 wrote:I thought that interfaces could only be method signatures with out any code?
That was indeed the case before Java 8. Java 8 allows you to define methods (static and default). Here is a link that explains this nicely: https://www.journaldev.com/2752/java-8- ... ult-method
If you like our products and services, please help us by posting your review here.

pinnacle28188
Posts: 2
Joined: Thu Dec 14, 2017 6:13 pm
Contact:

Re: About Question enthuware.ocajp.i.v8.2.1477 :

Post by pinnacle28188 »

Thanks for that explanation.

kevin35
Posts: 8
Joined: Mon Dec 25, 2017 4:30 am
Contact:

Re: About Question enthuware.ocajp.i.v8.2.1477 :

Post by kevin35 »

Code: Select all

interface House {
	int x=1;
	public default String getAddress() {
		return "101 Main Str";
	}
}
interface Bungalow extends House {
	int x=1;
	public default String getAddress() {
		return "101 Smart Str";
	}
}
class MyHouse implements Bungalow, House {
	 void foo() {
      getAddress();
		System.out.println(x);   //compile error: The field x is ambiguous
	}
}
Hello Paul,
class MyHouse doesn't have a problem with the overriding methods
but why it does not work for variable?
doesn't Bungalow hide the variable x in house?

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

Re: About Question enthuware.ocajp.i.v8.2.1477 :

Post by admin »

Yes, x in Bungalow does hide the x in House but your MyHouse class implements both the interfaces so it gets access to two xs independently. If you just implement Bungalow, Bungalow's x will be used instead of House's x.

Unlike static fields (and also static methods and instance fields), instance methods are overridden (not hidden). There are different rules for overriding. The nearest superclass's or interface's method overrides any other implementation of the same method that it might have inherited. So in this case Bungalow's getAddress will override House's getAddress. Unlike, the variable x, MyHouse will not get two versions of getAddress.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 38 guests