Overriding inner class' method in Interface - java

I have an interface in which I declare an inner class:
public interface myInterface
{
int count();
public class myInterfaceInnerClass
{
public void testOverride()
{
}
}
}
And class Test implements myInterface:
public class Test implements myInterface
{
public Test()
{
// TODO Auto-generated constructor stub
}
public int count()
{
return 1;
}
//Here I would like to be able to override the inner class' method testOverride
}
Is it possible to override the inner class' method testOverride in class Test?
EDIT:
In response to comment made by O. Charlesworth, I came up with the following:
public interface myInterface
{
myInterface aInterface = new myInterfaceInnerClass();
int count();
public class myInterfaceInnerClass implements myInterface
{
#Override
public int count()
{
// TODO Auto-generated method stub
return 0;
}
public void testOverride()
{
}
}
}
In class Test:
public class Test implements myInterface
{
public Test()
{
// TODO Auto-generated constructor stub
}
public int count()
{
return 1;
}
}
Is it still possible to override the inner class' method testOverride
here in class Test?

Here's something you can do:
import myInterface.myInterfaceInnerClass;
public class Test extends myInterfaceInnerClass implements myInterface {
#Override public int count() {
// ...
return 0;
}
#Override
public void testOverride() {
// ...
}
}
Reason for extending class myInterfaceInnerClass (A picture > a million words):
(source: gyazo.com)

Related

Is it legal and defined to pass a method reference to a non-overriding derived class method to its base class?

Is it legal and defined to pass a method reference to a non-overriding derived class method to its base class?
public class Base {
private Supplier<Int> intSupplier;
public Base(Supplier<Int> intSupplier) {
this.intSupplier = intSupplier;
}
public Int getInt() {
return inSupplier.get();
}
}
.
public class Derived extends Base {
public Derived() {
super(this::returnsOne);
}
private Int returnsOne() { return 1; }
}
.
assert(1 == new Derived().getInt())
Why not just do normally ?
public class Derived extends Base {
#Override
public Int getInt() {
returnsOne();
}
private Int returnsOne() { return 1; }
}

How to write a shared code between two classes

