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.
Related
As I know, if the static method from other class, we may interpret their relationship with dependency, just like the answer from How to show usage of static methods UML Class Diagram
However, how about for the static variable from other class? Is it the similar case; using dependancy?
For example,
class A{
public static String CHAR="Charecter";
public static String INT="Integer";
public static String STR="String";
}
class B{
public String Type;
public B(){
Type=STR;
}
public void B(String t){
Type=t;
}
}
would it result in the uml class diagram as below?
Note that although I would rather use enumeration in real life for this case, I just would like to know how it works.
Yes, this is similar.
Dependency shows that one class is "aware" of some other class and uses it in some way (or more generally depends on it). It can for instance refer to (public) static attributes, static operations and so on.
Consider the following code:
// ======== Abstract class ========
public abstract class Creatures {
public abstract void loseEnergy();
public void execute()
{
loseEnergy();
}
}
// ======== Animals ========
public class Animals : Creatures
{
public override void loseEnergy(){}
}
public class Birds : Animals
{
public override void loseEnergy(){}
}
// ======== Human ========
public class Human : Creatures
{
public override void loseEnergy(){}
}
public class Male : Human
{
public override void loseEnergy(){}
}
public class Female : Human
{
public override void loseEnergy(){}
}
[ This code was based on the code by Jayson suggested here: "Base class methods calling derived class methods ?" ]
In the given code example, I would like to have the runtime executing EACH derived class object's certain method, in this case, which is 'loseEnergy()', however, I could not find the solution.
How do I approach this problem?
What can be useful to know or to try.. in order to solve this issue?
Your help is very much appreciated!
Thank you!
Kind regards,
Segara
P.S. Some search I have done so far:
"How to call overriden methods in all derived classes"
"Collection of derived classes that have generic base class"
"How to call derived function using base class object"
"Call method of the derived class through reflection possible or no"
EDIT:
I decided to stick to the idea I had before which is to have some list that would contain the objects of the classes that have 'loseEnergy()' method. Having such list I will be able to call every object's method 'loseEnergy()', which is what I wanted.
Question can be closed.
Thank you.
I didn't really understand your problem but anyway i can try to give you some means to use abstract classes :
If you use a abstract method, you SHOULD override it in a subclasses (like a method declared in an interface)
If you want that all inherited class use a same method, you can implement it in the abstract class ; all subclasses will use the method you implements if you don't override it, you've have to not declare it in the subclasses (extends < ABS_CLASS > is good enough)
If you want use a method of the abstract class which is override in the sub class you can use the keyword super .
I hope it will help you.
if you mean that you want the calls: female.loseEnergy() -> human.loseEnergy() -> creature.loseEnergy(), call the base method in the first line of the overriden one
public class Female : Human
{
public override void loseEnergy()
{
base.loseEnergy();
// do stuff
}
}
In the Greenfoot environment that you mention in the post above, the act() method is called only on actors which have been added into the "world". Internally, this adds them into a list. The simulation process iterates through the list and calls act() on each object in turn. Objects that are not "in the world" are not known to the system and so do not have their act method called. There is no magic here going on here.
If you wanted similar behaviour but without manually adding objects into a list, you could possibly have the base class constructor add new objects into a global list. I don't know C# so I don't know precisely how to do this, but I cannot imagine it would be difficult.
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> {}
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
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.
}
}