This question already has answers here:
Java: Interface with new keyword how is that possible?
(9 answers)
Closed 8 months ago.
I understood that java interface cannot be instantiated. However, the below code looks like it instantiated an interface ? I'm trying to understand the code.
interface I{
int a();
int b();
}
public class C {
public static void main(String [] arg){
I i=(new I(){
public int a(){return 1;}
public int b(){return 2;}
});
assert i.a()==1 && i.b()==2;
}
}
An interface is like an all-abstract class, but that unambiguously supports multiple inheritance. You can instantiate any child of it that implements all the abstract methods.
The code you are asking about is creating an anonymous class that provides an implementation of all the abstract methods. Since the class is anonymous, the type of its instance can is most specifically given as I. Given that it's anonymous, that's also the only aspect of it you care about.
Related
This question already has answers here:
Java How to call method of grand parents? [duplicate]
(3 answers)
Why is super.super.method(); not allowed in Java?
(22 answers)
Closed 3 months ago.
Assume that class A extends class B, which extends class C. Also all the three classes implement the method test(). How can a method in class A invoke the test()method defined in class C?
Answer is "It is not possible to invoke test()".
class C {
public static void test(){
System.out.println("IMESH ISURANGA");
}
}
class B extends C {
}
class A extends B {
//how prove it
}
class Demo {
public static void main(String[] args) {
//----
}
}
How to prove it?
The answer is "It is not possible to invoke test()". Because if it's possible, it would break encapsulation.
We can invoke the test() method in class B using "super." keyword. This happens under the assumption that we are aware that we are violating the encapsulation on our own class. But we are not allowed to access the class above the parent class using "super." keywords because we don't know what rules the superclass is enforcing. So, we can't bypass an implementation.
This question already has answers here:
Why should I ever overload methods?
(6 answers)
Closed 2 years ago.
I want to have an interface that allows me to use methods with optional parameters. Suppose I have an interface:
public interface Stuff {
public int Add();
}
And I have two classes A and B who implement the interface. One method needs parameter, but the other one doesn't.
public class CLASS A implements Stuff{
public int Add();
}
public class CLASS B implements Stuff{
public int Add(String name);
}
How can I achieve this?
I think You have to override function.
You can read about that here => https://www.geeksforgeeks.org/overriding-in-java/
This question already has answers here:
Overriding member variables in Java ( Variable Hiding)
(13 answers)
Closed 7 years ago.
I am using a Inheritance concept here. I have extended class A(Superclass) into class B(Subclass). And I have created an object of a subclass and by using that object I have called add() method. But it is printing a value 5(Super Class).
Why it didn't take subclass's value which is 10 ?
class A{
int a=5;
public void add(){
System.out.println(a);
}
}
class B extends A{
int a=10;
}
public class InheritExample {
public static void main(String args[]){
B b1=new B();
b1.add();
}
}
Help Appreciated.
Thanks.
There's no overriding of instance variables. Overriding only works on instance methods. Therefore, the method add of class A has access only to the a variable declared in A, even if you call that method for an instance of class B.
This question already has answers here:
Java - Interface, instantiating an interface?
(3 answers)
Closed 7 years ago.
I came across a piece of code that I don't entirely understand.
There is an interface that is created within a class like this:
public class SomeClass {
public interface SomeInterface{
public String getInfo(String str1, String str2);
}
public static final SomeInterface SomeOtherName = new SomeInterface() {
#Override
public String getInfo(String str1, String str2){
//return something;
}
}
}
and then in another class they call the method getInfo using SomeOtherName. I don't understand what is going on here. To be honest I have not created an interface while in a class and then create an object of that interface type and then override methods in it. Can someone please explain me what is going on in this piece of code as I need to test it.
They are called as annonymous classes
They enable you to declare and instantiate a class at the same time.
Edit :
They are like local classes except that they do not have a name. Use them if you need to use a local class only once.
SomeOtherName is an instance of an anonymous class that implements the SomeInterface interface.
This question already has answers here:
Why is Multiple Inheritance not allowed in Java or C#?
(17 answers)
Why is there no multiple inheritance in Java, but implementing multiple interfaces is allowed?
(21 answers)
Closed 9 years ago.
In C++, you can extends many classes, so what's the advantages of this design in Java that a class can only extends one class ?
Since interface is a pure kind of class(abstract class actually), why not limit the number of interfaces implementation just like class extension ?
Being able to extend only one base class is one way of solving the diamond problem. This is a problem which occurs when a class extends two base classes which both implement the same method - how do you know which one to call?
A.java:
public class A {
public int getValue() { return 0; }
}
B.java:
public class B {
public int getValue() { return 1; }
}
C.java:
public class C extends A, B {
public int doStuff() {
return super.getValue(); // Which superclass method is called?
}
}
Since interfaces cannot have implementations, this same problem does not arise. If two interfaces contain methods that have identical signatures, then there is effectively only one method and there still is no conflict.