I'm trying to override a abstract method using anonymous class but it shows error like Class A is not abstract and abstract method eat() not implemented.Please Help me out
package javaapplication1;
abstract class D
{
abstract void eat();
}
public class A extends D
{
public static void main(String[] args)
{
D obj_d=new D(){
#Override
void eat()
{
System.out.println("I'm eating!");
}
};
obj_d.eat();
}
}
Related
I have a base class
abstract class Repository<T> {
Future<List<T>> getAll();
Future<T> getByID(int id);
}
Then I would extend it just to specify type. But Dart has interesting syntax typedef ... = .... What is better?
Classic way
abstract class UserRepository extends Repository<User>{}
Another way
typedef UserRepository = Repository<User>;
Last is more aesthetic
What pros and cons for every way?
You should use inheritance, because typedef required for creating aliases.
abstract class Repository<T> {
Future<List<T>> getAll();
Future<T> getByID(int id);
}
// with alias
typedef UserRepository = Repository<User>;
class UserRepositoryImpl extends UserRepository {
...
}
// without alias
class UserRepositoryImpl extends Repository<User> {
...
}
I'm learning Swift at the moment and came across "protocols". They seem to me like a prebuild class that you extend, like in typescript, is that true?
Thanks, Pascal
I would compare them to interface in typescript.
interface FooBar {
foo: () => void
bar: () => void
}
class MyFooBar implements FooBar {
public foo(): void {
...
}
public bar(): void {
...
}
}
I'm trying to create an abstract class called SpanishData
And then I want to create another class called alphabet that extends Spanish data
I'm getting an error: the superclass SpanishData doesn't have a zero-argument constructor. How do I fix this?
Here is my code:
abstract class SpanishData{
String englishWord;
String spanishWord;
String mp3;
SpanishData(this.englishWord,this.spanishWord,this.mp3);
void getList (){
}
}
//the alphabet class
import '../SpanishDataAbstract.dart';
class Alphabet extends SpanishData{
#override
void getList(
)
}
You need to refer to the properties of the parent class your class is extending. You can do this using the super keyword.
The super() method on a class constructor allows a subclass to pass arguments and execute the constructor of its superclass.
The code below works:
abstract class SpanishData{
String englishWord;
String spanishWord;
String mp3;
SpanishData(this.englishWord,this.spanishWord,this.mp3);
void getList (){
}
}
class Alphabet extends SpanishData{
// create a constructor of the alphabet class and call the parent constructor
Alphabet(String englishWord, String spanishWord, String mp3) : super(englishWord, spanishWord, mp3);
#override
void getList(){}
}
I have a class that takes one of its subclasses as an argument. When constructing that subclass, I want to be able to use this as the value of that argument:
class A(val b: B)
class B extends A(this)
However, this fails to compile
this can be used only in a class, object, or template
[error] class B extends A(this)
[error] ^
Is there any way to get around this? I'm pretty sure that a pattern like this can be written in Java.
I'm not so sure about the last statement:
public class MyClass {
static class A {
A(B b) {
System.out.println(b.value);
}
}
static class B extends A {
String value;;
B() {
super(this);
value = "x";
}
}
public static void main(String args[]) {
new B();
}
}
gives the following error:
/MyClass.java:10: error: cannot reference this before supertype constructor has been called
super(this);
^
There is no good reason to attempt to let the this reference escape the scope of the constructor before the object itself has been constructed. Refactor it.
I have the following scenario
public abstract ClassA{
public virtual void Initialize(string a, int b){
}
}
public abstract ClassB : ClassA{
public virtual int Initialize(string a, int b){
}
}
When I try to create stub for Class B, I receive the error saying that SClassB already defines a member called 'Initialize' with the same parameter types.
How do I resolve the issue?
Thanks,
Sathish
As far as I know, you can not override within an abstract class so you would either need to lose the abstract/virtual and use override modifier on ClassB, or override it in the class(es) that inherits from ClassB.