How to select a specific class from set of classes which are implementing same interface class? - interface

How to select a specific class from set of classes which are implementing same interface class ?

You don't call a method directly from an interface, you call it on a reference that points to an instance of a class. Whichever class that is determines which method gets called.

Related

Which is the difference beetwen implement a object of interface type or object of class type?

I have seen is possible to instantiate an object who implements an interface instead of a class. I´d like to know what are the benefits of this.
I attach an example :
Interface just tells the program, that this object it just received has these methods defined. So you can have multiple different classes implement the same interface. And when a method accepts a given interface, then it can accept all the classes that implement the interface.
By instansiating a class into interface, you are saying, that whatever following code does, it cares only about the methods stated in the interface.
If you instansiate into class type, you are saying you want only this spesific type and none else.

Why main method is written only in object not class?

How does a main method gets called in scala ? Why does a main method gets called in when it is written only in object but not in class ?
Because the specification says so:
A program is a top-level object that has a member method main of type (Array[String])Unit. Programs can be executed from a command shell. The program's command arguments are passed to the main method as a parameter of type Array[String].
The main method of a program can be directly defined in the object, or it can be inherited.
It speaks only about top-level objects, not classes. If you define a main method in a class, then it will be just an ordinary method that you can invoke on the instances of this class. Unless you define a top-level object that inherits the main from this class / trait, this method called main will not be treated as an entry point of the application.
The main method must be a static method. In Scala to create a static method you put it in an object. Methods in a class are not static.
In the scala language they decided to separate class, which hold only instance behavior and state, and object which hold static behavior and state. This is different from java where classes hold both instance and static members where something is made static using the static keyword.
It is because in scala the only way to define a method static is object class. And also it is necessary only one instance of main class is created , not multiple instances. That's why it is object class

Interface or class as method (or constructor) parameter

Why do I have class or interface passed as a method parameter in a class? I don't get that concept. For example:
Declaration:
public void doSomething (Class yourClass){}
Calling the method:
doSomething(yourClass);
What is the benefit? Is there an alternative? I can't call methods for yourClass anyway, for example: doSomething(yourClass.someMethod()) or doSomething(yourClass) and then yourClass.someMethod() are both invalid.
There's several possibilites, depending on the particular language. doSomething(Class) could...
Instantiate and return a Class generator.
Create an object or service to track or otherwise interact with one or more Class objects.
Return useful information about Class (e.g. via reflection).

Is singleton class equal to a class with static method

I created a Class A in which all the methods are class methods (+). Another Class B is a singleton.
I want to know if I can check if Class A [A someoperation] Is like class B in that only one instance of A exists and I do not need to instantiate it.
How can I accomplish this?
When you call a class method, the class is not necessarily instantiated, unless the class method actually creates a class.
Also - class methods do not make a class a Singleton. It just means that the method is called on the class instead of an objet of the class.

iphone, calling a method of different class in cocos2d layers?

I have two classes, both are subclasses of CCLayer,
I want to call a method of first class into second class, what should I code?
Your question is not providing much detail, but from my understanding of what you say, you need the following:
a selector in the public interface of your first class;
a pointer ivar in the second class that you will properly initialize so that it points to an instance of the first class;
In this way you will be able to call the first class' method from the second class.