Java Immutables builder interface defintion in parent class? - java

In the Immutables documentation it says that:
For advanced use cases, it may be desirable to have builders that produce different types of objects but conform to the same interface, akin to the original Builder pattern. This is achievable by declaring a static nested class named “Builder” which will be extended by the generated builder.
It then provides an example where:
there is an interface that multiple Immutable classes implement called Vehicle
there is an interface for the builders the Immutable class will generate call VehicleBuilder
the two abstract Immutable classes Scooter and Automobile implement Vehicle and have a abstract static class that implements VehicleBuilder.
This is super useful but it means every Immutable implementation needs to duplicate the abstract static class line. Is there any way to push this up into the parent definition?
I've tried making Vehicle an abstract class and moving the abstract static class into it:
public abstract class Vehicle {
public abstract static class Builder implements VehicleBuilder {}
}
However this does not work and the builder on the generated class does not extend it.

Related

Abstract Class Vs. [Interface+Inheritance] in Java

In context of Java, could a class replace the need of extending an abstract class by extending another non-abstract class and implementing an interface together, both of which combined have all the methods(abstract and implemented), of an abstract class?
In context of Java, could a class replace the need of extending an
abstract class by extending another non-abstract class and
implementing an interface together, both of which combined have all
the methods(abstract and implemented), of an abstract class?
Can it? Yes
Should it? No
An abstract class can be replaced by a concrete one, but you will be altering your system.
Do remember: an abstract class can not be instantiated, nor should it be, since it's not 'concrete enough' to make sense for your business. (If it does, it shouldn't have been an abstract class to begin with)
If you make it concrete, you risk that developers will use instances of the (what-should-be) abstract class.
If you change it the way you propose:
public void doSomething(MyAbstractClass instance){
// here we know there is an implementation provided by a subclass
}
would become
public void doSomething(MyShouldBeAbstractClass instance){
// here they can pass instances of the base class, which might have unsupported methods
}
For instance:
public String getConcreteInformation(){
throw new UnsupportedOperationException("Should be called on a child class");
}
and could lead to a lot of nasty bugs

Do Abstract classes need to implement an entire interface in java 7?

I need a group of different classes to implement a certain interface. However, a lot of those classes, but not all of them, need a same implementation for some of the methodes defined in the interface. I was wondering if I could make an abstract class implement the interface and only create the methods that are the same for those classes?
For example, I have interface A:
public interface A{
public returnType method1(){};
public returnType method2(){};
}
Can I do this:
public abstract class AbstractPartialA implements A{
#Override
public returnType method1(){
implementation
}
}
and than have the classes extending from this Abstract class implement the remaining methods that are required to fulfill the interface?
Yes, you can, that is the exact purpose of abstract classes.
Do Abstract classes need to implement an entire interface in java 7?
And the answer is " NO ". Abstract class can implement entire interface or it can implement only some methods of an interface.
Case-1
If it implements entire interface and is still declared as 'abstract', it means we don't want other(people who are going to use our class) to create object for our class
Example of such class is HttpServlet in javax.servlet.http . Here HttpServlet class doesn't have any abstract method, but still it is declared as 'abstract'
Case-2
Simple, if the class doesn't implement any one of the method of an interface, then it is declared as 'abstract'. Now it will be the responsibility of the other class which extends the abstract class to provide the implementation of such method which is not implemented by 'abstract class'
You can, and when you try to extend from AbstractPartialA Java will ask you to:
Implement all methods declared in implemented interfaces and not implemented
Implement all methods declared as abstract in superclasses
Remember that a class is considered to implement all interfaces implemented by its superclasses, not just the ones specifically written after the implements keyword in this class' declaration. This applies both to the types of the class (and thus the types of its references) and to the methods it is required to implement.

Abstract class and interface together?

I have a section of my code where some classes are implementing an interface.
It feels correct, but there is a little duplication among the child classes - namely 3 methods.
So this is screaming out to use an abstract class.
My question is, will there be any cons in using both abstract class and interface in the following situations:
Abstract class to implement the interface and child classes to extend the abstract class
Child classes to extend the abstract class and implement the interface
Or
Should abstract classes and interfaces not be used together at all like this?
It's perfectly normal to use these two together. Consider for instance AbstractList (implementing List) and AbstractMap (implementing Map) in the JDK.
My knee-jerk reaction would have been to have the abstract class implement the interface and then have the concrete classes derive from it:
abstract class Base implements TheInterface {
/* ...shared methods... */
}
class Concrete1 extends Base { }
class Concrete1 extends Base { }
But your question raising the other possibility made me think, and I can't see much of an argument against doing it that way:
abstract class Base {
/* ...shared methods... */
}
class Concrete1 extends Base implements TheInterface { }
class Concrete1 extends Base implements TheInterface { }
Further, I can see an argument for doing it that way, specifically that it removes the coupling between the abstract class and the interface. If you have another class that needs the functionality Base provides but doesn't need to implement the interface, you have the flexibility to do that.
There's also a third option: Composition. You could not have an abstract class at all, but rather have the multiple concrete classes that implement the interface use a common helper class in their implementation:
class Helper {
/* ...shared methods... */
}
class Concrete1 implements TheInterface {
/* ...uses instance of Helper */
}
class Concrete1 implements TheInterface {
/* ...uses instance of Helper */
}
This has that same flexibility, in another form.
I do not think there is a rule of thumb as such. When designing try and follow the SOLID principles to figure out if what you are doing is good or bad. You can find these principles over here. In this particular case, I would think you should ensure you are abiding by the "Open-Close Principle".
Personally I prefer the statement that an abstract class is a background for other classes, so if three other classes have something common, their common ancestor, the abstract class created only for those three other classes, should provide also code from the interface. This would make the abstract class "complete" (in its way) providing all these properties and methods that the three classes share.
However, it finally makes no difference if all of them would implement the same interface. Making abstract class giving everything that is common is in my opinion a more clear way. It's then easier to compare classes by looking only at this what differs.

