Learning about Abstract classes and interfaces, is there anything else I have forgotten about?

Abstract Class
1.Cannot be initated.

2.Can have concrete methods.

3.Abstract methods with no body

4.A class extending the abstract class does not need to use all the abstract methods from the abstract class.

5.A class can only extend 1 abstract class

Interface
1.Cannot be initated

2.Isnt a class

3.All methods must be with no body

4.A class implementing the interface must declare all methods and right a body for them whether they are used or not

5.A class can implement many interfaces.

Recommended Answers

All 22 Replies

and I think you can't make an object of type abstract


I've the same example done by interface , abstract and inheritance

do you need it ?

If you can please send via e-mail or pm

what is ur email?

pm sent

and I think you can't make an object of type abstract

Correct. You cannot create an instance of an abstract class.

You could add:
A class that extends an abstract class but does not provide an implementation for all the abstract methods must also be declared abstract.

i sent you 3 e-mails :) you may find them n Junk b-coz i sent them via yahoo mail

best of luck :)

Learning about Abstract classes and interfaces, is there anything else I have forgotten about?

Abstract Class
3.Abstract methods with no body

4.A class extending the abstract class does not need to use all the abstract methods from the abstract class.

a few remarks here:

3. just because the class is abstract, this does not mean it has to contain any abstract methods. so, it MAY contain abstract methods, which have no body, but end in a ';'
on the other hand: as soon as a class contains only 1 abstract method, the class must be abstract

4. use ... maybe not, but it MUST implement them all, except in case the extending class is an abstract class itself. the first concrete (not abstract) class extending an abstract class (either directly, or through extending an abstract class which extends another abstract class) MUST implement all the abstract methods in it. your compiler will make this quite clear to you once you try to compile a class in which you have forgotten to implement one of those abstract methods.

@Mr Pen
this is an open forum, so don't solve issues in personal messages or by e-mail. post any answers or sollutions here, so they can be of help to anyone.
also, don't just hand out code. the point is to help people learn, by explaining them where they've gone wrong, and why they get certain errors.

writing your own code will make you understand better than being handed custom-made code. I'm not critisizing you for helping, but you may want to choose a more appropriate way to do so.

Stultuske, so if its an abstract method it has to be implemented in the class that inherits it, but if its a method it doesnt have to be implemented?

A few notes.

Abstract Class
1.Cannot be initated.

instantiated - "initiated" doesn't mean anything here...

4.A class extending the abstract class does not need to use all the abstract methods from the abstract class.

Does not need to use them, but it must provide code for them, even if the code is an empty body or an error message. Put simply: a concrete class must implement all of the abstract methods it inherits.

5.A class can only extend 1 abstract class

5. A class extends exactly one superclass. (if no class is specified, the class extends Object).

Stultuske, so if its an abstract method it has to be implemented in the class that inherits it, but if its a method it doesnt have to be implemented?

abstract methods have to be implemented, non-abstract methods are extended.
you can override them, but you shouldn't do so unless you want the method in question to perform other logic than in the abstract class.

YOu can however create "class variable" like variables for an Interface.And you can use those variables to store references to the objects of the classes that implement that interface

@stevanity:
so ... you put references to the classes that will implement the interface in the interface itself?
this implies you know which classes will implement this interface while you're writing it. or did I misunderstand what you are saying?

I think you misunderstood my point.

I was stating that 'subtype polymorphism' is also supported by Interfaces just like Abstract Classes. (even though interfaces and implementing classes have "has a" relation)

I meant, you can create an Interface Variable ( InterfaceName var; ) and use it to hold reference of the instances of the classes that implement that interface so as to implement Runtime Polymorphism, which is a massive feature of oops.

That was what I was telling.

But isn't it a language "feature" that all variables in an interface are implicitly static final, so how would that work?
ps Does anyone know what's happened to the Java Language Reference/Definition online - seems to have vanished???

ps Does anyone know what's happened to the Java Language Reference/Definition online - seems to have vanished???

I noticed that yesterday... good thing I've got the PDF on all of my machines, or I'd worry...

Sunacle even has broken links to it still on their pages: http://download.oracle.com/javase/6/docs/index.html

Notice that the VM spec has also gone west - I wonder what this portends...

Yes.
I hope both can be initiated by polymorphism where it is initiated by instantiating the [[/B]last class of the class hiererchy[B]] class which is result of extending or implementing.

But isn't it a language "feature" that all variables in an interface are implicitly static final, so how would that work?
ps Does anyone know what's happened to the Java Language Reference/Definition online - seems to have vanished???

I think i wrongly used the word "variable" becoz Im a beginner to Java/C# style of programming.

What I mean is,

Look at the following code:

interface Inter {

//stuff

}

class InterImplement implements Inter {

//stuff

}

class MainClass {

public static void main(){

Inter inter_1;
inter_1=new InterImplement();
inter_1.func_implement_in_InterImplement_declared_in_Inter();

}

This is what I meant. RunTime polymorphism can be achieved using Interface similar to Abstract Classes. This enables us to create common interface (not keyword) to access objects of unrelated class hierarchies provided they implement a common interface(keyword)

And BTW, what do you call this:

Inter inter_1;

a placeholder? to hold reference to object? What do you call it? Can anyone help me?

Isnt this naming convention ambiguous? I have seen articles that refer to this as variable or some as class variable. But that actually refer to the variable inside a class. So its very ambiguous.. Can anyone tell the proper name for that place holder. A reference Variable?

yes, that code is fine. What you v[can't do is declare a "variable" variable in the interface itself - it will always be a constant (static final).

inter-1 is a reference variable. It (only) holds references to instances of classes that implement Inter. Because its not declared static it is also an "instance variable". The Oracle doc uses "class variable" as a synonym for static variable.

objects of unrelated class hierarchies

maybe different class hierarchies is a better word, sinche they both will pass a IS-A test for the interface.

yes, that code is fine. What you v[can't do is declare a "variable" variable in the interface itself - it will always be a constant (static final).

inter-1 is a reference variable. It (only) holds references to instances of classes that implement Inter. Because its not declared static it is also an "instance variable". The Oracle doc uses "class variable" as a synonym for static variable.

Yes. ok. Ill use the word reference variable hereafter. And yeah you cant declare variables inside interfaces unless they are constants.

Thanks.

I found another word in the Core Java book from Sun Microsystems.

It refers them as "Object Variables"
Now this sounds pretty apt!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.