|
It is currently Thu May 02, 2024 4:39 pm
|
Author |
Message |
RB
Emperor
Joined: Wed Apr 16, 2003 1:25 am Posts: 2560
|
![Reply with quote Reply with quote](./styles/getaway/imageset/en/icon_post_quote.gif)
Nothing special. But you can learn inheritance (classes) together with implementation (interfaces). One class can ihnerit only one other class and not two or more. As well, all the public methodes from the parent classes would be accessible as public methods of the children class Private methodes would be accessible only in the parent class. Any of children would be unable to access them. At end, there are protected methodes, which will be accessible in the children classes but only if the object call them. Not from outside.
A children class can as well override a method from a parent class. See the example: One class can inherit only one other class but it can implement as much interfaces as you like. One interface is just a set of function declarations. All functions from implemented interfaces must be implemented in the class. Otherwise it won't work. At end, you can implement and extend at the same time: One question could be "why would I need inheritance when by implementing I can declare any function I wish?". First the functions from an interface are just declarations. Programmer is not allowed to define bodies of them inside the interface. E.g. the following example would be false There must not be body. Second, when classes child1 and child2 inherit the class parent, you can set them in the same array of parent's type and use them. Although they are hold by a pointers of type parent, child1 will behave as child1 and child2 will behave as child2. Of course one limit is that from pointer of type parent you can call only the methods which are defined/declared in parent, but if e.g. children3 had its characteristic method and you know it is a child3 object, you can cast the pointer parent to child3. See the example:
_________________ ++
|
Fri Apr 21, 2006 12:38 pm |
|
![](./styles/getaway/theme/images/spacer.gif) |
derf
Minor Diety
Joined: Fri Apr 11, 2003 2:17 pm Posts: 7721 Location: Centre of the sun
|
Thanks for the 1337 tips GFree.
Heres another problem:
Ive started to write a program that will be used as a super-class eventually. It needs to take integers from the "user" so that it has row and column lengths for a 2d array:
The problem is that i dont know how to get integer from the user. Only String. It fails to compile because rowLength and colLength are String, not integer.
_________________ "Well a very, very hevate, ah, heavy duh burtation tonight. We had a very derrist derrison, bite, let's go ahead and terrist teysond those fullabit who have the pit." - Serene Branson
|
Sat Apr 22, 2006 9:24 am |
|
![](./styles/getaway/theme/images/spacer.gif) |
Satis
Felix Rex
Joined: Fri Mar 28, 2003 6:01 pm Posts: 16650 Location: On a slope
|
_________________ They who can give up essential liberty to obtain a little temporary safety, deserve neither liberty nor safety.
|
Sat Apr 22, 2006 9:34 am |
|
![](./styles/getaway/theme/images/spacer.gif) |
derf
Minor Diety
Joined: Fri Apr 11, 2003 2:17 pm Posts: 7721 Location: Centre of the sun
|
The compiler doesnt like "try":
Below is the rather ugly-looking program. (for reference):
_________________ "Well a very, very hevate, ah, heavy duh burtation tonight. We had a very derrist derrison, bite, let's go ahead and terrist teysond those fullabit who have the pit." - Serene Branson
|
Sat Apr 22, 2006 9:59 am |
|
![](./styles/getaway/theme/images/spacer.gif) |
RB
Emperor
Joined: Wed Apr 16, 2003 1:25 am Posts: 2560
|
After try you need at least one catch anyway.
See a bit about it.
_________________ ++
|
Sat Apr 22, 2006 1:56 pm |
|
![](./styles/getaway/theme/images/spacer.gif) |
Satis
Felix Rex
Joined: Fri Mar 28, 2003 6:01 pm Posts: 16650 Location: On a slope
|
lol, you were supposed to read the link, small fry. I just copied part of the source code...I omitted the catch part. Try and catch aren't required, though they can be handy in some situations.
_________________ They who can give up essential liberty to obtain a little temporary safety, deserve neither liberty nor safety.
|
Sat Apr 22, 2006 8:58 pm |
|
![](./styles/getaway/theme/images/spacer.gif) |
derf
Minor Diety
Joined: Fri Apr 11, 2003 2:17 pm Posts: 7721 Location: Centre of the sun
|
Mmm... When i copy and paste the whole code (same as website exactly) into a new Java file, it doesnt seem to work.
It compiles fine, but when i run it i get the error:
Any ideas?
Oh, btw, when i say "prompt the user", i mean displaying a dialog box so he can input one number and then the other.
_________________ "Well a very, very hevate, ah, heavy duh burtation tonight. We had a very derrist derrison, bite, let's go ahead and terrist teysond those fullabit who have the pit." - Serene Branson
|
Sun Apr 23, 2006 9:59 am |
|
![](./styles/getaway/theme/images/spacer.gif) |
RB
Emperor
Joined: Wed Apr 16, 2003 1:25 am Posts: 2560
|
You get input error? May you enter something unexpected ![Rolling Eyes :roll:](./images/smilies/icon_rolleyes.gif) the program awaits two integers to add.
_________________ ++
|
Sun Apr 23, 2006 2:45 pm |
|
![](./styles/getaway/theme/images/spacer.gif) |
derf
Minor Diety
Joined: Fri Apr 11, 2003 2:17 pm Posts: 7721 Location: Centre of the sun
|
I solved it.
I discarded the whole try & throw affair and replaced it simply with:
String numStr = JOptionPane.showDialogBox("Please enter number");
int numInt = Integer.parseInt(numString);
Bingo.
_________________ "Well a very, very hevate, ah, heavy duh burtation tonight. We had a very derrist derrison, bite, let's go ahead and terrist teysond those fullabit who have the pit." - Serene Branson
|
Sun Apr 23, 2006 4:26 pm |
|
![](./styles/getaway/theme/images/spacer.gif) |
derf
Minor Diety
Joined: Fri Apr 11, 2003 2:17 pm Posts: 7721 Location: Centre of the sun
|
Hey GFree, would you mind throwing in some parameters in the code you posted above explaining inheritance? Its just that im finding it difficult to put into perspective with parameters.
_________________ "Well a very, very hevate, ah, heavy duh burtation tonight. We had a very derrist derrison, bite, let's go ahead and terrist teysond those fullabit who have the pit." - Serene Branson
|
Thu Apr 27, 2006 12:04 pm |
|
![](./styles/getaway/theme/images/spacer.gif) |
derf
Minor Diety
Joined: Fri Apr 11, 2003 2:17 pm Posts: 7721 Location: Centre of the sun
|
My god, i think i just got it to work somehow... ![Shocked :shock:](./images/smilies/icon_eek.gif)
_________________ "Well a very, very hevate, ah, heavy duh burtation tonight. We had a very derrist derrison, bite, let's go ahead and terrist teysond those fullabit who have the pit." - Serene Branson
|
Thu Apr 27, 2006 12:39 pm |
|
![](./styles/getaway/theme/images/spacer.gif) |
RB
Emperor
Joined: Wed Apr 16, 2003 1:25 am Posts: 2560
|
Some of them are pieces of code which are not ment to work without an public class with public static main methode.
Some of them (those who have one public static main methode in one of classes) are meant to work independent, but they must be saved in *.java file which has the same name as the class with main.
_________________ ++
|
Thu Apr 27, 2006 3:32 pm |
|
![](./styles/getaway/theme/images/spacer.gif) |
derf
Minor Diety
Joined: Fri Apr 11, 2003 2:17 pm Posts: 7721 Location: Centre of the sun
|
Ahhh, ok.
*done*
Now im screwing around with Constructors, and with no proper textbook and no lecture until Wednesday, im struggling with the following concept:
Heres the question:
I need to implement a class that works out the perimiter and area of a rectangle, but apparently the question says: "There is need for a default constructor that initialises the length and width of a rectangle to zero and an additional constructor where the length and width can be initialised at the time of creating an object."
I just hit a mental block here because so far, ive only used constructors such as:
What can you tell me?
_________________ "Well a very, very hevate, ah, heavy duh burtation tonight. We had a very derrist derrison, bite, let's go ahead and terrist teysond those fullabit who have the pit." - Serene Branson
|
Fri Apr 28, 2006 10:41 am |
|
![](./styles/getaway/theme/images/spacer.gif) |
RB
Emperor
Joined: Wed Apr 16, 2003 1:25 am Posts: 2560
|
That one is not a constructor but assignment of the new object of type Thingy to an pointer thingy1 of type Thingy.
I think you should read the chapter which introduces in constructors once more.
As for the question, here is the start:
_________________ ++
|
Fri Apr 28, 2006 11:35 am |
|
![](./styles/getaway/theme/images/spacer.gif) |
derf
Minor Diety
Joined: Fri Apr 11, 2003 2:17 pm Posts: 7721 Location: Centre of the sun
|
Ok, gotcha. Now thats done.
Now one last thing.....Polymorphism. There are loads of explanations in books and stuff, but it means nothing to me because i cant seem to put into context. Unfortunately, thats how my brain works. You could go on and on endlessly about concepts, but to me they will never have value without context.
GFree, could you write a class showing the most basic use polymorphism please?
_________________ "Well a very, very hevate, ah, heavy duh burtation tonight. We had a very derrist derrison, bite, let's go ahead and terrist teysond those fullabit who have the pit." - Serene Branson
|
Wed May 03, 2006 6:30 am |
|
|
Who is online |
Users browsing this forum: No registered users and 4 guests |
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot post attachments in this forum
|
|