Why nested abstract class in java

I am wondering what does it mean to have a nested abstract class ? for example,
abstract class A{
abstract class B{
}
}
Are there any use cases or scenario that we might need such as design ? or is there something useful in such pattern ? and why Java allows us to do it ?
In design, you want the base class class A to present only an interface for its derived classes. This means, you don’t want anyone to actually instantiate an object of the base class. You only want to upcast to it (implicit upcasting, which gives you polymorphic behavior), so that its interface can be used. This is accomplished by making that class abstract using the abstract keyword. In other hand you want to use only part of functionality of class A so you create class B (as child) to reduce the coupling or implementation dependencies between systems and prevent duplicates.
But bear in mind when you define an inner class, code without inner classes is more maintainable and readable. When you access private data members of the outer class, the JDK compiler creates package-access member functions in the outer class for the inner class to access the private members. This leaves a security hole. In general we should avoid using inner classes. Use inner class only when an inner class is only relevant in the context of the outer class and/or inner class can be made private so that only outer class can access it. Inner classes are used primarily to implement helper classes like Iterators, Comparators etc which are used in the context of an outer class. About abstract class, it should be abstract to helpers, suppose your helpers should be too complicated to write abstract form for them.
In your case, I don't remember extensive usage of nested abstract classes, maybe in Swing world.
abstract classes are used to provide a partial implementation of a class for inheritance. it allows you to define the scheme of a class without providing the full definiton, so that it can be specified in a child class. it works somewhat like a Interface in that you can perform any operation specified in the abstract class upon an instance of any classes derived from it. Nested abstracted classes are designed to be inherited by other inner classes (even anonymous ones I think) but not by classes defined outside the outermost class.
public class HelloEveryone{
abstract class Hello{
void SayHello(){
System.out.println("Hello!");
}
abstract void SayHelloAlt();
}
public class HelloWorld extends Hello{
public void SayHelloAlt(){
System.out.println("HelloWorld!");
}
}
public class HelloUniverse extends Hello{
public void SayHelloAlt(){
System.out.println("HelloUniverse!");
}
}
void Go(){
ArrayList<Hello> hellos = new ArrayList<Hello>();
hellos.add(new HelloWorld());
hellos.add(new HelloUniverse());
for (Hello h : hellos){
h.SayHello();
h.SayHelloAlt();
}
}
}
static void main(){
HelloEveryone hello = new HelloEveryone();
hello.Go();
}

I've found many questions on abstract classes and interfaces but answer me this: Can an abstract class do everything an interface can?

I'm really confused, and I've read a TON of questions on this topic and I have not been able to pinpoint anything specifically that an interface can do that an abstract class cannot do.
Is there anything an interface can do that an abstract class cannot do?
I am asking in the context of my Java class, but feel free to remove the java tag if it applies to other languages as well (possibly C#?).
Edit: I understand that an abstract class can do things an interface cannot, but if an abstract class can do everything an interface can do, then what is the point of an interface? What does "implement multiple interfaces" mean?
Interfaces as such cannot do what abstract classes do.
This is because abstract classes can contain code - interfaces cannot. But any given class can only have one superclass - extends - as opposed to any number of interfaces - implements, so if you use abstract classes you essentially paint yourself in a corner of the inheritance tree, because your class can only extend a single class.
This limitation does not apply to interfaces, allowing a single class to have many purposes depending on how many interfaces it implements.
You can't inherit from multiple abstract classes in c#, but you can implement multiple interfaces.
I think this may apply to java too
Edit:
You can't inherit from multiple classes. If you have an abstract class called Clonable, and an abstract class called Disposable then you can only inherit one of these classes and you're forced to make a decision about which class your class should be a subtype of:
e.g:
public abstract class Clonable
{
public abstract void Clone();
}
public abstract class Disposable
{
public abstract void Dispose();
}
// MyClass cannot be Disposable too, it is not allowed by the language
public class MyClass : Clonable
{
override void Clone()
{
}
}
Note it is a design decision of the language to allow you only to inherit from one class.
If on the other hand you have interfaces, the language allows you to implement both
e.g.
public interface IClonable
{
void Clone();
}
public interface IDisposable
{
void Dispose();
}
public class MyClass : IClonable, IDisposable
{
void IClonable.Clone()
{
}
void IDisposable.Dispose()
{
}
}
Well read:
http://mindprod.com/jgloss/interface.html
http://mindprod.com/jgloss/interfacevsabstract.html
What does "implement multiple interfaces" mean?
Consider:
public static interface Ifun extends Comparable<Ifun>, Serializable {}//or
public static class Cfun2 implements Comparable<Cfun>, Serializable
When class implements Ifun, then:
Comparable interface imposes a total ordering on the objects of each class that implements it. Objects that implement this interface can be sorted automatically by Collections.sort.
The serialization interface has no methods or fields and serves only to identify the semantics of being serializable.
It means object can have more then 1 interface.
Interfaces are non instantiable classes that only contains methods that subclasses can inherit. The only thing that interfaces can do (in java) is that a class can implement many interfaces while a class can only extend 1 abstract class.

Categories