How to override correctly?

I'm trying to grasp the concept of how to override

Page 1 of 1

12 Replies - 979 Views - Last Post: 25 March 2009 - 03:30 PM Rate Topic: -----

#1 Valacom  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 23-March 09

How to override correctly?

Post icon  Posted 23 March 2009 - 04:07 PM

Hello, I'm in a java class and I use the Head First Java book and for overriding it doesn't show much code or examples for how to use it.
My teacher assigned a program that has quite a few classes but just to grasp the gist of overriding i'll only use 3 in this thread.
Basically the 3 classes I want to use are my superclass WoWCharacter which is extended by class Horde which is then extended by the class Orc. For my overriding method my WoWCharacter class is supposed to have an abstract speak method which is then supposed to be overridden by my Orc's speak method and is supposed to have the output of "Grrrr". Like I said headfirst really doesnt have examples of how to override and I jsut can;t seem to understand it.

public abstract class WoWCharacter {

	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int hpGen = (int)(Math.random() * 501); // Generates health
		int level = (int)(Math.random() * 81); // gens level
		int race = (int)(Math.random() * 4)+ 1; // gens race 1 orc 2 troll 3 gnome 4 dwarf
		
		System.out.println(race);
		
		if (race ==1) {
			Orc zug = new Orc(hpGen);	
		}
	   public abstract void speak();


That is my WoWcharacter Class there is more to it but that has to do with the other aspects of the program. Also ignore the random generators.



public class Orc extends Horde {

	int health;
	int level;
	
	public Orc(int gethp) { // Health object and constructor
		System.out.println("You've made an orc.");
		
		health = gethp;
		System.out.println("Health is " + health);
		
			
	
	
}
	public void speak() {
		System.out.println("Grrr");
	}

	}



And here is my orc class but when i run my program it wont print the "Grrr" I so desire it to print.

I offer my thanks to you in advanced as I know this is a helpful community.

Is This A Good Question/Topic? 0
  • +

Replies To: How to override correctly?

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: How to override correctly?

Posted 23 March 2009 - 04:29 PM

Never seen a main() method in an Abstarct class but why not no reason for not working
And what is Horde that Orc extends ?
Was This Post Helpful? 0
  • +
  • -

#3 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: How to override correctly?

Posted 23 March 2009 - 04:32 PM

View Postpbl, on 23 Mar, 2009 - 03:29 PM, said:

Never seen a main() method in an Abstarct class but why not no reason for not working
And what is Horde that Orc extends ?

Lol, You are so not a WoW player ;)
Horde is one of the 2 sides, an Orc is one of the characters playable in the game. I take it horde is his Superclass.
WoW 101 for ye :P

This post has been edited by Fuzzyness: 23 March 2009 - 04:33 PM

Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: How to override correctly?

Posted 23 March 2009 - 04:34 PM

View PostFuzzyness, on 23 Mar, 2009 - 03:32 PM, said:

View Postpbl, on 23 Mar, 2009 - 03:29 PM, said:

Never seen a main() method in an Abstarct class but why not no reason for not working
And what is Horde that Orc extends ?

Lol, You are so not a WoW player ;)
Horde is one of the 2 sides, an Orc is one of the characters playable in the game. I take it horde is his Superclass.
WoW 101 for ye :P

Are you telling me that Horde extends WoWCharacter ?
If yes you have a big design problem here :)
Was This Post Helpful? 0
  • +
  • -

#5 Valacom  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 23-March 09

Re: How to override correctly?

Posted 23 March 2009 - 04:38 PM

View Postpbl, on 23 Mar, 2009 - 03:29 PM, said:

Never seen a main() method in an Abstarct class but why not no reason for not working
And what is Horde that Orc extends ?


Yes sorry horde is orcs superclass i dont have any code done for that yet as im still trying to figure out the override.

Any who my horde superclass is supposed to contain the startcity=ogrimar instance variable and that's all.

The reason i'm donig it as such is because he gave us a direction sheet that looks like this.

WoWCharacter(abstract)
/
Horde
/
Orc(with overridden speak)

This post has been edited by Valacom: 23 March 2009 - 04:39 PM

Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: How to override correctly?

Posted 23 March 2009 - 04:52 PM

This is damgourous..
A class should never reference its descendants

