I have the ParentClass, with method TestMethod (NOTE: I shouldn't make any changes to the ParentClass at all, it should stay as it is):
class ParentClass{
public static function TestMethod() {
return "original method";
}
}
And the child class that extends original class has two additional methods (ChildMethod1 and ChildMethod2) that are not included in the ParentClass:
class ChildClass extends ParentClass{
public function ChildMethod1() {
.
.
.
}
public function ChildMethod2() {
.
.
.
}
}
I'm instancing and working with an objects from ChildClass. But I need to override the TestMethod from the ParentClass.
In the moment when I will call the ChildMethod1, the TestMethod from the ParentClass should be overriden and to look like this:
public static function TestMethod() {
return "child method 1";
}
and in the moment when I will call the ChildMethod2, the TestMethod should be overridden like this:
public static function TestMethod() {
return "child method 2";
}
so probably the ChildClass should look like this:
class ChildClass extends ParentClass{
public function ChildMethod1() {
.
.
.
}
public function ChildMethod2() {
.
.
.
}
public function TestMethod() {
if(some condition){
return "child method 1";
} else {
return "child method 2";
}
}
}
NOTE: TestMethod in ParentClass is actually a static function.
Is there any way of implementing this logic without any change to the ParentClass? I was thinking maybe introducing the variable in ChildClass can help us in the creating the conditions in overriden class, but not sure how to implement all of that.
As far as I know, since a static method isn't part of a class instance, it can't be overridden. This is by design in every programming language in the world.
If these static methods were instance methods...
...probably you would end up implementing two derived class of ParentClass and that conditional logic would be implemented where you instantiate the so-called derived classes, and this means that you would be taking advantage of polymorphism:
ParentClass instance;
if([condition])
{
instance = new DerivedClass1();
}
else
{
instance = new DerivedClass2();
}
instance.OverriddenMethod();
}
When you cast a reference of a most derived instance into a less derived one, you're performing an upcast. While you're upcasting either DerivedClass1 or DerivedClass2, you're not losing their implementations of OverriddenMethod, because upcasting just reduces the typing without losing implementation details.
Related
I'm trying to access a private method from a static one in the same class in Dart.
class MyClass{
void _initFunc() {
/// ...
}
static void info(){
if (condition){
_initFunc();
}
}
}
I got this error
Instance members can't be accessed from a static method.
Can you please explain me why, and how can i do. info() must be static, and _initFunc() must be private.
There is no "this" in a static method. So MyClass.info() doesn't have a "this" to call this._initFunc();
In Dart the static modifier on class members and methods makes them available without creating an instance of a class object. For example special constructors are static because you want to create a instance with them. Consider MyClass foo = MyClass.fromAnotherObject(bar); -> static MyClass fromAnotherObject(){} is static because you don't yet have a MyClass object to call it on.
In your example code, you could change _initFunc() to a public function (remove the "_") and either:
a) instantiate a MyClass object inside your static info() method and call initFunc()
class MyClass{
void initFunc() {
/// ...
}
static void info(){
if (condition){
MyClass myClass = MyClass();
myClass.initFunc();
}
}
}
OR
b) declare initFunc() also static and call it from info()
class MyClass{
static void initFunc() {
/// ...
}
static void info(){
if (condition){
MyClass.initFunc();
}
}
}
An experimental/theoretical question concerning any OOP language (Java, C#, Typescript, etc..)
Usually, when implementing an interface in a class, one has to override all the functions of that interface in the class by having to write the code for each function from that interface. And every class that implements this interface has to have its own code for those functions. Suppose we have an interface called Test that has a function called foo() in which I want the following code System.out.println("This is an interface function");.Usually one has to make an interface and declare the function and then writes the code in every implementing class
But what if there is some "theoretical" way of having all of the function written in the interface and every implementing class can just call it without having to override. Something like the following (using Java as a base language):
interface Test {
public foo() {
System.out.println("Test log.")
}
}
class Class1 implements Test {
constructor() {}
}
class Class2 implements Test {
constructor() {}
}
class Main {
public static void main() {
Class1 f1 = new Class1();
Class2 f2 = new Class2();
f1.foo();
f2.foo();
}
}
Java has this feature since Java 8. Interfaces can have default implementations where the class is free to override them, but not required to do so.
interface Test {
default void foo() {
System.out.println("Test log.");
}
}
Single interface: IDoSomething {...}
Two classes implement that interface:
ClassA : IDoSomething {...}
ClassB : IDoSomething {...}
One class uses any of those classes.
public class DummyClass(IDoSomething doSomething) {...}
code without Autofac:
{
....
IDoSomething myProperty;
if (type == "A")
myProperty = new DummyClass (new ClassA());
else
myProperty = new DummyClass (new ClassB());
myProperty.CallSomeMethod();
....
}
Is it possible to implement something like that using Autofac?
Thanks in advance,
What you are looking for is, as I remember, the Strategy Pattern. You may have N implementations of a single interface. As long you register them all, Autofac or any other DI framework should provide them all.
One of the options would be to create a declaration of the property with private setter or only getter inside Interface then implement that property in each of the class. In the class where you need to select the correct implementation, the constructor should have the parameter IEnumerable<ICommon>.
Autofac or any other DI frameworks should inject all possible implementation. After that, you could spin foreach and search for the desired property.
It may look something like this.
public interface ICommon{
string Identifier{get;}
void commonAction();
}
public class A: ICommon{
public string Identifier { get{return "ClassA";} }
public void commonAction()
{
Console.WriteLine("ClassA");
}
}
public class A: ICommon{
public string Identifier { get{return "ClassB";} }
public void commonAction()
{
Console.WriteLine("ClassA");
}
}
public class Action{
private IEnumerable<ICommon> _common;
public Action(IEnumerable<ICommon> common){
_common = common;
}
public void SelectorMethod(){
foreach(var classes in _common){
if(classes.Identifier == "ClassA"){
classes.commonAction();
}
}
}
}
Suppose we have a default method in a interface,
in implementing class if we need to add some additional logic besides the one the default method already does we have to copy the whole method? is there any possibility to reuse default method... like we do with abstract class
super.method()
// then our stuff...
You can invoke it like this:
interface Test {
public default void method() {
System.out.println("Default method called");
}
}
class TestImpl implements Test {
#Override
public void method() {
Test.super.method();
// Class specific logic here.
}
}
This way, you can easily decide which interface default method to invoke, by qualifying super with the interface name:
class TestImpl implements A, B {
#Override
public void method() {
A.super.method(); // Call interface A method
B.super.method(); // Call interface B method
}
}
This is the case why super.method() won't work. As it would be ambiguous call in case the class implements multiple interfaces.
i was recently digging on new partial methods in c#3.0, i understood the use of partial class, that it could be chunked into multiple file one contain the definition and other declaration, but i wanted to know,i created a partial class like below:
in class1.cs
partial class A
{
partial void Method();
}
in class2.cs
partial class A
{
partial void Method()
{
Console.WriteLine("Hello World");
}
}
now in class3.cs
class MainClass
{
static void Main()
{
A obj = new A();
obj.Method(); //Here i cannot call the "Method" method.
}
}
then whats the use of creating partial method, i read on MSDN that, at runtime, compiler compiles the class into one, in that case compiler should be getting the "Method" method implementation also, then why it dont allow me to call the "Method" method in the main method, can anyone correct me if i am wrong, and tell me why i am unable to call this partial method in main.
From MSDN
No access modifiers or attributes are allowed. Partial methods are
implicitly private.
It's a private method, so you can't call it from main.
You can call a partial method inside the constructor where the method is defined.
For example
public partial class classA
{
partial void mymethod();
}
public partial class classA
{
partial void mymethod()
{
Console.WriteLine("Invoking partial method");
}
public ClassA()
{
mymethod();
}
}
public class MainClass
{
static void Main()
{
ClassA ca=new ClassA();
}
}
That's it..now execute your code and see the result..
OutPut
Invoking partial method
Yes, we can't call it from Main(). Problem is not Partial method problem is method without specifier in a class is Private and private method can be called inside the class only.
Try creating a new public method in Partial class:
partial class A
{
partial void Method();
}
partial class A
{
partial void Method()
{
Console.WriteLine("Hello World");
}
public void Study()
{
Console.WriteLine("I am studying");
Method();
}
}
class MainClass
{
static void Main()
{
A obj = new A();
obj.Study();
}
}