class SomeClass {
public void someMethod(){}
public void otherMethod(){
//Calling someMethod()
}
}
Whats the difference when you call an instance method as:
--> someMethod(); OR this.someMethod();
vs
--> SomeClass.this.someMethod();
There is no difference from doing:
//...
public void otherMethod(){
someMethod();
}
//...
to doing
//...
public void otherMethod(){
this.someMethod(); // `this` in this case refers to the class instance
}
//...
Now if you would have
class SomeClass {
public static void someMethod(){}
public void otherMethod(){
//Calling someMethod()
}
}
you could do:
//...
public void otherMethod(){
SomeClass.someMethod(); // as the method is static you don't need to call it from an instance using `this` or omitting the class
}
//...
And lastly this syntax SomeClass.this.someMethod(); would not be correct in all scenarios. An example of where this could be used (correct) is as follow:
class SomeClass {
public void someMethod(){}
public void otherMethod(){
//Calling someMethod()
}
class OtherClass {
public OtherClass() {
// OtherClass#someMethod hides SomeClass#someMethod so in order to call it it must be done like this
SomeClass.this.someMethod();
}
public void someMethod(){}
}
}
Related
I have a class called TestingClass and I have a one more class called TestCase .
In TestingClass I have 4 methods which are dependent on each other, Can i call the this class file from my TestCase file and invoke all the methods at once?
Yes you can if they have visibility.
In your TestCase you have to declarate a variable of TestingClass and instantiate it. Then, if your methods are public you can use them.
Here is an example:
public class TestCase {
private TestingClass testingClass = new TestingClass();
#Test
public void test1(){
testingClass.methodFromTestCase();
}
}
You can try with extend the TestingClass and overwriting those methods as like,
public class TestCase extends TestingClass {
#Override
public void method1(){
super.method1();
}
#Override
public void method2(){
super.method2();
}
#Override
public void method3(){
super.method3();
}
#Override
public void method4(){
super.method4();
}
}
There is another way to do it, refer below example:
package overridefunction;
public class SuperClass
{
public void method1()
{
System.out.println("superclass method1");
this.method2();
this.method3();
this.method4();
this.method5();
}
public void method2()
{
System.out.println("superclass method2");
}
private void method3()
{
System.out.println("superclass method3");
}
protected void method4()
{
System.out.println("superclass method4");
}
void method5()
{
System.out.println("superclass method5");
}
}
package overridefunction1;
public class SubClass extends SuperClass
{
#Override
public void method1()
{
System.out.println("subclass method1");
super.method1();
}
}
package overridefunction1;
public class Demo
{
public static void main(String[] args)
{
SubClass mSubClass = new SubClass();
mSubClass.method1();
}
}
subclass method1
superclass method1
superclass method2
superclass method3
superclass method4
superclass method5
Why does the following code produce the output "in super", when the object's type is the sub class (OtherClass2) and the argument to the test function is of type Person2? Shouldn't the method call test(new Person2()); call the sub class's test function?
public class HelloWorld
{
public static void main(String[] args)
{
OtherClass2 s = new OtherClass2();
s.goToThing();
}
}
public class Person
{
}
public class Person2 extends Person
{
}
public class OtherClass
{
public void hello()
{
test(new Person2());
}
public void test(Person p)
{
System.out.println("in super");
}
}
public class OtherClass2 extends OtherClass
{
public void test(Person2 g)
{
System.out.println("In sub");
}
public void goToThing()
{
hello();
}
}
public void test(Person2 g)
of OtherClass2 does not override
public void test(Person p)
of OtherClass. It overloads it. However, it only overloads it for variables whose compile time type is OtherClass2 (since overloading is determined at compile time).
Therefore
test(new Person2());
invokes the method public void test(Person p) of the super class, since OtherClass has no method with the signature public void test(Person2 g) (which could have been overridden by the test method of OtherClass2).
Had you added an #Override annotation above public void test(Person2 g), the compiler would have told you that this method is not overriding any method of the super class.
Because your test method in OtherClass2 does not override test in OtherClass (it overloads).
Would you have
public class OtherClass2 extends OtherClass
{
public void test(Person g)
{
System.out.println("In sub");
}
public void goToThing()
{
hello();
}
}
it would work as expected.
See some further details and differences between overriding and overloading.
Because hello(); method is in OtherClass. You calls goToThing() which is in OtherClass2, after that OtherClass2 calls hello() method which in OtherClass, hello() method calls test() method from OtherClass. Try this:
public class OtherClass2 extends OtherClass
{
public void hello()
{
test(new Person2());
}
public void test(Person2 g)
{
System.out.println("In sub");
}
public void goToThing()
{
hello();
}
}
Can Someone tell me with an example why an class should be defined inside an interface.
The below is the simple code i was trying.
interface Watsapp
{
class A
{
public void Validate()
{
}
};
abstract public void SendText();
public void SendPic();
};
its totally depends on logic requirements.
whenever we declare inner class, it treats as a data member so here also you can treat this class as a data member
just assume scenario some one needs object of A inside Interface and there is no class right now.
see eg.
public interface Watsapp
{
class A
{
public void Validate()
{
}
public String iDoSomething()
{
return "i did";
}
};
public A objOfA = new A();
abstract public void SendText();
public void SendPic();
};
And main Class is bellow:
public class TestMain {
public static void main(String[] str){
System.out.println( Watsapp.objOfA.iDoSomething());
}
}
mostly people create anonymous class for one time use, but here You created a class with name.
see:
public interface Watsapp
{
/*class A
{
public void Validate()
{
}
public String iDoSomething()
{
return "i did";
}
};*/
Thread t = new Thread()
{
public void run() {
// something ...
}
};
abstract public void SendText();
public void SendPic();
};
Thank you.
For a project I want to provide a generic interface to resemble a workflow, like
public interface IWorkflow
{
public void start();
public void doWork();
public void end();
}
For that, I have lots of implementation, like
public class CoffeeWorkflow implements IWorkflow
{
public void start()
{
// setup coffee
// prepare dishes
// ...
}
public void doWork()
{
// drink coffee
}
public void end()
{
// wash dishes
}
}
Now I want to provide more information to those functions, like
public interface IWorkflowStartArgs
{}
And especially:
public class CoffeeWorkflowStartArgs implements IWorkflowArgs
To give it into the method
public interface IWorkflow
{
public void start(IWorkflowStartArgs args);
public void doWork();
public void end();
}
respectivly:
public class CoffeeWorkflow implements IWorkflow
{
public void start(CoffeeWorkflowStartArgs args)
{
}
}
But this does not work, as it is not recognized as implementation
of the interface.
Should I pass in an IWorkflowStartArgs and cast it inside?
Is there a better solution to that?
You can define interface like
interface IWorkflow<T extends IWorkflowStartArgs>
{
public void start(T args);
public void doWork();
public void end();
}
and when you create CoffeeWorkflow you can create something like
class CoffeeWorkflow implements IWorkflow<CoffeeWorkflowStartArgs>
{
#Override
public void start(CoffeeWorkflowStartArgs args) {
// TODO Auto-generated method stub
}
#Override
public void doWork() {
// TODO Auto-generated method stub
}
#Override
public void end() {
// TODO Auto-generated method stub
}
}
Java won't consider it as the specific implementation type.
Consider the following case, where you can see where the problem will occur (IF automatic mapping of implemented class in the function argument is seen valid by Java):
public class CoffeeWorkflow implements IWorkflow
{
public void start(IWorkflowStartArgs args)
{
// This is what Java sees as actual implementation
}
public void start(CoffeeWorkflowStartArgs args)
{
// This is yet again SEPARATE method with different signature
// In case of auto-casting (if there would have been), this method would be AMBIGUOUS
}
}
The solution?
Well use Generics as illustrated by #sanbhat
Or if you don't want to go into Generics,
Then I think you should pass in an IWorkflowStartArgs and cast it inside as you said first,
like this way:
public class CoffeeWorkflow implements IWorkflow
{
public void start(IWorkflowStartArgs args)
{
if (args instanceof CoffeeWorkflowStartArgs) {
CoffeeWorkflowStartArgs coffeeArgs = (CoffeeWorkflowStartArgs) args;
// ....
}
}
// ....
}
Suppose you have another similar class TeaWorkFlow,
then again you need to check instanceof.
This is why Generics were mainly introduced - To avoid repeatedly checking by instanceof;
And to serve as a general model for similar pattern classes.
There is a solution using a single generic parameter, that ensures type safety. Lets use the generic parameter WorkflowType for it:
interface IWorkflow<T extends WorkflowType>
{
public void start(IWorkflowStartArgs<T> args);
public void doWork(IWorkflowWorkArgs<T> args);
public void end(IWorkflowEndArgs<T> args);
}
You can now instantiate your generic parameter:
public class CoffeeWorkflowType extends WorkflowType {
}
Your CoffeeWorkflow looks like this:
public class Coffee implements IWorkflow<CoffeeWorkflowType> {
{
public void start(IWorkflowStartArgs<CoffeeWorkflowType> args);
public void doWork(IWorkflowWorkArgs<CoffeeWorkflowType> args);
public void end(IWorkflowEndArgs<CoffeeWorkflowType> args);
}
and the implementations for your workflow arguments:
public class CoffeeWorkflowStartArgs implements IWorkflowStartArgs<CoffeeWorkflowType> { ... }
public class CoffeeWorkflowWorkArgs implements IWorkflowWorkArgs<CoffeeWorkflowType> { ... }
public class CoffeeWorkflowEndArgs implements IWorkflowEndArgs<CoffeeWorkflowType> { ... }
Having issue in Java,
we can call class methods like
interface samp{
public void printMsg();
}
ClassA implements samp{
public void printMsg()
{
S.o.p("Hi ClassA");
}
}
ClassB implements samp{
public void printMsg()
{
S.o.p("Hi ClassB");
}
}
public MainClass{
public static void main(String args())
{
samp s= new ClassA();
s.printMsg();
samp s= new ClassB();
s.printMsg();
}
}
we can do this, am having different type of class method not similar methods for all classes but I want to implement the future is it possible to do? is any other pattern for this, pls help me to find this.
like
ClassA{
public void fun1(){..}
public void fun2(){..}
}
ClassB{
public void fun3(){..}
public void fun4(){..}
}
want to call these methods using a single refrence, need to asign object to that refrence dynamically is it possible friends?...
Thanks in advance
You cant do that using common interface.You can only call the method which is defined in interface using an interface reference type, even though the object it points to belong to another class have different other methods.
you can call only those class function which are defined in interface because its reference can access only those functions. ex:
interface samp{
public void printMsg();
}
ClassA implements samp{
public void printMsg()
{
S.o.p("Hi ClassA");
}
public void newmthd(){
S.o.p("you can't call me from samp reference.");
}
}
ClassB implements samp{
public void printMsg()
{
S.o.p("Hi ClassB");
}
}
public MainClass{
public static void main(String args())
{
samp s= new ClassA();
s.printMsg();
s.newmthd() //error... s don't have any knowledge of this function.
samp s= new ClassB();
s.printMsg();
}
}
Define all the methods you want your reference to have in an a superclass, but leave the implementations empty. Then, create your subclass and override the necessary methods.
Example:
Class MySuperClass {
public void fun1() {}
public void fun2() {}
public void fun3() {}
public void fun4() {}
}
Class ClassA extends MySuperClass {
public void fun1() { //implementation details }
public void fun2() { //implementation details }
}
Class ClassB extends MySuperClass {
public void fun3() { //implementation details }
public void fun4() { //implementation details }
}
public Class Tester {
public static void main(String[] args) {
MySuperClass class1 = new ClassA();
MySuperClass class2 = new ClassB();
}
}