C# How To Do Inheritance From A Generic Class - c#-4.0

I have ClassA:
public class ClassA<T>
I have a generic method in ClassA:
protected TP FunctionA<TP>(Expression<Func<T, TP>> p)
{
//Code for method here
}
ClassB derives from ClassA:
public class ClassB : ClassA<ClassB>
ClassC derives from ClassB:
public class ClassC : ClassB
When creating ClassC like in the example above, ClassA will still have a type of ClassB because ClassC derives from ClassB, and ClassB is what sets the type of ClassA to ClassB.
What I am wondering is how do I derive from ClassB while at the same time setting ClassA type to ClassC and also ClassB still needs to be able to be used on its own without needing to derived from.
Thanks!

ClassB is no longer generic. Nor is ClassC. If you want to keep it generic then the pattern is:
class ClassB<T> : ClassA<T> {}
class ClassC<T> : ClassB<T> {}

Related

Groovy #Canonical and #TupleConstructor choking with abstract base class

I have the following Groovy classes:
#Canonical
abstract class BaseEntity {
Long id
String refId
}
#Canonical
#TupleConstructor(includeSuperFields = true, includeFields = true)
#ToString(includeSuperProperties = true)
class GroceryItem extends BaseEntity {
String name
Integer quantity
}
Then at runtime I'm creating an instance of GroceryItem:
GroceryItem cheeseWedges = new GroceryItem(1L,
'067e6162-3b6f-4ae2-a171-2470b63dff00', 'Cheese Wedges', 4)
When this constructor runs I get the following exception:
groovy.lang.GroovyRuntimeException: Could not find matching constructor for: com.example.myapp.GroceryItem(java.lang.Long, java.lang.String, java.lang.String, java.lang.Integer)
at groovy.lang.MetaClassImpl.invokeConstructor(MetaClassImpl.java:1732)
at groovy.lang.MetaClassImpl.invokeConstructor(MetaClassImpl.java:1532)
at org.codehaus.groovy.runtime.callsite.MetaClassConstructorSite.callConstructor(MetaClassConstructorSite.java:49)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallConstructor(CallSiteArray.java:60)
What's going on here? What's the fix? Is the fact that BaseEntity is abstract causing problems here? I seem to remember having a similar issue with these annotations + ABC's a few years ago.
Hope you might aware that Canonical itself is combination of ToString, TupleConstructor and EqualsAndHashCode.
Here since you are explicitly specifying TupleConsturctor with options, Canonical may be removed for sub-class i.e., GroceryItem.
Apart from it, need to includeSuperProperties=true options for TupleConsturctor in order to achieve the desired behaviour. Here is blog which you can refer for more details.
Since the base class is abstract, annotation is not required.
In case if the base class is regular class, and you want to call super() of base class constructor, then callSuper=true option can be included to TupleConstructor annotation of child class. Of course, Canonical would be required that time for base class as well.
In case if a property is defined with access modifier in base class, say public String description then includeSuperFields=true option needs to be added to TupleConstructor of child class.
Here is fixed code snippet:
import groovy.transform.*
abstract class BaseEntity {
Long id
String refId
}
#TupleConstructor(includeSuperProperties=true)
#ToString(includeSuperProperties=true)
class GroceryItem extends BaseEntity {
String name
Integer quantity
}
def item = new GroceryItem(1L,'067e6162-3b6f-4ae2-a171-2470b63dff00', 'Cheese Wedges', 4)
println item.toString()
You can quickly try it online demo

Creating object of parent child class together in one line?

I wrote this but i can't understand what is this.
Public ClassA {
//some methods here
}
Public ClassB extends ClassA {
Public static void main(String[] args) {
ClassA abc=new ClassB
//What is purpose of this line and what advantage it gives us. I accidently wrote this but compiler (Eclipse not generating any error on this statement).
}
You happened to stumble across the distinction of static and dynamic type for a variable and its connection to the inheritance relation.
Your variable abc has a static type ClassA. Hence the compiler will only let you use methods defined in that class on that variable. After your initialization of abc, it has dynamic type ClassB.
Since ClassB extends ClassA, it has every method and attribute of ClassA (and maybe more) and it is OK to use is through abc.

Class to inherit the constructor of its base class

I would like to know if I can access the constructor of the base class in its derived classes in C#. If yes please let me know how could we make it. Thanks in advance.
You can call the base class constructor as part of the execution of the derived class constructor
public MyBase
{
public MyBase() { }
}
public Derived
{
public Derived() : base() { }
}
When using this pattern, you are said to be using the base class initializer.
For more background, see the base keyword and instance constructors on MSDN.

UML relationship of a static call from another class

I am creating a class diagram but I was wondering if there would be any association between the 2 classes shown below - as far as I understand it, for association, ClassA must have an instance of ClassB which in this case there is not, however, it does need to know about a variable of ClassB, so is there an association between these 2 classes?
public class ClassA()
{
int val = ClassB.x
}
public class ClassB()
{
public static int x = 5;
}
Sure there is association. You can't use ClassA without existing of ClassB.
Yes there is an association between these two classes. The association is neither an aggregation nor a composition, it is a "uses/usage" dependency.
ClassA ------Uses-----> ClassB
Take a look at this link to know more about different types of dependencies

derived entity instance in base type is null at initialization time

I have say ClassA entity = new ClassA(){firstname="blah", age=28}
Also class A inherit a ClassB, so in ClassB contructor I want to do something like ClassB()
{ do something with classA entity, but the think is that the entity instance is still null, it goes thru the newing up stage then after the values firstname and age get set, is there a way around this to be able to get the not null instance of the derived class and pass it to the base class? Thankx. Using C# 4.
are you calling ClassA's constructor from ClassB?
public class ClassA
{
}
public class classB
{
public ClassB(): base()
{
//Do something with ClassA
}
}
in client code i have ClassA c = new ClassA(){firstname=""}
in the library it goes like this:
public partial class ClassA: ClassB
{
}
public class classB
{
public ClassB()
{
AddValidation(..);//here i want to acces the instance of entity ClassA that was populated in client code.
}
}

Resources