Java abstract class extends two classes - java

I have got a similar line digging up a OpenSource Project:
abstract class AbstractClass<A extends ParentClass1,
B extends ParentClass2>
Can you please tell me what actually it means? I think java does not support multiple inheritance so what actually we are doing here? And what is A and B here? :S

Here A and B are just place holders which can be replaced with any class that extends ParentClass1 and ParentClass2 respectively. You can pass arguments in angular brakets(<>) when you create object of this class. These arguments will be substituted for A and B by the compiler during compilation.
The above code does not indicate multiple inheritance. Please read generics in detail to understand this.

it's generic type
look here for generics: http://docs.oracle.com/javase/tutorial/java/generics/types.html

If you remove the content within <>, then you see its only
abstract class AbstractClass
that means, the class is not extending any classes at all.
abstract class AbstractClass<A extends ParentClass1,
B extends ParentClass2>
essentially means, the class would like to declare 2 generics A and B, and force its type. That means, it want the implementer of the class to supply the 2 classes which extends ParentClass1 and ParentClass2 respectively.
Please see this for more clarification

Related

Java Enum can't extends other Enum, class can

User from Stackoverflow asked this question, unfortunately doesn't have answer which can explain situation.
Why enum can't extend another class while all other classes can
I have the same question. Java also tells that it is multyple extends and it is not supports. Java documentation.
But this part confusing me. If any class by default extends Object class(Main class in Java), and any new class can extend another class (except Object class), but any new Enum can't? Why?
Any Enum extends Enum class by default and for special for enum can't extend more classes, and if say it is program way, will be more understandable. But in documentation was writen can't extends because it is multyple classes. Maybe Documentation isn't correct? If someone can explain this part, please explain this.
Thank you!
As the comments on the question you linked to explain, all classes do not extend Object.
A class:
class Foo { }
implicitly extends Object, but a class:
class Bar extends Foo { }
Just extends Foo.
It is also a descendant of Object via Foo.
So an enum implicitly extends Enum, and can't extend anything else.
Enums are final classes, so nothing can extend them.
From the documentation you have linked:
Note: All enums implicitly extend java.lang.Enum. Because a class can
only extend one parent (see Declaring Classes), the Java language does
not support multiple inheritance of state (see Multiple Inheritance of
State, Implementation, and Type), and therefore an enum cannot extend
anything else.
Basically Enum has complier magic built in, that actually has the complier extend the Enum class from the java.lang.Enum.
Java doesn't support multiple inheritance of classes and hence you cant have extended Enums.
See this from the spec jls-8.9 as well.
An enum type is implicitly final unless it contains at least one enum
constant that has a class body.
It is a compile-time error to explicitly declare an enum type to be
final.

Why won't Java let you inherit from a generic type-variable?

public class MyClass<T> extends T {...}
The above declaration will fail to compile with the error:
error: unexpected type
class MyClass<T> extends T {}
^
required: class
found: type parameter T
where T is a type-variable:
T declared in class MyClass
I can't really think of a reason for this to happen, so I am wondering if someone can shed some light on why it is that Java won't let you inherit from a generic type-variable.
The most obvious reason I can think of isn't even about type-erasure; it is the fact that when you make A a subclass of B, the Java compiler needs to know what constructors B has. If a class does not have a no-arguments constructor, then its sub-classes must call one of its defined constructors. But when you declare
public class MyClass<T> extends T {...}
It is absolutely impossible for the compiler to know what the super constructors are, since T is not fixed at compile-time (that's the whole point of generics after all), which is a situation that cannot be allowed in Java.
Java has quite a lot of language restrictions unlike C++ for example. What you want is not possible for many reasons listed in the comments (T might be final or have abstract methods). However, you are allowed to extend from a supertype having the generic type parameter:
public class MyClass<T> extends AnotherClass<T>
You might find the following alternative interesting:
public class MyClass<T extends AnotherClass> extends AnotherClass
What you want to do does make not much sense.
Your question is not so weird as it may look like :) Consider how would you deal with following:
Suppose your real class for T has a single constructor with 3 parameters. How would you implement the constructor of inherited class, if you don't know how to call the super constructor?
Suppose your real class for T has public final methods and you have defined methods with the same signature in the inherited class. What method would your object have? You cannot resolve such conflict.
Simple deductions based on your question.
T is a type.
MyClass extends T - MyClass is enhanced version of T.
MyClass < T> extends T - MyClass is enhanced T, but only for Type T.
there is no reason to state ' I extend T but only for type T'.
If you extend T, MyClass is already a Type of T & definitely not some X,Y or Z.
Generics are needed if you want to ensure Type safety, if you extend it is already type safe.

Generics in Java with and abstract class

