Methods with the same name in java - java

Suppose we have an interface such as the following in java
public interface AnInterface
{
public void aMethod();
}
and a class as follows:
public class AClass
{
public void aMethod()
{
//bla bla bla
}
}
Now I'm going to define another class such as Subclass that extends AClass and implements AnInterface, as in:
public class Subclass extends AClass implements AnInterface
{
public void aMethod()
{
//do something
}
}
What does exactly aMethod() do in Subclass? Does it implement the method in AnInterface? Or does it overrides the method in AClass?
What should I do in order to make aMethod() implement the method of AnInterface?
Similarly, if I want it to override the method in AClass, what can I do with it?

As you might have noticed, the interface method(s) does not have any body. This simply means, that a class implementing this interface must implement those methods, here aMethod(). Your class SubClass extends AClass and inherits the method aMethod() from it. Now implementing the aMethod in the SubClass will simply override the method from AClass and at the same time adhere to the interface rules. So the method in effect will be the one in the SubClass.
To answer your question in short:
The aMethod() in Subclass currently both implements the method from AnInterface AND overrides it from AClass.

Related

Can we create instantiation of interface and abstract class with the help of anonymous class in Java?

I went to an interview. Interviewer asked me if one can instantiate an interface and abstract class? As per my knowledge I said "No". But he said "Yes, we can with the help of an anonymous class".
Can you please explain to me how?
This was a trick questions.
No you can not instantiate an interface or abstract class.
But you can instantiate an anonymous class that implements/extends the interface or abstract class without defining a class object. But it is just a shortcut to defining a fully named class.
So I would say technically your answer was correct.
I don't know what is "instantiation of interface and abstract class".
I think it's an inaccurate, improper expression of something,
we can only guess at the intended meaning.
You cannot create an instance of an interface or an abstract class in Java.
But you can create anonymous classes that implement an interface or an abstract class.
These won't be instances of the interface or the abstract class.
They will be instance of the anonymous class.
Here's an example iterator from the Iterator interface that gives you an infinity of "not really":
new Iterator<String>() {
#Override
public boolean hasNext() {
return true;
}
#Override
public String next() {
return "not really";
}
};
Or a funky AbstractList that contains 5 "not really":
List<String> list = new AbstractList<String>() {
#Override
public int size() {
return 5;
}
#Override
public String get(int index) {
return "yes";
}
};
Assume you have an abstract class: MyAbstractClass with abstract void method myAbstractMethod. Then you can make an "instance" of this class via this code:
MyAbstractClass myAbstractClassInstance = new MyAbstractClass() {
public void myAbstractMethod() {
// add abstract method implementation here
}
};
myAbstractClassInstance extends your MyAbstractClass in this case. When you instantiate this class you have to implement all abstract methods as you can see from the code above.
The same way works for interfaces, assume you have an interface MyInterface with a void method myInterfaceMethod inside, then you can create an "instance" (implementation of this instance) via this code:
MyInterface myInterfaceImpl = new MyInterface() {
public void myInterfaceMethod() {
// add method implementation here
}
}
myInterfaceImpl is an implemetation of MyInterface in this case. When you create an object using interface, you have to implement interface methods as it is shown above.
Interface :
interface Interface1 {
public void m1();
}
When you right
new Interface1() {
public void m1() {
}
}
Its not actually creating the instance of Interface. Its creating an instance of its subtype which doesnt have any name/reference. Hence we cannot create an instance of interface or abstract class
You cannot create instances of abstract classes or interfaces using the new operator. For example,
new AbstractSet(); // That's wrong.
You can, however, use them to declare reference variables. For example, You can do this:
AbstractSet set;
You can instantiate anonymous as well as declared implementing classes or subclass.
For example, Set extends AbstractSet, so you can instantiate Set.
Yes, we can create by having defining the abstract methods or the interface methods on the fly during instantiation. That's like a Named anonymous class.
//interface
Runnable r = new Runnable(){
public void run() {
System.out.println("Here we go");
}
};
//Abstract class
abstract class MyAbstract {
abstract void run();
}
MyAbstract ab = new MyAbstract(){
#Override
void run() {
System.out.println("Here we go");
}};

How to call the abstract method from a class which is not in hierarchy in java

Below is my code. I have the abstract class Myabatract which has the method myMethod and I have a subclass MySubClass in which I have overridden the myMethod. In my client class
I have a method callMethod from which I want to directly call the myMethod of Myabatract class is this possible?
abstract class Myabatract {
public void myMethod() {
System.out.println("This is from Myabatract");
}
}
class MySubClass extends Myabatract {
public void myMethod() {
System.out.println("This is from MySubClass");
super.myMethod();
}
}
class Client{
public void callMethod(){
}
}
You can create an anonymous implementation of the abstract class. This is particularly easy given the fact that it does not use any abstract methods.
class Client {
public void callMethod() {
Myabatract instance = new Myabatract() { /* nothing to implement*/ };
instance.myMethod();
}
}
As a user of the MySubClass type, you have no way to call the Myabatract method because it has been overridden, unless MySubClass were to expose it. Your only recourse would be to create another method that exposed the super method from within MySubClass (or other child implementations).
It's important to note that this will not work:
class Client {
public void callMethod() {
MySubClass instance = new MySubClass() {
#Override
public void myMethod() {
super.myMethod();
}
};
instance.myMethod();
}
}
super is the non-anonymous class, MySubClass, which means nothing is actually changing. Interestingly, this can be worked around in C++ using the scope resolution operator (::).
It's also worth pointing out that you are calling super.myMethod() in your implementation of MySubClass, which does invoke the Myabatract method.

Declaring an Interface and implementing in classes in an abstract class Java

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");
}
}

Calling another classes method with the object of the class

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();
}
}

concrete class method keeps throwing an exception from abstract class despite implementation

I have an abstract class Automobile which has an unimplemented method called move
so
public abstract class Automobile {
public void move() {
throw new UnsupportedOperationException();
}
}
I have a concrete class which extends my abstract class and implements the move method.My problem is the method keeps throwing an UnsupportedOperationException
public class Car extends Automobile{
int x;
public void move(){
x++;
}
}
It could be for many reasons in your concrete class: maybe your concrete doesn't actually extends Foo? Or maybe it calls super.move() somewhere in its body.
Instead of throwing an exception, the correct way is to define the class and method as abstract to force subclasses to override it.
public abstract class Foo {
public abstract void move();
}
Please note if Foo only has abstract methods, like in the example above, that's an interface that you want, not an abstract class. Also, you should name it to define a behaviour
public interface Moving {
void move();
}
And then:
public class MovingObject implements Moving {
....
#Override
public void move() {
// your implementation
}
....
}
Are you calling super.move() in your implementation class? Eclipse generates that call by default if you used Source->Override/Implement Methods...
Otherwise I think, that you did not override the method correctly.

Categories