Dart: Inherit some of the public methods privately - flutter

I want to inherit a class, but I want to make some of its methods private, which allows mutation of the object. I do not want to override those methods, as that will make them still accessible and cause a runtime error. I want something like this:-
class BaseClass {
//...
void update() {/*Implementation*/}
void read() {/*Implementation*/}
}
class ChildClass extends BaseClass {
//...
void read() {/*Implementation*/}
}
int main() {
final first = BaseClass(), second = ChildClass();
first.update(); //Works
second.update(); //Error: Method does not exist.
}

I don't think what you want it possible. The entire point of extending classes is that child classes can access all public methods of the superclass. You might want to use composition instead. That would be something like:
class BaseClass {
//...
void update() {/*Implementation*/}
void read() {/*Implementation*/}
}
class ChildClass {
BaseClass _baseClass
//...
void read() { _baseClass.read() }
}

Related

Accessing private method from a static one

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();
}
}
}

Kotlin abstract method with body

As mention here: if a function in an interface has no body it is abstract by default. But there is nothing about interface's function with body.
Example:
interface MyInterface {
fun foo() { print("Something") }
fun bar()
}
fun main(args: Array<String>) {
println(MyInterface::foo.javaMethod)
println(MyInterface::bar.javaMethod)
}
Output will be:
public abstract void MyInterface.foo()
public abstract void MyInterface.bar()
How it's possible, that method with defined body is abstract?
This has to do with the way default methods in Kotlin interfaces are implemented. The foo and bar methods in your interface really are both abstract.
However, there is an inner class inside the interface that looks something like this (simplified):
public interface MyInterface {
void foo();
void bar();
public static final class DefaultImpls {
public static void foo() {
System.out.print("Something");
}
}
}
This class is the one that contains the default implementations of any functions that you gave a body to inside the interface.
Then, if you create a class that implements this interface, and you don't override the foo method:
class MyClass: MyInterface {
override fun bar() {
println("MyClass")
}
}
Then you get one generated automatically, which just calls the implementation inside DefaultImpls:
public final class MyClass implements MyInterface {
public void bar() {
System.out.println("MyClass");
}
public void foo() {
MyInterface.DefaultImpls.foo();
}
}
You can find all these details by using the bytecode viewer that comes with the Kotlin plugin (Tools -> Kotlin -> Show Kotlin Bytecode, and then the Decompile option).

Java8 overriding (extending) default method

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.

Junit using Mock Objects

MyClass{
public void myfunction(){
AnotherClass c=new AnotherClass();
c.somethod();//This method sets some values of the AnotherClass object c;
}
}
I have the above scenario to be tested.How do I check if the value of AnotherClass Object c is set properly.I understand that I have to use Mock Objects for these.But couldn't figure out how because here I cannot pass the mock object of AnotherClass to myfunction because of the above design.Can anybody help me?
if you really want to do this should do a redesign like follows (as Dan has also suggested)
import org.junit.Test;
import org.mockito.Mockito;
public class TestingMock {
#Test
public void test() {
MyClass target = Mockito.spy(new MyClass());
AnotherClass anotherClassValue = Mockito.spy(new AnotherClass());
Mockito.when(target.createInstance()).thenReturn(anotherClassValue);
target.myfunction();
Mockito.verify(anotherClassValue).somethod();
}
public static class MyClass {
public void myfunction(){
AnotherClass c = createInstance();
c.somethod();//This method sets some values of the AnotherClass object c;
}
protected AnotherClass createInstance() {
return new AnotherClass();
}
}
public static class AnotherClass {
public void somethod() {
}
}
}
You will see that commenting out c.somethod() makes the test fail.
I'm using Mockito.

Calling Partial Methods in C#

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();
}
}