what is the difference between creating an Object of interface and implementing an interface
example :
public interface A{
public void testMethod();
}
on way is creating an object of interface
public class B{
A a = new A(){
#override
public void testMethod(){ //implemtation here }
};
}
other way is
public class B implements A
{
#override
public void testMethod(){}
}
You are wrong:
here you anonymously implement interface and you alrady have instance of annonymouse class
public class B{
A a = new A(){
#override
public void testMethod(){ //implemtation here }
};
}
Here you create named implementation, you only create class without instantiate it.
public class B implements A
{
#override
public void testMethod(){}
}
You can't create an object of interface. Interface it's an abstract class but with all the methods are abstract. In the first code you are creating an anonymous class (i recommend you to read about this feature in java) that implements the interface A, in this case you are limited with the interface's methods even if you define additional method in your implementation you can't call it. In the second code you are creating a class that implements the interface A which means that you have a class that at least contain all the methods defined in the interface A and you can add inside your class B other methods and call its.
Related
Here's My code:
public interface Baseinterface {}
abstract class Interface1 implements Baseinterface{}
abstract class Interface2 implements Baseinterface{}
public interface Classinterface {}
And i want to use this code:
public class Myclass(Baseinterface interfaceversion) implements Classinterface{}
Where the kind of interface implementation is passed as a constructor.
So when a function is defined in both of those abstract classes my actual class knows which one to use. I am fairly new at java.
Thanks.
I may be misunderstanding the nature of the question, but here goes:
Given this code which describes two abstract classes that implement the same method as defined by an interface:
interface BaseInterface {
void foo();
}
abstract class ITestA implements BaseInterface {
public void foo() {
System.out.print("A");
}
}
abstract class ITestB implements BaseInterface {
public void foo() {
System.out.print("B");
}
}
public class MyClass {
private BaseInterface enclosed;
MyClass(BaseInterface base) {
enclosed = base;
}
public void foo() {
enclosed.foo(); // call the implementation specific to the instance passed in by constructor
}
}
This could be called like:
public class Test {
void bar() {
// This may look weird cause we're defining an anonymous implementation of the abstract class, without adding any new implementation details
ITestA impl = new ITestA() {};
MyClass myClass = new MyClass(impl);
myClass.foo(); // prints "A"
}
}
interface A{
public void get();
public void set();
}
abstract class abstractA implements A{
#Override
public void get(){
System.out.println("in get funciton");
}
abstract public void set();
}
class B extends abstractA implements A{
#Override
public void set(){
System.out.println("In set method");
}
}
Is it not necessary to implement get method in class B?
Is it because abstractA already implemented the same method ?
Is this multiple inheritance ?
Is it not necessary to implement get method in class B?
No, since abstractA already implements it and B extends abstractA.
Is it because abstractA already implemented the same method ?
Yes. And because B extends abstractA. Both are required for this to work.
Is this multiple inheritance ?
No, it is not. B is still inheriting only one class - abstractA.
No because get() is already implemented in abstractA. This is inheritance.
Java does not allow multiple inheritance (which consist in inheriting from multiple classes BTW). The class B inherits from abstractA, and implements the behavior required by A. Actually, you could write your code like this :
interface A{
public void get();
public void set();
}
abstract class abstractA implements A{
#Override
public void get(){
System.out.println("in get funciton");
}
abstract public void set();
}
class B extends abstractA /* no need to specify that B implements A */{
#Override
public void set(){
System.out.println("In set method");
}
}
B inherits everything implemented by abstractA, which also include its interfaces.
B inherits the implementation of get() from abstractA.
Multiple inheritance is when B inherits from more than one classes. Which is not the case here.
Here is my scenario:
public interface Father{ public void sayHi(); }
public abstract class FatherImpl implements Father{
#Override
public void sayHi() { System.out.print("Hi"); } }
then is the child
public interface Child{}
public class ChildImpl extends FatherImpl implements Child{}
and test function is
Child c = new ChildImpl();
c.sayHi();
This will throw compiling error.
Only when i change child interface to
public interface Child{} extends Father
Then the program runs properly.
Anyone can help me explain the reason.
Child c = new ChildImpl();
The Child interface doesn't have a sayHi() method, that's in ChildImpl. You're referencing c as a Child object, so you can't access the method.
You could either reference the object as a ChildImpl or add another class.
ChildImpl c = new ChildImpl();
Or
public abstract class TalkingChild {
#Override
public void sayHi() {
System.out.print("Hi");
}
}
Or
public interface TalkingChild {
public void sayHi();
}
The best solution completely depends on your specific scenario.
The problem is that the compiler cares only the declared type - the type that is assigned is irrelevant.
Applying this to your case, the type Child has no methods. It doesn't consider that you assigned a ChildImpl, which does have a sayHi() method, to the Child variable.
I am doing an exercise, the book is not helping me grasp the concept, neither are the online resources. This may seem really silly but I don't know what I'm missing!!! I am quite new to Java and have had a look at other examples on stack but to no avail :s I need to declare 3 interfaces. Each interface needs to declare a method with the same name as its interface. Then the abstract class is extended by 3 classes which implement the aforementioned interfaces.Each class needs to be instantiated. If anyone could explain the procedure to this I would be eternally grateful.
interface antiLockBrakes{
public void antiLockBrakes();
}
interface cruiseControl{
public void cruiseControl();
}
interface powerSteering{
public void powerSteering();
}
public abstract class Auto{
abstract class Model1 extends Auto implements antiLockBrakes{
public abstract void antiLockBrakes();
Model1 mod1 = new Model1();
mod1.antiLockBrakes();
}
public static void main(String[] args){
}
}
this is your question: someone to explain how exactly to declare and interface and then have it implemented in the abstract class right??
Here's the answer for it.
See lets consider I have an interface
interface someInterface{
public void someMethod();
}
Now to implement the someInterface in abstract class
public abstract class SomeClass implements someInterface{
public void someMethod(){
System.out.println("Inside someMethod");
}
public abstract myMethod();
}
See in the class SomeClass we have implemented interface by giving definition to method someMethod() and since we want this SomeClass to be a abstract class we have defined one abstract method myMethod() for it.
Now any class which extends from SomeClass will also implement interface someInterface implicitly (because SomeClass has implemented it) and if it want its own definition for someMethod() it can override it. But if a child class wants to be a concrete class ( a class in which all its method will have implementation) then it has to provide implementation for abstract method myMethod().
HTH:)
this is what I like to use to see the difference between abstract classes and interface classes
interface class
//I say all motor vehicles should look like that :
interface MotorVehicle {
void run();
int getFuel();
}
// my team mate complies and write vehicle looking that way
class Car implements MotorVehicle {
int fuel;
public void run() {
System.out.println("Wrroooooooom");
}
public int getFuel() {
return this.fuel;
}
}
abstract class
// I say all motor vehicles should look like that :
abstract class MotorVehicle2 {
int fuel;
// they ALL have fuel, so why let others implement that ?
// let's make it for everybody
int getFuel() {
return this.fuel;
}
// that can be very different, force them to provide their
// implementation
abstract void run();
}
// my team mate complies and write vehicle looking that way
class Car2 extends MotorVehicle2 {
void run() {
System.out.println("Wrroooooooom");
}
}
Class A implements interface I that requires method doAction(). If I call a method from class A of class B, and pass "this"(class A) into that method, how can I call a method that lives in class A from the method in class B? For example:
class A implements I {
public void start() {
B.myMethod(this);
}
#Override
public void doAction() {
// Do stuff...
}
}
Class B {
public void myMehtod(Class theClass) { //How would I accept 'this', and...
theClass.doAction(); //How would I call the method?
}
}
I am doing this for purposes of a custom library, without knowing the exact name of the class that extends I.
This is a very basic question about how interfaces work. I'd recommend trying to find a tutorial about them.
Anyway, all you have to do is declare a parameter with the interface as its type. You can invoke interface methods on variables of the interface type (or any sub interface or class that implements that interface).
Class B {
public void myMethod(I theClass) {
theClass.doAction();
}
}