I was reading some code for learn something about manage data in Android.
When i read a litle of the code...
I find this declaration.
public abstract class DBObject<T extends DBObject<?>> implements Cloneable{}
I understadn that this is and Abstract class with T that is a Generic Object
but i don't understand why y have to put a wildcart after Extend the same class.
Here is all the code
https://github.com/tasks/tasks/blob/master/src/main/java/com/todoroo/andlib/sql/DBObject.java
It looks like this is used so that it can be parameterised with its subclasses. It would be used like so:
class Foo extends DBObject<Foo> { ... }
It appears this is done solely so that the as() method will return the correct type.
The reason for the wildcard is because without it you would have to somehow specify a recursive generic definition, DBObject<T extends DBObject<T extends DBObject<...>>> - which is impossible.
Edit: Although see comments below; the wildcard is not actually necessary.

when we are inheriting a class and interface then we have to write first extends and then implements but not viceversa?why

why in inheritance first we have to write first extends and then inheritance why not implements and then extends ?what makes then to think in this angle
class A{}
interface B{}
if we write
class C implements B extends A {}
it will show compile time error but if we write
class c extends A implements B{}
it will run fine why so?
Because it is defined that way in the Language Specification #8.1:
NormalClassDeclaration:
ClassModifiersopt class Identifier TypeParametersopt Superopt Interfacesopt ClassBody
As you can see, the extending part (Superopt) comes before the implementing part (Interfacesopt).
I think this is just a simple demand of the Java language.
For example you cannot write int private variable, the access modifiers and data types have to be at a special position. The same here with the inheritance (class Y extends Z implements A).
The answer is that it's a design decision, defined in the Java Language Specification. It doesn't explain why the order has to be strict, but Java has strict rules by design (and that's one of the qualities that helps to keep many cryptic bugs away).
A possible reason is that extends represents a a stronger relationship than implements (although both represent "is a" or "is a type of" relationships). If a certain class implements dozens of interfaces, and it were allowed to list the interfaces before the class, you might miss the important extends clause which scroll off the screen.
Being strict also makes writing the compiler easier than if the rules were lax. Since only one extends class is allowed, as soon as you read the extends token and the class name you are done and can read the next tokens. If you could use implements before extends, then you would have to parse a list of interfaces before the extends.
It makes sense:
You can extend a class, using an extend keyword, to produce a bigger class (base + extension)
You can extend an interface, using an extend keyword, to produce a bigger interface (base + extension).
It is the same mechanism, thus the same name. It is some form of a programmatic "copy and paste" - as I always liked to think of it: we copy the source from base class and paste into the other one. We can also copy some text from base interface to another one (the extended).
Now, we need the distinction between a class and an interface and it's a pratical one: interface describes a contract, class follows some contract.
So there is a relation between a class and interface: the class fullfills a contract (interface). Another words, class implements an interface.
As for your example:
class c extends A implements B{} can be deconstructed into:
class c extends A
class c implements B
A class can only extend another class.
A class can only implement an interface.
As for the first example
class C implements B extends A {} - it cannot be deconstructed and the reason is only based on the grammar! Purely on syntactical (textual) level it's forbidden.

What does this generics code below do ?

I am unable to understand what the generics code below does. I am new to generics so would appreciate all the help i can get!
Public abstract class AMetadata< C extends CMetadata, P extends PMetadata, R extends RMetadata> extends GEntityMetada<C>
{
// class does stuff here
}
Could anyone explain how the classes are related ?
This specifies that the AMetadata class will deal with three generically-defined types, each of which are guaranteed to extend a different type (CMetadata, PMetadata, and RMetadata, respectively).
Furthermore, the AMetadata class itself extends the GEntityMetada generic class, with its generic argument being the first generic argument type (C, which extends CMetadata) passed to AMetadata.
To say how the classes are related would require more knowledge of the code base than this snippet provides. For example, it is possible (though unlikely) that a single type could actually extend CMetadata, PMetadata, and RMetadata, and that type could therefore be used as an argument to all three classes. But there is nothing in this generic definition to indicate that there has to be any relationship between these three classes.
The only other information you can really get from this is that a type that extends CMetadata is a valid generic parameter for the GEntityMetada class. Whether GEntityMetada requires its argument to extend CMetadata is unclear.
The generic type params are subtype of a class, e.g. C is a subtype of CMetadata in your example.
The class AMetaData extends a class GentityMetad. So it has a generic parameter C. I can't tell if GentityMetad puts any restrictions on C.
For AMetadata, there is an additional requirement on C: it must extend CMetadata.
In addition, the class AMetaData has two more generic types, P and R, which must extend PMetadata and RMetadata respectively. These are unrelated to GEntityMetad.
It just states what is the template of AMetadata you are willing to create.
I'll use short names for clearness. So assume your code looks like this:
public abstract class AM <C extends CM, P extends PM, R extends RM> extends GM<C>
This means that you can create AM object but you have to say what type of elements it should have. Here is the example:
AM<CMChild, PM, RMChild> extends GM<CMChild>

Categories

Resources