interface & abstract class variables

M

man4*.*

While writing abstract class do you define any variables?
My question is: Why you do that? it doesn't make sense to me becouse
you can't make instance of abstract class..
I understand why to make non-abstract method in abstract class, but
this story with variables...as I said doesn't make sense (most probably I'm
wrong)

With interface as (100% abstract class) defining variable is even worse...

am I missing something? ;-)

THX
 
M

man4*.*

I found answer for my question...
"variables defined in interface are always public static and final"...which
means constants....
what a silly question I've asked... :)
 
J

Jason Cavett

First off, do you understand the differences between an abstract class
and an interface.

An abstract class defines a concept of what the implementing class is.
For example, if you have a concrete class that extends an abstract
class, you're basically saying that the concrete class IS A(n) abstract
class. (A ktichen IS A room - for a real life example.) An interface,
on the other hand, is a contract for your class. So, if a class
implements an interface, it is essentially saying that that class will
have certain abilities and characteristics, but will provide its own
implementation of those things. So, a car and a plane and a truck are
all vehicles (abstract) but they all implement the Driving interface.
This guarantees they can all drive, but each one does it in their own
way.

So, back to your question.

An abstract has the ability to implement some of its methods while
leaving others up to the concrete classes. As a result, an abstract
class will need variables to implement those methods. What scope those
variables have to the concrete classes depends on what you want to do
with that abstract class. The two you should generally look at,
though, are private variables (the only way to access is via
getter/setter methods within the abstract class) and protected
variables. Protected variables can be directly accessed by classes
that extend the abstract class. In the following example,
AbstractClass implements a method called "stuff()." Stuff uses a
variable called "text." Because text is protected, the implementing
class, "ConcreteClass" can access "text" directly without worrying
about getters/setters.

public abstract class AbstractClass() {

protected String text;

public String stuff() {
text = "Some Stuff";
return text;
}

public abstract String otherStuff();
}

public class ConcreteClass() extends AbstractClass {

public String otherStuff() {
text = "Some Other Stuff"; // text variable from AbstractClass
return text;
}
}

I hope I got all that correct (it's early still) and I hope that
explains things a little bit for you. Someone correct me if I messed
up in my explanation anywhere.
 
M

man4*.*

wow, THX a lot...you've mentioned a few very important
details that helped me a lot to figure out some other things...

THX 1 again!
 
T

trippy

man4*.* took the hamburger said:
While writing abstract class do you define any variables?
My question is: Why you do that? it doesn't make sense to me becouse
you can't make instance of abstract class..

Because you might want the child class(es) to have those variables and
this way, you can ensure the child class(es) will have them. This
applies to methods too.
I understand why to make non-abstract method in abstract class, but
this story with variables...as I said doesn't make sense (most probably I'm
wrong)

With interface as (100% abstract class) defining variable is even worse...

am I missing something? ;-)

THX

--
trippy
mhm31x9 Smeeter#29 WSD#30
sTaRShInE_mOOnBeAm aT HoTmAil dOt CoM

NP: "All I Really Want" -- Alanis Morissette

"Now, technology's getting better all the time and that's fine,
but most of the time all you need is a stick of gum, a pocketknife,
and a smile."

-- Robert Redford "Spy Game"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top