Implementing interface to a abstract class Java - java

I was testing a program in which i was trying to implement a interface on abstract class . as given below
interface Inf{
void display();
}
abstract class InfTst implements Inf{
}
class InterfaceTest extends InfTst{
void display(){
System.out.println("Hello");
}
}
but it is shows error
error: display() in InterfaceTest cannot implement display() in Inf
void display(){
what is this error means and how resolve it, please help me.

When you omit access modifier on an interface it defaults to public, but on concrete classes it defaults to package-private.
Change your method signature to below in concrete class InterfaceTest
public void display(){..}

In concrete classes by default package is private. A concrete method means, the method have complete definition so must be your method modifier are always public.
package domain;
interface Inf {
void display();
}
abstract class InfTst implements Inf {
}
class InterfaceTest extends InfTst {
public void display() {
System.out.println("Hello");
}
}
public class StackOverFlow extends InterfaceTest {
public static void main(String[] args) {
StackOverFlow sof = new StackOverFlow();
sof.display();
}
}
output : - Hello

Related

How can we call abstract class constructor?

public abstract class MyAbstractClass{
public MyAbstractClass(){
System.out.println("MyAbstractClass Constructor");
}
}
Can anyone help me with this problem?
You could use super() in the child class which extends this abstract class.
For ex:
public class MyAbstractClassImpl extends MyAbstractClass {
public MyAbstractClassImpl() {
super();
}
public static void main(String[] args) {
MyAbstractClassImpl obj = new MyAbstractClassImpl();
}
}
You can not call directly the constructor of an abstract class, i.e. you are not allowed to create instances of an abstract class. The constructor(s) should actually be protected and not public. So either you have an anonymous class overriding the abstract parts or a non-abstract sub-class of your abstract class. Then the constructor is explicitly called via super() as mentioned in the other posts.
Try using super() in a child class.
public abstract class MyAbstractClass{
public MyAbstractClass(){
System.out.println("MyAbstractClass Constructor");
}
}
public class Child extends MyAbstractClass{
public Child() {
super();
System.out.println("Child!");
}
public static void main(String[] args) {
new Child();
}
}

Which Method will get override from interface or class?

In the below program I had extended Demo1 class and implemented Demo interface and in Practice class I override public void demo() which was declared in both class and interface then from which that method will get Override? and why?
interface Demo{
void demo();
}
class Demo1{
int i=10;
public void demo() {
System.out.println("this is demo"+i);
}
}
public class practice extends Demo1 implements Demo {
public static void Main(String[] args ) {
practice p=new practice();
p.demo();
}
public void demo() {
System.out.println("This is Overrided method");
}
}
The short answer is that it doesn't matter. What the compiler is is looking for is that the signature of the abstract interface method is implemented in your class, or inherited from a supertype (and it doesn't care whether that inherited signature was meant to implement the abstract method in question). And whether your demo() method is called on a practice object declared as Demo or Demo1 is also irrelevant the method signature is implemented either way.
You can, in fact, even remove your demo() override (assuming you didn't need to change the behavior), and the code would still compile:
class practice extends Demo1 implements Demo {
public static void Main(String[] args) {
practice p = new practice();
p.demo();
}
}
That is, even if Demo1.demo() has nothing to do with Demo.demo(), the fact that practice inherits Demo1.demo() which has the same signature as Demo.demo() and without violating access and exception constraints, that makes practice a valid implementation of Demo.
Method from interface is not overrided, it's implemented.
Your practice class inherits the demo method from Demo1 class and this method will be used to implement Demo interface method if you omit the implementation of demo method in practice class itself.
Since you implement the demo method in practice class itself - this method overrides the method from Demo1 class and also implements the method from Demo interface
If you run the below code
public class practice extends Demo1 implements Demo {
public static void main(String[] args ) {
practice p=new practice();
p.demo();
}
public void demo() {
System.out.println("This is Overrided method");
}
}
Output will be "This is Override method" and if you comment the demo method like below
public class practice extends Demo1 implements Demo {
public static void main(String[] args ) {
practice p=new practice();
p.demo();
}
/*public void demo() {
System.out.println("This is Overrided method");
}*/
}
Output will be "this is demo10". In first case as class object finds definition of demo in self, it will give preference to this over to parent class.
Demo1 is providing implemetation of demo(). so it will take parent demo() method. Output will be
this is demo10
But then you have implemented again overriding of demo() method in child class practice
so output is now
This is Overrided method

How to use a non abstract method from a Abstract class in another class without extending

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

method implemented in abstract class, but appears in interface

I'm learning abstract classes vs interfaces at the moment and trying to figure out situations where to use one over the other. I'm having trouble figuring out this example at the moment:
public interface Face {
public void test();
}
public abstract class Tract {
public void test() {
System.out.println("over here");
}
}
public class Thing extends Tract implements Face {
public void test() {
// what should print out?
}
}
Here, the test() function is implemented in the abstract class. If you don't implement it in the subclass, would it call the abstract class' method and print out "over here"? Does the interface accept implementations from an ancestor class or do you have to implement it in the subclass, therefore overriding the abstract class implementation?
All the interface cares about is that the class has implemented a method called test() that returns void. It does not matter whether the method is implemented in the class directly or in any ancestor (parent) class.
In your case, the Thing class has inherited its definition of test() from Tract, and therefore implements the Face interface without you having to provide a definition explicitly.
In the class "Tract" you have given an implementation for the method coming from the interface. Also you override it in "Thing" class so when calling this method on a Thing instance then this version(Thing version) is going to be called.
All java methods are virtual.
lets consider little bit modified code,
I hope, you will get the idea:
public interface Face {
public void test();
}
public abstract class Tract {
public void test() {
System.out.println("Tract here");
}
}
public class Thing extends Tract implements Face {
public void test() {
System.out.println("Thing here");
}
}
public class Thing2 extends Tract implements Face {
}
lets go to output:
Tract tr = new Tract();
tr.test();
will not compile because you can't instantiate abstract class.
Thing th = new Thing();
th.test();
will print "Thing here"
Thing2 th2 = new Thing2();
th2.test();
will print "Tract here",
because you not overwritten the test() method in abstract class.
Main idea of this approach - you can abstract implementation in the future use
class C {
void print(Face face) {
face.test();
}
}
new C(new Thing()).print();
will print "Thing here";
new C(new Thing2()).print();
will print "Tract here";
You can hide different implementations
But this is not main idea of abstract classes.
main idea abstract classes are:
public interface Face {
public void test();
}
public abstract class Abstract {
abstract public void test();
}
public class Thing1 extends Abstract implements Face {
public void test() {
System.out.println("Thing1 here");
}
}
public class Thing2 extends Abstract implements Face {
public void test() {
System.out.println("Thing2 here");
}
}
main idea - you can declare method without implementation
new C(new Thing1()).print();
will print "Thing1 here";
new C(new Thing2()).print();
will print "Thing2 here";
main idea - you declare the method in abstract class, that you MUST override to compile code.
I hope, this is enough explained answer.

How to define a member interface in a static context in java?

The member interface can only be defined inside a top-level class or
interface or in a static context.
Case A: Interface within a top-level class works perfectly
package multiplei.interfaces.test;
public class InterfaceBetweenClass {
interface Foo {
void show();
}
class InnerClass implements Foo{
public void show(){
System.out.println("Inner Class implements Foo");
}
}
public static void main(String[] args) {
new InterfaceBetweenClass().new InnerClass().show();
}
}
Case B: Interface within an interface works good.
public interface Creatable {
interface Foo{
void show();
}}
Case C: I know it sounds stupid that why would anyone define an interface in a static context. But it give me the same error message when i try to define the interface in static context.
package multiplei.interfaces.test;
public class InterfaceBetweenClass {
public static void main(String[] args) {
interface Foo { //Line 5
void show();
}
}
}}
But line number 5 gives me the following error message "The member interface Foo can only be defined inside a top-level class or interface or in a static context." Please help me out with this If an interface can be defined in static context then how?
You cannot define interfaces within methods.
I think the scenario the error message is referring to is defining an interface inside an inner class (which can be done, but only if that is a static inner class):
class A{
static class X{
interface Y{}
}
}
For interface, both nested-interface and member-interface are the same thing (#opposite to member-class). Moreover, member-interface is an interface which is DIRECTLY enclosed by another class or another interface. Therefore, local interface DOES NOT exist.

Categories