permaculture playing cards*
The moose likes Beginning Java and the fly likes abstract, polymorphism, protected, concrete Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "abstract, polymorphism, protected, concrete" Watch "abstract, polymorphism, protected, concrete" New topic
Author

abstract, polymorphism, protected, concrete

Richard Chambers
Ranch Hand

Joined: Apr 03, 2008
Posts: 40
I have a few general questions about the assignment I'm completing

I'm trying real hard to understand this stuff, but have a poor textbook

and a worse instructor.

The assignment deals with Shape hierarchy

I've created several class files for this package
Shape is a super class
TwoDimensionalShape and ThreeDimensionalShape classes extend Shape
Square and Circle extend TwoDimensionalShape
Cube and Sphere extend ThreeDimensionalShape

Now for the questions.

I know what the concrete methods of the abstract methods of the superclass,
but do I have some special keyword or other way of identifying them?
or do I just declare them like a normal method.

What is protected used for. My text only says that it is a legal keyword for a class or method, but doesn't explain anything.

and very specifically, the only error in my 11 file program:

in my class cube I have this method:



I have essentially the same code in class Sphere, and it compiles OK.

why?

the toString method is in Shape - two levels up from Sphere and Cube.

again, Sphere and Cube both extend ThreeDimensionalShape
ThreeDimensionalShape is abstract
ThreeDimensionalShape extends Shape
Shape is also abstract
all are public, none are static

Thanks

(This is my last assignment in this class.
I'm going to buy HeadFirst Java and learn it for real now)
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4530
    
    5

Hi Richard

The keyword protected refers to the scope of the member (a member is a class-level variable, or a method).

Members defined as protected are inherited by subclasses, which can choose to declare them protected or public. They cannot be accessed by other classes.

Members declared as public are inherited by subclasses, and can be accessed by other classes.

Members declared as private are not inherited by subclasses and can only be accessed within the class that declares them.

Members declared without a keyword ("default" access) are package-private: they can be accessed by classes within the same package.

Here are a few links you might like to bookmark:
The Java Language Specification (download)
The Java Language Specification (online)
JLS : Java Glossary


luck, db
There are no new questions, but there may be new answers.
Stevi Deter
Ranch Hand

Joined: Mar 22, 2008
Posts: 265

Originally posted by Richard Chambers:



I get this compiler erroe:





Richard,
I think if you read the compiler error really closely, you'll see that it's correct, "toSting" does not exist. Sometimes our eyes let us see what we expect and not what we really typed.

I have a post in my blog that gives an overview of the protected modifer and how it really works (based on this JavaRanch topic).

Good luck!


There will always be people who are ahead of the curve, and people who are behind the curve. But knowledge moves the curve. --Bill James
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38467
    
  23
Originally posted by Darryl Burke:
Hi Richard

The keyword protected refers to the scope of the member (a member is a class-level variable, or a method).

Members defined as protected are inherited by subclasses, which can choose to declare them protected or public. They cannot be accessed by other classes.
Lots of people think that, but a protected member can be accessed by other classes in the same package as well as subclasses.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4530
    
    5

Hi Ritchie

Thanks for the clarification.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38467
    
  23
You're welcome. And Stevi Deter's quote gives the same information, which I hadn't noticed earlier. Sorry.

BTW: which book is it you don't like?
Richard Chambers
Ranch Hand

Joined: Apr 03, 2008
Posts: 40
Thanks for all the help

Who'd of thought - a typo.

My frustration over the textbook would probably be
better directed at the instructor.

I'm taking this course at a local community college.

I chose to take it there BECAUSE I didn't want self instruction.

This whole semester we've had one lecture -
and it was "THIS CLASS CONSISTS ENTIRELY OF LABS-
DON'T BOTHER ME"

One student asked a question on the first project,
and he chewed him out in front of everyone.

No one has dared ask a question since.

The book has ten chapters, and we had ten projects and ten tests

Our projects have NOTHING to do with the material in
the current chapter we are studying. Very frustrating.

We've had ten test. The tests also have very little to
do with the material in the chapter it is for.

Each test has also had several questions where he gives code
and we're supposed to tell what it outputs.

What can I say, I cheated. I cut and paste the code into my text editor.
compile it, run it. enter the output on the test.

50% of the time he marks it wrong and claims a different output.

The book is Deitel's Small Java.

perhaps it would be a good book with a good instructor.
It is not good for self study. I read a section many times,
just to figure out what it is saying. Way too much needless prose.
One of my biggest frustrations, is it will use terminology in Chapter 3, that it doesn't introduce until Chapter 8. I spent a large amount of
time skipping through the book trying to figure out what it was talking about. I also bought JAVA for Dummies as a "Desk reference". The text will spend lot's of pages on a concept, and thouroughly muddy it.
At least with Dummies, I can clarify it to a couple of paragraphs.


I can see that if an instructor lectured, covering the high points of
each chapter, then used assignments and projects from that chapter,
it could have potential.

Reading other threads, I find that the JAVA I am learning, is not
the practical stuff I will be able to use. For our projects,
the instructor fleshes out our programs with psuedo code, and we
complete the programming. I've become good at translating, pseudo code
to JAVA, but that is NOT JAVA programming, and I don't feel comfortable
writing anything much more complicated than "Hello World", 5 months and $1000 later. We've not even mentioned graphics.

I also see where Deitel's website lists the book as having 900+ pages,
our text is much smaller, and I suspect a Readers Digest version.

I've also located our "projects", and find they are in Dietel's C++ text.

I feel like I'm reading a text translated from a foriegn language,
with projects adapted from a totaly different text for a different
programming language.

I DID learn enough to know I want to actually learn JAVA.

I turn in this project, and take my final, then I am done with this stage.
Next week, I'll look through the book reviews here again, but I think
I'll get HeadFirst, and self study my way through it. Someone's also suggested Thinking In Java.

Thanks again for everyone's help. And as I actually start LEARNING JAVA
this summer, I'm sure I'll be back often with questions.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38467
    
  23
Sorry to hear about your troubles. I like Deitel myself, but I stick to the full-size editions which stretch to 1500 pages, but I know there are many people who absolutely detest the Deitel style.
And as for the exercises, the Deitel books all have the same exercises in the back!
Dummies (is that Barry Burd?) doesn't go into object-orientation enough for my likings, but Burd is very good at explaining how little bits of syntax work.

If you haven't found out yet how helpful people are here, you soon will
Stevi Deter
Ranch Hand

Joined: Mar 22, 2008
Posts: 265

Originally posted by Richard Chambers:
Who'd of thought - a typo.


It's definitely a Good Thing you're initially learning via writing code with a text editor and compiling with javac; you'll have a much stronger foundation in the language.

Once you get the basics down, it'll be time to look into an IDE. The main value of an IDE, IMHO, is simply to help avoid tearing your hair out when you could SWEAR you typed "toString()"!

Originally posted by Richard Chambers:
I DID learn enough to know I want to actually learn JAVA.


Given your pains, I'm really glad to hear that!

Have you complained to the Community College about this "professor" yet?
[ May 08, 2008: Message edited by: Stevi Deter ]
 
wood burning stoves
 
subject: abstract, polymorphism, protected, concrete