Re: New (as in days) to Java - question about "super()" method



On Fri, 29 Jul 2005, Don Bruder wrote:

> Let's start right from start, ala "12 step" meetings...

The crowd here is quite different from the group you'd find at any "12
step" meeting. Might not be the best approach.

> Hi, I'm Don, and I'm a serious greenie when it comes to Java.
>
> (This is the part where you all say "Hi Don! Glad to have you here!")
>
> "How serious,", you ask? Well, in the past 3-4 days, I've done a
> "power-skim" of a "Learn Java on the Mac" book that came in PDF format
> on my DE's install disk, I've got some code to look at/play with, and
> I've been playing around in it for the last 2-3 days, give or take,
> trying to get a feel for the language.
>
> I come from a fairly extensive programming background - multiple
> flavors of BASIC, 6502/65C02 assembler, 8086 assembler, Turbo and UCSD
> Pascal, FORTRAN, COBOL, and a handful of others, including some serious
> weirdos that are more for the sake of being able to say "I can (or
> could at one time) code in _____" than any practical purpose. For the
> past couple-four years, I've been doing my thing almost exclusively in
> C, with a *VERY* minor foot-wetting in C++, and recently a foray into
> Python that pretty much left me shaking my head and asking "Why
> mistreat a computer that way?!?!?" (Not much different from my reaction
> to its namesake TV program, if you want the truth) The list isn't to
> brag, it's simply to put across the fact that I'm *NOT* a newbie to
> coding, only to coding *in Java*.

The problem here is that we don't know how you learned this stuff. I was
self-taught and had a list FAR longer than your list. Wasn't prepared to
learn Java because Object Oriented Programming is quite different from
procedural or assembly programming. Went to university and learned all
about the theory behind different programming languages. Learning Java
became much easier. Bottom line, I knew a lot before going to university
but there were gaps in my knowledge that made learning OOP difficult.

> So far, the PDF/book I'm using as a reference is serving me well, and
> where it's lacking, my own programming experience is doing a reasonable
> job of filling in the few blanks. However, I've encountered something
> that the book doesn't address (although there is a tantalizing hint,
> more in a moment) and nothing in my knowledge base can be safely
> applied, since it's obviously something that's going to have its own
> special Java meaning. To save newsgroup space, I've trimmed all the
> guts out of the method involved (a constructor, I believe, is the
> proper terminology in this case, no?), leaving only the parts that have
> me puzzled:

[lots of code snipped]

If you see something like:

super("start server");

you are dealing with inheritance and constructors. Learning two concepts
at once can be confusing. Learn one then the other.

Constructors: if I have a class called MyFabulousClass it can contain
methods. If the name of the method is the same as the name of the class,
e.g.

MyFabulousClass() {
// your code goes here
}

then it is called a constructor. When you create an instance of the class
it calls the constructor, e.g.

MyFabulousClass test = new MyFabulousClass();
^ ^ ^
| | |
| | constructor
| instance
class

Second topic, inheritance: if I have a class and I want to add some extra
functionality to it I can use inheritance. For example, I have the class
Human. I want to add the method Urinate(). A Man does this different than
a Woman. So I will have:

class Man extends Human
and
class Woman extends Human

I'll then add a Urinate() method to each class. Methods like Sleep() and
Eat() will come from Human. That is inheritance.

Now we can answer your question, what is super(). If I create a contructor
for Man it might look like:

Man(String name) {
super(name);
// do stuff for Man
}

Because Man extends Human, the super(name) is calling the parent
constructor, i.e. it is calling Human(name). You will also see things
like:

this(name);

If I have more than one constructor, e.g.

Man() {
name = "Joe Public";
}

Man(String s) {
name = s;
}

I can write it as:

Man() {
this("Joe Public"); // use the constructor below
}

Man(String s) {
name = s;
}

Before you go any further, I think you want to get a good book that
clearingly explains things like inheritance, interfaces, overloading, etc.
In other words, all the Object Oriented concepts. You want to question why
we need Java? Why cannot everyone write everything in C, COBOL or Scheme?
If you find the answer to that you will find learning Java much easier.

--
Send e-mail to: darrell dot grainger at utoronto dot ca

.



Relevant Pages

  • Re: Programming to an Interface
    ... implementation inheritance either. ... inheritance when programming in Java. ... you would refuse to ever inherit if programming in Python, Ruby, ... I can't comment on the dynamically typed OO languages for I don't know ...
    (comp.object)
  • Re: Programming to an Interface
    ... implementation inheritance either. ... inheritance when programming in Java. ... you would refuse to ever inherit if programming in Python, Ruby, ... implementation inheritance a single time). ...
    (comp.object)
  • Re: java tutorial available for download
    ... I have changed the spelling of constructer to constructor now. ... C++, Java, and OO programming books. ...
    (comp.lang.java.programmer)
  • Dealing with inheritance anomalies in Java
    ... In Java there are a few ways to cope with what is called the inheritance ... "Analysis of inheritance anomaly in object-oriented concurrent programming ... * use an object-orientated design without multithreading (in which case, ...
    (comp.lang.java.programmer)
  • Dealing with inheritance anomalies in Java
    ... In Java there are a few ways to cope with what is called the inheritance ... "Analysis of inheritance anomaly in object-oriented concurrent programming ... * use an object-orientated design without multithreading (in which case, ...
    (comp.lang.java.programmer)