- a Orc may know what a Horde and a WhoWCharacter are
- a Horde may know what a WhoWCharacter is

But a WhoHCharacter should never know what a Horde or a Orc is... it defeats all OO inheritance principles{ your sons have a "is a" relationship with you but not you with them)

Suppose you decide to get ride of the class Orc ... then WhoHCharacter wont compile anymore.... not a good idea at all
Was This Post Helpful? 0
  • +
  • -

#7 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1156
  • View blog
  • Posts: 2,538
  • Joined: 05-May 05

Re: How to override correctly?

Posted 23 March 2009 - 05:57 PM

Quote

Never seen a main() method in an Abstarct class but why not no reason for not working


Hahaha. Looks weird. You can run the subclass without exception, main() is inherited just like any other public super class method.

Where's your Horde class? Define it and override any abstract methods defined in its super class. If your a non-abstract subclass then you have to implement your parent's abstract methods. But, if your an abstract subclass, you don't and if you have children that are non-abstract they must implement them, for example:

abstract class Parent
{
	abstract void smack();
}
abstract class Child
{
	//I don't have to implement smack() since I'm abstract
	//but my concrete child better!
}
class BadA$$ extends Child
{
	void smack(){}
}

This post has been edited by blackcompe: 23 March 2009 - 05:58 PM

Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: How to override correctly?

Posted 23 March 2009 - 06:17 PM

an even if blackcompe is right move to main() method to Orc
that will make things a lot clearer
Was This Post Helpful? 0
  • +
  • -

#9 spikesanguine  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 16
  • Joined: 10-October 08

Re: How to override correctly?

Posted 23 March 2009 - 08:25 PM

Really you'd like it to be Orc extends Horde, Horde extends WowCharacter, create methods and attributes that are common to each (It's helpful to think, "All Orcs are Horde, all horde are enemies of the alliance and are all WoWCharacters, all WoWCharacters can walk, can pick up items, can open inventory, etc.)
Also, to practice good OO principals, you should make a test class or main class that executes and manipulates the objects. Have it create an orc object and initialize name, class, level, etc. Then have the newly created orc invoke his growl() method.
Was This Post Helpful? 1
  • +
  • -

#10 virgul  Icon User is offline

  • D.I.C Regular

Reputation: 44
  • View blog
  • Posts: 269
  • Joined: 18-March 09

Re: How to override correctly?

Posted 23 March 2009 - 11:07 PM

wait, do you want to do an override, or polymorphism?

Because with the
speak()
it looks like you are trying to define how the type speaks, like a dog going woof, and a cat meowing.

If thats the case then youll prolly want to do something more like this


(in your abstract class)
public String speak()
{
	 return "grrrr";
}




then to override it you would do this (in say your orc class or where ever else you will need it, like night elf)
public String speak()
{
	return "GrRrR";
}



then to use this you would just say
System.out.printf("the Orc says %s",Orc.speak())


This post has been edited by virgul: 23 March 2009 - 11:08 PM

Was This Post Helpful? 0
  • +
  • -

#11 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1156
  • View blog
  • Posts: 2,538
  • Joined: 05-May 05

Re: How to override correctly?

Posted 24 March 2009 - 05:32 AM

Quote

wait, do you want to do an override, or polymorphism?


Polymorphism applies to overriding and inheritance implementation. Your speaking as if they weren't related.

This post has been edited by blackcompe: 24 March 2009 - 05:44 AM

Was This Post Helpful? 0
  • +
  • -

#12 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: How to override correctly?

Posted 24 March 2009 - 08:04 PM

View Postblackcompe, on 24 Mar, 2009 - 04:32 AM, said:

Quote

wait, do you want to do an override, or polymorphism?


Polymorphism applies to overriding and inheritance implementation. Your speaking as if they weren't related.

:^: Right on
That has been fully discussed before
Plymorphism is a concept/philosophy that can be implemented by overriding
Was This Post Helpful? 0
  • +
  • -

#13 virgul  Icon User is offline

  • D.I.C Regular

Reputation: 44
  • View blog
  • Posts: 269
  • Joined: 18-March 09

Re: How to override correctly?

Posted 25 March 2009 - 03:30 PM

wow, just re read my post, that was a stupid slip...

lol, thanks for clearing that up for me too.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1