In the below code, in class_1 and class_2 both extends from AbstractClass. I am trying when I call:
c1.setValid(5)
The following two lines, returns 5 as well:
System.out.println(c1.getValid());
System.out.println(c2.getValid());
pleas let me know how can I modify the Super class to achieve that.
main:
public class Main {
public static void main (String[] args) {
Class_1 c1 = new Class_1();
Class_2 c2 = new Class_2();
System.out.println(c1.getValid());
System.out.println(c2.getValid());
c1.setValid(5);
System.out.println(c1.getValid());
System.out.println(c2.getValid());
}
}
class_1
public class Class_1 extends AbstractClass {
public Class_1() {
// TODO Auto-generated constructor stub
}
public void setValid(int v) {
SetValid(v);
}
public int getValid() {
return GetValid();
}
}
class_2.:
public class Class_2 extends AbstractClass {
public Class_2() {
// TODO Auto-generated constructor stub
}
public void setValid(int v) {
SetValid(v);
}
public int getValid() {
return GetValid();
}
}
code:
public abstract class AbstractClass {
public int isValid = -1;
public void SetValid(int value) {
this.isValid = value;
}
public int GetValid() {
return this.isValid;
}
You can delete setValid and getValid from Class 1 and 2
and use the parent's function SetValid and GetValid
If that is your question.
If your goal is to have all instances of your class to always have the same value, then you can simply use the static keyword.
public abstract class AbstractClass {
public static int isValid = -1;
...
}
When static is used for a global variable, you're essentially declaring that this global variable will be shared by every instance of this class, including it's children.
Therefore, updating isValid with a new value in Class_1 will cause Class_2 to have the same value that Class_1 updated.

Java - How to call method class with interface without know class name

I'm new in java, I want to call method class from implemented Class with interface without know class name "ClassA", which only know Object c and I have 2 file.
File (1) CobaInterface.java
package cobainterface;
public class CobaInterface {
public static void main(String[] args) {
ImplementedClass implementedClass = new ImplementedClass();
ClassA clsA = new ClassA();
implementedClass.myMethodFromClassA(clsA);
}
}
class ClassA{
public Integer getTwo(){
return 2;
}
}
interface MyInterface {
public void myMethod();
//here interface
public void myMethodFromClassA(Object c);
}
File (2) : ImpementedClass.java
package cobainterface;
public class ImplementedClass extends CobaInterface {
public void myMethodFromClassA(Object c) {
//System.out.println(c.getTwo()); <- wrong when call method c.getTwo()
}
}
How about if I want to call method getTwo() from ClassA without know Class Name, which only know Object c from file (2) as describe in code above. Thanks for advance.
You should use generic types so the implementation knows what the object will be,
interface MyInterface<T> {
public void myMethod();
//here interface
public void myMethodFromClassA(T c);
}
The impl becomes,
package cobainterface;
public class ImplementedClass Implements MyInterface<ClassA> {
public void myMethodFromClassA(ClassA c) {
//System.out.println(c.getTwo()); <- wrong when call method c.getTwo()
}
}
All together,
class Scratch {
public static void main(String[] args) {
ImplementedClass implementedClass = new ImplementedClass();
ClassA clsA = new ClassA();
implementedClass.myMethodFromClassA(clsA);
}
}
class ImplementedClass implements MyInterface<ClassA> {
#Override
public void myMethod() {
}
#Override
public void myMethodFromClassA(ClassA c) {
System.out.println(c.getTwo());
}
}
class ClassA {
public Integer getTwo() {
return 2;
}
}
interface MyInterface<T> {
void myMethod();
void myMethodFromClassA(T c);
}
You could also do a cast
System.out.println((MyClass)c.getTwo());
but you will lose all benefit of type saftey.

How to implement nested interface method whose return type is Outer interface?

I have an OuterInterface that has nested InnerInterface. OuterInterface has method called GetInnerInterfaceReference(). Return type for this method is InnerInterface.
interface OuterInterface {
interface InnerInterface {
public void InnerInerfaceMethod();
}
public InnerInterface GetInnerInterfaceReference();
}
Now it is implemented in Example class. So, GetInnerInterfaceReference() method should return reference to InnerInterface type variable. How to achieve this?
class Example implements OuterInterface,OuterInterface.InnerInterface {
public void InnerInerfaceMethod() {
System.out.println("inner method called");
}
public InnerInterface GetInnerInterfaceReference() {
//return
}
}
Once I get reference, then I can use following code to call method inside the InnerInterface
public class Sample {
public static void main(String[] args) {
Example ex = new Example();
ex.GetInnerInterfaceReference().InnerInerfaceMethod();
}
}
I can get results simply by calling ex.InnerInterfaceMethod();
But, I got this query when I was going through WebDriver interface. This WebDriver has some static nested interfaces and some method declarations. Some of these methods return types are these nested interfaces. So I wonder how these methods are defined in the implemented classes like in ChromeDriver or FirefoxDriver?
like this:
class Example implements OuterInterface,OuterInterface.InnerInterface {
public void InnerInerfaceMethod() {
System.out.println("inner method called");
}
public InnerInterface GetInnerInterfaceReference() {
return this;
}
}
Something Like This:
public interface OuterIntf {
public interface InnerIntf {
public OuterIntf someInnerMethod();
}
public InnerIntf someOuterMethod();
}
public class Implementation implements OuterIntf, InnerIntf{
/* (non-Javadoc)
* #see practise.OuterIntf.InnerIntf#someInnerMethod()
*/
#Override
public OuterIntf someInnerMethod() {
// TODO Auto-generated method stub
return this;
}
/* (non-Javadoc)
* #see practise.OuterIntf#someOuterMethod()
*/
#Override
public InnerIntf someOuterMethod() {
return this;
}
}

How to find how many testcase are there in TestNG class from another java class

I have two class
public class xyzTest {
#Test
public void TestP1TUNG557(){
TestHelper.excuteTestcase(TUNG557);
Assert.assertTrue(TestHelper.TestResult);
}
#Test
public void TestP1TUNG559(){
TestHelper.excuteTestcase(TUNG559);
Assert.assertTrue(TestHelper.TestResult);
}
#Test
public void TestP0TUNG558(){
TestHelper.excuteTestcase(TUNG558);
Assert.assertTrue(TestHelper.TestResult);
}
}
public class TestHelper {
public excuteTestcase(String abc)
{
process(abc)
}
int TotalTescase(String pattern, Class testNGclass)
{
How to write here..? plz help
}
}
suppose if have called TotalTescase(String TestPO, Class xyzTest), it should return 1 and if have called TotalTescase(String TestP1, Class xyzTest) it should return 2.
If This is possible to get total test case like this ,plz help me or provide me some link
I have searched but i couldnt find. help me
You can use reflection technique to find out the matching methods in the supplied class like:
public int TotalTescase(String pattern, Class<?> testNGclass) throws ClassNotFoundException
{
int count = 0;
testNGclass.getClass();
Class<?> className = Class.forName(testNGclass.getName());
Method[] methods = className.getMethods();
for(int i=0; i<methods.length; i++)
{
String methodName = methods[i].getName();
System.out.println("Method Name: "+methodName);
if(methodName.contains(pattern))
{
count++;
}
}
return count;
}
In dry run try to implement IInvokedMethodListener and override beforeInvocation method
public class Test implements IInvokedMethodListener
{
static int testcount=0;
public void beforeInvocation(IInvokedMethod method, ITestResult testResult) {
testcount=testcount+method.getTestMethod().getInvocationCount();
}
#Override
public void onStart(ISuite suite) {
// TODO Auto-generated method stub
}
#Override
public void onFinish(ISuite suite) {
System.out.println(testcount);
}
}

Categories

Resources