Block an interface to be extended [duplicate] - java

This question already has answers here:
Final interface in Java?
(10 answers)
Closed 6 years ago.
As since in a class I can do:
public final class Foo{}
wich means no more classes can extends that Foo class... e.g. String class is final, so no custom class can extends the class String.
How can I prevent to do the same with an interface?
If I do
public interface ISome{
void fly();
}
I would like to allow that
class A implements ISome {}
but block that
public interface IHouse extends ISome{
void fly();
}
doing this
public final interface ISome{}
makes no sense... and will bring a compile error like:
Illegal modifier for the interface

You can't.
Supposedly the Java designers didn't think there would ever be an appropriate use case for this: if you don't want an interface to be extended then really you ought to declare those functions directly in a concrete class.
That said, you can achieve this in C++ as in this language an interface is more of a convention - consisting of only pure virtual functions, and you can enforce non-extensibility with techniques such as friendship.

Related

implement a typed list List<SomeThing> [duplicate]

This question already has answers here:
When do I have to use interfaces instead of abstract classes? [duplicate]
(18 answers)
When to use an interface instead of an abstract class and vice versa?
(26 answers)
Closed 4 years ago.
Apologies for the title, I'm not quite sure what it should be.
I'm new to Java and have inherited a codebase with the following pattern:
public abstract class BaseThing{...}
public class ThingA extends BaseThing{
public List<BaseThing> elements;
}
public class ThingB extends BaseThing{
public List<BaseThing> elements;
}
public class ThingC extends BaseThing{...}
public class ThingD extends BaseThing{...}
I want to write a method that works on BaseThing(s) that have an elements property e.g. ThingA and ThingB.
A Java method that accepts multiple types as a single parameter is similar to what I want to achieve, however the interface is for a method instead of a property. I don't think it's possible with properties on an interface.
I also looked at method overloading, however it doesn't appear to scale well, I'd need to add a new method every time a new ThingX is created.
What is the Java way of achieving this?

unable to understand the working of interface in java [duplicate]

This question already has answers here:
Java - Method name collision in interface implementation
(7 answers)
Closed 8 years ago.
I have declared an interface
public interface A {
public void print();
}
another interface
public interface B extends A {
public void print();
}
Then class
public class C implements A,B {
#Override
public void print() {
System.out.println(B.i);
}
}
Kindly tell me why there is no compile time error when both the interfaces have methods with same name and how does compiler determine which interface's method is being implemented??
thanks in advance
Implementing an interface in Java means, guaranteeing that methods, that are declared in the interface are implemented into the class.
In your example this means, that every class that implements interface A has a print-function, that takes no arguments and returns a void. Same goes for interface B. Every class that implements B has a print-funtction that take s no argument and returns a void.
If now class C implements both interfaces A and B, this means, that your class has to implement all methods that were declared in A and all methods that were declared in B. In your example these methods overlap, but do not conflict. Therefore there is no problem in that.
Your method implements both interface methods at once.
Interface B override interface A method..
When you use Interface you say to Java: "I will implement this thing"
Once the B override A you say to Java: "I will implement the same thing also on B";
So if you are implementing this once.. Basically it is like you implemented The method for both of them.. And this is not the right way to use this

Multiple supports inheritance in Java [duplicate]

This question already has answers here:
Multiple Inheritance and class Object
(5 answers)
Closed 9 years ago.
Well Java does not support multiple inheritance in java..
But wait in eclipse we can see any class extends OBJECT class by default, we can see all methods of Object class if we try to add unimplemented methods.
Now MY POINT IS i can make my class extend any class for example Thread.
So now my class extends Thread by user defined side and Object by default...
that means multiple class inheritance ?
Behavior similar to multiple inheritance can be seen with Java interfaces:
// implements BOTH Runnable AND ActionListener
public class MultipleInterfaces implements Runnable, ActionListener {
#Override public void run() {}
#Override public void actionPerformed(ActionEvent e) {}
}
Multiple inheritance would be like this:
// Not allowed, complete nuts
public class Amalgam extends ArrayList<Thread>, JPanel, Font {
public Amalgam() {
super(); // <- and what would this do?
}
}
A class can only have one superclass i.e. in java one class can only extend one class. If one is not specified, then it is implicitly extends to Object.
So suppose the class is MyClass and it extends MySuperClass. As MyClass extends MySuperClass so it will not extend directly Object. But MySuperClass class itself is not explicitly extending any class so it extends Object and in turn MyClass also extends Object in the hierarchy.
So it is not Multiple inheritance rather it is Multilevel inheritance. Hope it helps.
The behavior mentioned by you is multi-level inheritance which java supports by default
This is MultiLevel inheritance and not Multiple inheritance.
any class extends OBJECT class by default
That means Thead class extends Object too.

why do we declare a static class inside normal class? [duplicate]

This question already has answers here:
Java: Static vs inner class [duplicate]
(8 answers)
Closed 9 years ago.
What is the purpose of defining a static class inside a normal class.
public class ClassA extends ClassB implements IA, IB {
public static classStatic extends ClassC implements I1, I2 {
}
}
What is the purpose of defining a static class inside a normal class.
Do all fields/variables need to be static, if its accessing inside the static class.
Can anyone explain/show article me what exactly is achieved in oops by implementing this in JavaScript way.
In Java you can only have one public/package only class per file; that class may require some computation/logic that is to complex and would fit better in another class but it is not general enough to be in another file, it is tight to the encompassing class.
If the inner class wouldn't be static, then you would need an instance of the encompassing class (in your case ClassA) to instantiate the inner class ClassC.
No they do not need to be static; it is a class like any other class from this point of view.
JavaScript is not a class based object-oriented language, so you cannot have classes of any kind.

What's the difference between the implements & extends keywords in Java [duplicate]

This question already has answers here:
Implements vs extends: When to use? What's the difference?
(19 answers)
Closed 8 years ago.
What's the difference between the following keywords in Java: implements, extends?
An interface is an abstract specification of how a class should behave whilst a class is a concrete implementation of such a specification.
Therefore, when you write implements you're saying that you are fulfilling some abstract specification in the implementation you've written.
extends means that you take either an implementation (class) or specification (interface) and add to it with different or new functionality (or change the specification of its behaviour), thus modifying its behaviour and extend-ing it.
a class extends another class and implements interface. interface extends another interface.
Interface hasn't any implemented methods all defined methods are empty so if class inherits from the interface it should implement it's methods. But if Class1 inherits from Class2 then it already have some working methods (from Class2) and just extends Class2.

Categories