Here is a Java question
How can I implement the inner class's interface in outer class?
I try the following, but in vain. Thank You
class A implements interface B.C{
static class B{
interface C{
}
}
}
I would do like this :
Assuming both the classes are in same package with proper imports.
public class Nestedinterface {
public interface NestI{
void show();
}
}
public class NestedinterfaceImpl implements NestI {
public static void main(String a[]) {
NestI n = new NestedinterfaceImpl();
n.show();
}
public void show() {
// TODO Auto-generated method stub
System.out.println("Hello world");
}
}
Related
I am new to this concept, i know the property of interface and abstract.
when i explain the concept to my friends, they asked me to create abstract class inside the interface.
please tell me , is it possible to create abstract class inside the interface.
i googled, but i am not able find the exact answer for my question.
i tried the below code ,but i dont know how to cal the AbstractMethod.
interface Student {
public abstract class Subject {
public void AbstractMethod(){
System.out.println("hi");
}
}
}
class Data implements Student {
public void ClassMethod() {
System.out.println("method 2");
}
}
public class NewClass {
public static void main(String[] args) {
Data s=new Data();
Student.Subject obj=new Student.Subject();// compiler error
s.ClassMethod();
}
}
Wouldn't something like this be better?
interface Student {
public abstract void sayHi();
}
class Data implements Student {
#Override
public void sayHi() {
System.out.println("method 2");
}
}
Yes, you can. Here in the below example, I used Anonymous Class but you can use Lambda Expression also
interface Student {
public abstract class Subject {
public abstract void AbstractMethod();
public void show(){
System.out.println("Show Method");
}
}
}
class Data implements Student {
public void ClassMethod() {
System.out.println("method 2");
}
}
public class NewClass {
public static void main(String[] args) {
Data s=new Data();
Student.Subject obj=new Student.Subject(){
public void AbstractMethod(){
System.out.println("hi");
}
};
obj.show();
obj.AbstractMethod();
s.ClassMethod();
}
}
How to use a non-abstract method from an abstract class in another class without extending?
Abstract Class:
package com.test;
public abstract class MyAbstract {
public abstract void abstractMethod();
public void callNonAbstractMethod() {
System.out.println("Hello");
}
}
The anonymous class:
package com.test;
public class Example {
public static void main(String[] args) {
new Example().something();
}
void something() {
MyAbstract a = new MyAbstract() {
#Override
public void abstractMethod() {
//TODO implement
}
};
a.callNonAbstractMethod();
}
}
Internal class generated by the compiler would be for above example.
static class Example extends MyAbstract
{
Example(){}
void abstractMethod()
{
System.out.println("hiee");
}
}
Abstract Class:
package com.test;
public abstract class MyAbstract {
public abstract void abstractMethod();
public void callNonAbstractMethod() {
System.out.println("Hello");
}
}
You can't.
To call any non-static method of some class A, you need an instance of A or of a subclass of A, as such a method typically operates on data within such an instance. That's at the very core of what "object-oriented" is all about.
In your case, A is abstract and can't have direct instances. So the only way to call your method is to have some instance of some class B that extends A. You can either find an existing subclass that you can use, or create your own subclass.
I think you can use an anonymous class. Although it is a kind of extension, you are not explicitly using the keyword extends. In fact, you cannot use any class in java without implicitly extending because every class extends Object.
package com.test;
public class Example {
public static void main(String[] args) {
new Example().something();
}
void something() {
MyAbstract a = new MyAbstract() {
#Override
public void abstractMethod() {
//TODO implement
}
};
a.callNonAbstractMethod();
}
}
and here's your abstract class:
package com.test;
public abstract class MyAbstract {
public abstract void abstractMethod();
public void callNonAbstractMethod() {
System.out.println("Hello");
}
}
results in:
Hello
interface Y {
void search(String name);
}
class A implements Y {
void search(String name) {
//Is it possible to say: "If I was called from class B then do a search("B");
}
}
class B extends A {
}
public class Main {
public static void main(String[] args) {
B b = new B();
b.search();
}
}
Given the above code is it possible to reason in superclass which subclass was used for calling a method?
The reason I want to do this is because the code in Search is very similar for all Subclasses, the only thing that changes is the Classname, so I thought there is no need to Override in each subclass. I have updated the code to reflect this. Please let me know if there is a better way of doing it/
Calling this.getClass() inside your search method will give you the concrete class of the current instance.
For example:
class Example
{
static class A {
public void search() {
System.out.println(getClass());
}
}
static class B extends A {}
public static void main (String[] args) throws java.lang.Exception
{
new A().search();
new B().search();
}
}
outputs
class Example$A
class Example$B
The cleanest way to do it is to override the method in each subclass.
interface Y {
void search();
}
class A implements Y {
public void search(){
search("A");
}
protected void search(String name) {
// implement your searching algoithm here
}
}
class B extends A {
public void search(){
search("B");
}
}
public class Main {
public static void main(String[] args) {
B b = new B();
b.search();
}
}
That's the way inheritance is suppose to works. A super class should not know its subclasses.
And, in case you extends your class B, you can easily either:
-Keep the same behaviour as B:
class C extends B {
// do nothing, when calling search, it calls the method implemented in B
}
-Change the behaviour to search for "C"
class C extends B {
public void search(){
search("C"); // or search("whateveryouwant")
}
}
You can simply override the method in class B.
The other way could be to write the search() method as
void search() {
if (this.getClass().equals(B.class)) {
//The logic for B
} else if (this.getClass().equals(A.class)) {
//The logic for A
}
}
You have to provide the fully qualified name for the class.
Better follow template pattern.
interface Y {
void search(String name);
}
abstract class AbstractionTemplate implements Y{
#Override
public void search(String name) {
//a lot of code.
System.out.println("common stuff start");
doImplspecificStuffOnly();
System.out.println("common stuff end");
//a lot of code.
}
abstract void doImplspecificStuffOnly();
}
class A extends AbstractionTemplate{
#Override
void doImplspecificStuffOnly() {
System.out.println("a's stuff");
}
}
class B extends A {
#Override
void doImplspecificStuffOnly() {
System.out.println("B's stuff");
}
}
public class Main {
public static void main(String[] args) {
B b = new B();
b.search("hey");
}
}
I have a class in jar of which I want to invoke a method. But that method has parameter of abstract class and that abstract class is inner method of class in jar. AbstractClassA is a HIDDEN class. Here is code:
public class A{
private invokeThisMethod(AbstractClassA object){
}
public abstract class AbstractClassA {
public void update(int remaining){}
}
}
public class myClass{
//using Reflection get object of class A
objectOfClassAusingReflection.inovke("invokeThisMethod", params)
}
Problem here is how do I create concrete implementation of AbstractClassA to pass in invoke method and get update method callbacks ?
Something like this should work:
AbstractClassA a = new AbstractClassA() {
public void update(int remaining) {... do something...}
};
objectOfClassAusingReflection.inovke("invokeThisMethod", a);
You cannot create an instance of abstract class or any interface at runtime.
Instead create an anonymous class for this.
public abstract class A {
public void fun(){....}
public abstract void absFun();
}
public class MyClass {
objectOfClassA = new A(){
public void absFun(){...}
}
}
Or you can first create implementation for that abstract classes for which you will have to create another class extending A
class AWrapper extends A {
public class ImplementationClassA extends AbstractClassA {
// override abstract functions...
}
}
Now you can use this Awrapper class
AWrapper wrapperObj = new AWrapper();
A obj = wrapperObj; // just to make it clear that A can hold wrapperObj as it is implementation of it.
A.AbstractClassA absObj = wrapperObj.new ImplementationClassA();
...
objectOfClassAusingReflection.inovke("invokeThisMethod", params)
Below code should work--
Here, i used anonymus classes for both outer and inner class and then with the help of getdeclatedMethod called your update method.
"TestAbs" is your jar class--
public abstract class TestAbs {
private void invokeThisMethod(AbstractClassA object) {
}
public abstract class AbstractClassA {
public void update(int remaining) {
}
}
}
Then calling your jar class from "TestAbs1" like below--
public class TestAbs1 {
public static void main(String[] args) {
TestAbs.AbstractClassA abs = new TestAbs() {
AbstractClassA a = new AbstractClassA() {
public void update(int remaining) {
System.out.println("Inside update method : " + remaining);
}
};
}.a;
try {
int i = 1;
Class<?> class1 = Class.forName("app.test.mytest.TestAbs$AbstractClassA"); -- (*Getting instance of inner class*)
System.out.println(class1.getDeclaredMethod("update", int.class));
class1.getDeclaredMethod("update", int.class).invoke(abs, i);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
The output i got is --
public void app.test.mytest.TestAbs$AbstractClassA.update(int)
Inside update method : 1
Answer to your Comment:-
What I understood from your comment is that, you wanted to call method from abstractClass which is hidden in outerclass.
As per my understanding, there is one way like below--
public abstract class TestAbs {
private void invokeThisMethod(AbstractClassA object) {
}
private abstract class AbstractClassA { --- your hidden class
public void update(int remaining) {
}
}
public class ImplementedClass extends AbstractClassA{ -- use implemented class here
....
...
}
}
And after that, use your ImplementedClass the same way mentioned above.
You can find reference example for private inner class here from java docs.
Note: In your question context, since your inner class and outer class is in jar, so I think it is difficult for you add implementation class in your jar.
In case, you find any alternatives, please let all knows about this;
thanks.
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();
}
}