10 Replies - 1334 Views - Last Post: 18 February 2011 - 06:07 AM Rate Topic: -----

#1 fig79  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 75
  • Joined: 25-February 09

Overriding & Hiding Methods?

Posted 13 February 2011 - 01:33 AM

    public class Animal {
        public static void testClassMethod() {
            System.out.println("The class method in Animal.");
        }
        public void testInstanceMethod() {
            System.out.println("The instance method in Animal.");
        }
    }



The second class, a subclass of Animal, is called Cat:

    public class Cat extends Animal {
        public static void testClassMethod() {
            System.out.println("The class method in Cat.");
        }
        public void testInstanceMethod() {
            System.out.println("The instance method in Cat.");
        }

        public static void main(String[] args) {
            Cat myCat = new Cat();
            Animal myAnimal = myCat;
            Animal.testClassMethod();
            myAnimal.testInstanceMethod();
        }
    }


The Cat class overrides the instance method in Animal and hides the class method in Animal. The main method in this class creates an instance of Cat and calls testClassMethod() on the class and testInstanceMethod() on the instance.

The output from this program is as follows:
    The class method in Animal.
    The instance method in Cat.


As promised, the version of the hidden method that gets invoked is the one in the superclass, and the version of the overridden method that gets invoked is the one in the subclass

Above statement comes from here

My question :
1. Animal.testClassMethod(); is hiding method (yes or no)?
2. myAnimal.testInstanceMethod(); is overriding method (yes or no)?

I will wait your answer guys, then I will ask another question.

Is This A Good Question/Topic? 0
  • +

Replies To: Overriding & Hiding Methods?

#2 CodingSup3rnatur@l-360  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 992
  • View blog
  • Posts: 972
  • Joined: 30-September 10

Re: Overriding & Hiding Methods?

Posted 13 February 2011 - 04:09 AM

Hi,

Yes to both questions. We talk about hiding when using static methods, and overriding when we use instance methods.
Was This Post Helpful? 0
  • +
  • -

#3 masijade  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 196
  • View blog
  • Posts: 580
  • Joined: 03-April 10

Re: Overriding & Hiding Methods?

Posted 13 February 2011 - 12:04 PM

Well, that's one answer on his quiz. Maybe he should earn his grade.
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

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

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: Overriding & Hiding Methods?

Posted 13 February 2011 - 07:48 PM

Your methods are static so for sure

Animal.testClassMethod();

will call the method in animal

myAnimal.testClassMethod();

will have call the one of Cat
Was This Post Helpful? 0
  • +
  • -

#5 fig79  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 75
  • Joined: 25-February 09

Re: Overriding & Hiding Methods?

Posted 17 February 2011 - 04:50 AM

Thanks for you all who already answered my question.

The next question :
Animal myAnimal = myCat;


1. Could you explain to me. what is the meaning of the above statement?
2. What is the purpose?
Was This Post Helpful? 0
  • +
  • -

#6 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1011
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: Overriding & Hiding Methods?

Posted 17 February 2011 - 10:13 AM

Why not read 2 short tutorials, one about class and instance members:
http://download.orac.../classvars.html
and the second about inheritance (will answer your new questions):
http://download.orac...subclasses.html

You will find the answers to your questions and much more.
If after reading these tutorials you still have some trouble understanding anything, feel free to post again :)
Was This Post Helpful? 0
  • +
  • -

#7 fig79  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 75
  • Joined: 25-February 09

Re: Overriding & Hiding Methods?

Posted 18 February 2011 - 01:23 AM

@japanir
I have read it, but I still don't understand the meaning behind
Animal myAnimal = myCat;
?

What I understand is
- Cat myCat = new Cat() ---> Animal object & Cat object are created, because Cat is subclass from Animal.
Was This Post Helpful? 0
  • +
  • -

#8 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1011
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: Overriding & Hiding Methods?

Posted 18 February 2011 - 04:26 AM

In the inheritance tutorial go to the Casting Objects section. You'll find your answer.
http://download.orac...subclasses.html
Was This Post Helpful? 0
  • +
  • -

#9 fig79  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 75
  • Joined: 25-February 09

Re: Overriding & Hiding Methods?

Posted 18 February 2011 - 04:39 AM

@japanir

Animal myAnimal = myCat --> myAnimal is both Animal's object & Cat's object, am I correct?
Was This Post Helpful? 0
  • +
  • -

#10 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1011
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: Overriding & Hiding Methods?

Posted 18 February 2011 - 04:53 AM

Yes, that is called implicit casting.
That line
Animal myAnimal = myCat;

is possible, since Cat is also an Animal (extends Animal).
This line however,
Cat c = new Animal();

will give you an error, since Animal is NOT a Cat object.
Was This Post Helpful? 1
  • +
  • -

#11 fig79  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 75
  • Joined: 25-February 09

Re: Overriding & Hiding Methods?

Posted 18 February 2011 - 06:07 AM

View Postjapanir, on 18 February 2011 - 04:53 AM, said:

Yes, that is called implicit casting.
That line
Animal myAnimal = myCat;

is possible, since Cat is also an Animal (extends Animal).
This line however,
Cat c = new Animal();

will give you an error, since Animal is NOT a Cat object.


Thanks for your explanation.
But, why use like this
Animal myAnimal = myCat;

instead
Cat myCat = new Cat();
Animal myAnimal = New Animal();


So if want to use method in Animal class --> just use keyword myAnimal
and if want to use method in Cat class --> just use keyword myCat
It makes clearly what part of class's method is used.

This post has been edited by fig79: 18 February 2011 - 09:32 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1