I have been asked this question in one of my interview.
Lets say I have base class "A". There are four classes "B","C","D","E" those are derived from "A". Now I have one method which is common in class "D" and "E". What is the minimum way that I can write this method with which only class "D" and "E" can use them.
If I write them in base class then it will be visible to class "B" and "C" without reason.
You derive an intermediate class from A say call it for simplicity DECommon which will have this common method for D and E. Next derive D and E from it.
class A {
}
class DECommon : public A {
public:
virtual void newMethodDE() {
// DO YOUR MAGIC HERE
}
}
class D : public DECommon {
}
class E : public DECommon {
}
Hence D and E still will preserve is-a relationship to A, while logic of new common method will be implemented only once in DECommon intermediate class.
Related
abstract class MyClass() {
protected static foo: Array<number>;
protected static doWorkOnFoo(): void {
let x: number = 0;
for (let f of | what goes here? this? self?|.foo) {
x = x + foo;
}
}
}
When implementing an abstract class, and wanting derived classes to have a static property and a static method that operates on those properties, how would one access those in the abstract class so that the derived class can just use that method?
I know this can be worked around by just setting a default value on the static property and using this, but this sparked my interested and I'm curious to know if there's some way to access generic derived class or something from an abstract class in TS.
Thanks in advance!
EDIT:
While I wasn't able to find exactly what I was looking for (see comments), a workable solution is to change the signature of the doWorkOnFoo() method to the following:
protected static doWorkOnFoo(): (typeof MyClass) => void;
Since it is already an abstract class it can take a derived class as an argument and then reference the derived class's static properties.
I'm trying to make a Script# import library to wrap (parts of) the dojo toolkit--especially the dijit widgets. Unfortunately, dojo uses multiple inheritance, and C# doesn't support that (except for interfaces, which Script# doesn't handle properly--see below).
I'm trying to accomplish something like this:
[Imported]
public class A
{
public void Foo() {}
}
[Imported]
public class B
{
public void Bar() {}
}
[Imported]
public class C : A, B
{
public void Sproing() {}
}
but obviously that's not valid C#, and therefore isn't valid Script#.
Is there a way in Script# to accommodate multiple inheritance of [Imported] classes? I tried using interfaces, since C# supports multiple inheritance of them and I'm not providing implementations anyway:
[Imported]
public interface A
{
void Foo();
}
[Imported]
public interface B
{
void Bar();
}
[Imported]
public interface C : A, B
{
void Sproing();
}
however, when I try to use the library from another Script# project, code such as C c = null; c.Foo(); I just get a "Check that your C# source compiles and that you are not using an unsupported feature. Common things to check for include use of fully-qualified names (use a using statement to import namespaces instead) or accessing private members of a type from a static member of the same type." error on the c.Foo() call.
Any other ideas? The [Mixin] attribute doesn't appear to do what I need either.
The only other option I see at the moment (aside from fixing the interfaces problem in Script#, which I'm not prepared to do) is to ditch inheritance altogether and put all the
"inherited" members in each leaf class. That would look something like this:
[Imported]
public class A
{
public void Foo() {}
}
[Imported]
public class B
{
public void Bar() {}
}
[Imported]
public class C
{
public void Foo() {}
public void Bar() {}
public void Sproing() {}
}
Obviously that would get ugly fast, but I could automate it. Since JavaScript's type system is pretty fast-and-loose anyway, this might even work OK there. And in Script# land, consumers of the import library would simply need to do more explicit casts than they should need to do. Are there other disadvantages that I'm overlooking?
Interface inheritance isn't currently supported. It will be fixed in a future rev.
You could define:
interface A {
}
interface B {
}
class C : A, B {
}
It does mean you'll end up having to define all members, even if stubs in C.
I haven't looked at Dojo in any depth, but potentially a better strategy will be to have a base class with methods shared across many of the widgets, and then derived widget types for each individual widget type. That would be something similar to the jQueryUI stuff that is in the script# repository.
I am trying to map a class hierarchy which looks like this:
public abstract class A { }
public class B : A { }
public class C : A { }
I don't want to map class A because it is abstract, I know I can do:
.IgnoreBase<A>()
to not map A and map all properties of A in B and C. But my problem is that I also have another class D which looks like following:
public class D {
public virtual A a { get; set; }
}
Now, when I try to map with fluent nhibernate auto mapping feature I get an error that class D refers to an unmapped class A, though class A is actually mapped through subclasses B and C.
Anyone know how to solve this?
If you don't map class A the classes B and C won't be subclasses. They are just two classes that are not connected at all. NHibernate knows nothing about class A, so how should NHibernate know how to handle references to class A?
Not mapping A because it's abstract is no reason. You can map interfaces too.
Maybe it would be more clear what you want to do if you show us your DB model (tables).
Please see the code below :
package bk;
public class A {
protected void methodA() {
System.out.println("Calling the method A !");
}
}
// And I have an another package :
package com;
import bk.A;
public class B extends A {
public void methodB() {
System.out.println("Goi phuong thuc B !");
}
public static void main(String[] args) {
A a = new B();
a.methodA();
}
}
How can I allow a to call methodA()?
Cause methodA() is protected and it can be called within derived classes only. Change it to public if you want to call it like this
Protected methods can only be called from within the class itself, or from derived classes.
The a variable is declared as a variable of type A. Class A itself has no publicly available methodA, so you cannot call it.
Yes, you assign a new B instance to the a variable and the a.methodA() statement is inside the derived B class, but the compiler only sees that a is of type A. It could be any other subclass of A as well, in which case you still wouldn't have access to methodA.
You'll have to tell the compiler that the a variable is actually of type B. Then you will be able to call methodA, because you're calling it from within class B.
B a = new B();
You are trying to access methodA() like it is public. Declaring simply methodA() in the B class is fine, but you cannot do a.methodA().
Conversely if it wasn't a method and simply protected int a;
you could do
a = 1; in class B
but
A a = new A();
a.a = 1;
is not legal
A protected method is visible to inheriting classes, even not part of the same package. A package scope (default) method is not. That is the only difference between protected and package scope.
The theory is that someone extending your class with protected access knows more about what they are doing than someone who is merely using it with public access. They also need more access to your class’s inner workings. Other than that, protected behaves like default package access.
I'm reading this article about how JVM invokes methods, and I think I got most of it. However, I'm still having trouble understanding the need for invokeinterface.
The way I understand it, a class basically has a virtual table of methods and when calling a method with either invokevirtual or invokeinterface this virtual table is consulted.
What is the difference, then, between a method that's defined on an interface and a method defined on a base class? Why the different bytecodes?
The description of the instructions also looks very similar.
The article seems to claim that the method table of an interface can have "different offsets" every time a method is called. What I don't understand is why an interface would have a method table at all, since no object can have the interface as its actual type.
What am I missing?
Each Java class is associated with a virtual method table that contains "links" to the bytecode of each method of a class. That table is inherited from the superclass of a particular class and extended with regard to the new methods of a subclass. E.g.,
class BaseClass {
public void method1() { }
public void method2() { }
public void method3() { }
}
class NextClass extends BaseClass {
public void method2() { } // overridden from BaseClass
public void method4() { }
}
results in the tables
BaseClass
1. BaseClass/method1()
2. BaseClass/method2()
3. BaseClass/method3()
NextClass
1. BaseClass/method1()
2. NextClass/method2()
3. BaseClass/method3()
4. NextClass/method4()
Note, how the virtual method table of NextClass retains the order of entries of the table of BaseClass and just overwrites the "link" of method2() which it overrides.
An implementation of the JVM can thus optimize a call to invokevirtual by remembering that BaseClass/method3() will always be the third entry in the virtual method table of any object this method will ever be invoked on.
With invokeinterface this optimization is not possible. E.g.,
interface MyInterface {
void ifaceMethod();
}
class AnotherClass extends NextClass implements MyInterface {
public void method4() { } // overridden from NextClass
public void ifaceMethod() { }
}
class MyClass implements MyInterface {
public void method5() { }
public void ifaceMethod() { }
}
This class hierarchy results in the virtual method tables
AnotherClass
1. BaseClass/method1()
2. NextClass/method2()
3. BaseClass/method3()
4. AnotherClass/method4()
5. MyInterface/ifaceMethod()
MyClass
1. MyClass/method5()
2. MyInterface/ifaceMethod()
As you can see, AnotherClass contains the interface's method in its fifth entry and MyClass contains it in its second entry. To actually find the correct entry in the virtual method table, a call to a method with invokeinterface will always have to search the complete table without a chance for the style of optimization that invokevirtual does.
There are additional differences like the fact, that invokeinterface can be used together with object references that do not actually implement the interface. Therefore, invokeinterface will have to check at runtime whether a method exists in the table and potentially throw an exception.
Comparing both instructions in the JVM Spec, the very first difference is that invokevirtual checks the accessibility of the method during the lookup, while invokeinterface doesn't.