Results 1 to 3 of 3
  1. #1
    Splanger is offline Member
    Join Date
    Jul 2011
    Posts
    10
    Rep Power
    0

    Default Override methods

    Hi there,
    I'd like to ask a question. In one of tutorials on java applet I've been read, it gave an example:

    public class FirstApplet extends Applet {
    Image NewImage(){

    public void init() {
    ...


    And it said, that the line with method's name "init" is overriding the method "init" of Applet class.

    Can you please explain me, what does it mean in java "to override a method"?

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,184
    Rep Power
    19

  3. #3
    turk is offline Member
    Join Date
    Jul 2011
    Posts
    7
    Rep Power
    0

    Default

    Overriding a method in Java is making it more specific to the subclass.

    Let me give you an example.

    A given class called Rectangle and a method that returns its area:

    Java Code:
    public class Rectangle {
         ...
         public double area() { return a*b; } // Being [I]a[/I] and [I]b[/I] the size of heigh and weight.
         ...
    }
    Now we have a class called Square which is a specific class of Rectangle:

    Java Code:
    public class Square extends Rectangle {
         ...
         public double area() { return Math.pow(a, 2); } // Being [I]a[/I] the size of its side.
         ...
    }

    As you can see we overrode the method area in the subclass Square which inherits from Rectangle. Now if we create an object Square and call its method area it will execute the method in Square.
    If we hadn't define the method area in Square and we still create a Square object and call the area method, the code from Rectangle would be executed.
    Last edited by turk; 07-07-2011 at 03:40 PM.

Similar Threads

  1. Override a superclass's methods with a subclass
    By zach&kody in forum New To Java
    Replies: 7
    Last Post: 05-24-2011, 02:50 PM
  2. why we cann't override static methods
    By haoberoi in forum New To Java
    Replies: 2
    Last Post: 11-11-2008, 11:07 AM
  3. How To Override Methods, NetBeans 6.1
    By markw8500 in forum NetBeans
    Replies: 1
    Last Post: 08-16-2008, 11:58 PM
  4. Override Methods In NetBeans IDE
    By JavaForums in forum NetBeans
    Replies: 0
    Last Post: 07-30-2007, 11:13 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •