Jump to content

Problem with subclass

- - - - -

  • Please log in to reply
16 replies to this topic

#1
toto_7

toto_7

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 295 posts
Hello,

I have one class Message, which takes as parameter a String. If that String is larger than 50 char, i want to create an element of class BigMessage. Now if also that String is larger than 100 char, i want to throw MessageTooBig Exception. Somewhere i have a mistake, because when trying to run it, i have StackOverfrowException at 5th line of BigMessage.


My driver...:

try{

Message msg_1 = new Message("HelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHello");

}catch(MessageTooBigException e){

System.out.println("Message is too long!");

e.getStackTrace();}


Message...:

public Message(String text) throws MessageTooBigException{

		if(text.length()>50){

			[COLOR="red"]new BigMessage(text);[/COLOR] //Error here

		}

		else{

			this.text = text;

		}

	}


public class BigMessage extends Message{


	public BigMessage(String text) throws MessageTooBigException{

		[COLOR="red"]super(text);[/COLOR] //Error here

		if(text.length()>100){

			throw new MessageTooBigException("Message too big!");	

		}

	}


}



"Programming is like sex. One mistake and you have to support it for the rest of your life."

-Michael Sinz

#2
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
What does the constructor of Message look like?

#3
toto_7

toto_7

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 295 posts
is the second piece of code above

"Programming is like sex. One mistake and you have to support it for the rest of your life."

-Michael Sinz

#4
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
Ow yea ^^. First of all. StackOverflow means you're stuck in an infinite loop of methods calling methods.
In this case you create a Message, which in its constructor calls the constructor of BigMessage, which does super(), meaning it will call the constructor of Message, which will call the constructor of BigMessage, which will call Message's constructor, which will call BigMessage's constructor, which will call Message's constructor, which will call BigMessage's constructor, which will call Message's constructor, which will call BigMessage's constructor, which will call Message's constructor, which will call BigMessage's constructor, which will call Message's constructor, which will call BigMessage's constructor, which will call Message's constructor, which will call BigMessage's constructor
... you get the picture -> stackoverflow

2 ways to fix:

*static method to create message:

public class Message{

    public Message(String text){

        this.text = text;

    }


    public static Message createMessage(String text){

        if(text.length()>50){

	    return new BigMessage(text); 

	} else {

	    return new Message(text);

	}

    }

}

When creating a message you do this instead of "new..."

Message message = Message.createMessage("blablablablablablablablabla");

You can / should even make the constructor private then.



*Let BigMessage call another constructor.

        public BigMessage(String text) throws MessageTooBigException{

		super(text, true); //Whatever, just something different

		if(text.length()>100){

			throw new MessageTooBigException("Message too big!");	

		}

	}


public class Message{

    public Message(String text, boolean whatever){

           this.text = text;

    }

}



#5
toto_7

toto_7

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 295 posts
How stupid i am .... :s.. Thanks used the first way, but when text is larger than 100, MessageTooBig doesn't appear but print it out normally. How i can fix it?

"Programming is like sex. One mistake and you have to support it for the rest of your life."

-Michael Sinz

#6
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
put a System.out.println("k"); here to make sure the error gets thrown:
if(text.length()>100){

     throw new MessageTooBigException("Message too big!");	

     //here

}

If it doesn't print the length of the string outside the if (or debug to see the value)

#7
toto_7

toto_7

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 295 posts
When trying to add it, underlined and says unreachable code

"Programming is like sex. One mistake and you have to support it for the rest of your life."

-Michael Sinz

#8
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
Oops, yes. Put it above the throw, inside the if.

#9
toto_7

toto_7

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 295 posts
Nevermind .. :) nop, text is larger than 100 but "k" is nowhere

"Programming is like sex. One mistake and you have to support it for the rest of your life."

-Michael Sinz

#10
toto_7

toto_7

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 295 posts
Forget it wim. im sucks .. i had
if(text.length()>50 && text.length()<100)

My God i need a coffee :P

"Programming is like sex. One mistake and you have to support it for the rest of your life."

-Michael Sinz

#11
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
Well then
if(text.length()>100){
is false

OR you create a Message instead of BigMessage somehow.

#12
toto_7

toto_7

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 295 posts
No no, now is working fine just removed the
&& text.length()<100


Asks to create an example of polymorphism using BigMessage in my driver class. I know that polymorphism is instead call Message, im calling BigMessage. Or im wrong?

EDIT
I have add
Message message4 = new BigMessage("blablablabla..."); // larger than 50 char


is that right??

"Programming is like sex. One mistake and you have to support it for the rest of your life."

-Michael Sinz




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users