Results 1 to 5 of 5
  1. #1
    kkid is offline Senior Member
    Join Date
    Jul 2012
    Posts
    241
    Rep Power
    3

    Default Pass a superclass as a parameter?

    Hello,

    I am just starting to use inheritance with my objects and wondered it it was possible to do what I propose?


    I am making three objects (Obj1, Obj2, Obj3). Obj3 is a subclass (extends) Obj2 and Obj2 extends Obj1


    i.e.
    Obj1
    |to|
    Obj2
    |to|
    Obj3


    Each of these classes will have a variable named "location" or a method called "getLocation()" if it would make a difference. I want to use this variable from another class "Cla1".

    Cha1 will have a method "locationPlus1()"

    Would I be able to to make locationPlus1() with the parameter as Obj1 and thus it would be compatible with the subclasses of Obj1 too?


    Therefore, if Obj1/2/3 had a variable "location", "locationPlus1()" could be written like this...


    Java Code:
    public int locationPlus1(Obj1 object){
         return object.location;
    }




    Would this work?
    Or would it be possible If I made Obj1 extend an abstract class?

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,021
    Rep Power
    6

    Default Re: Pass a superclass as a parameter?

    Methods can be overridden, fields can not. So it does indeed make a difference whether you are referencing an inherited field or invoking an overridden method. In your example above, locationPlus1() will always return the Obj1 classes location, even if the subclasses are shadowing (hiding the field) and setting their own value. So if you pass Obj2 or Obj3 instance to the method you will get Obj1's location value. However, if instead of calling object.location you called object.getLocation(), then the objects method that you passed will be invoked, assuming it has been overridden. IMO, it is not advisable to shadow inherited fields. The compiler should warn you about it.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    kkid is offline Senior Member
    Join Date
    Jul 2012
    Posts
    241
    Rep Power
    3

    Default Re: Pass a superclass as a parameter?

    Would it be best then to overload the locationPlus1 method - make 3 different variants of it each with a different parameter?

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,021
    Rep Power
    6

    Default Re: Pass a superclass as a parameter?

    I guess it depends on what you want it to do. For any class that overloads a method, all instances of that class will refer to that method no matter how
    they are represented in the hierarchy. So Obj1 foo = new Obj3(). foo.getLocation() invokes Obj3's getLocation method..

    I recommend you simply try some simple inheritance hierarchies with overridden methods to see how they work.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,021
    Rep Power
    6

    Default Re: Pass a superclass as a parameter?

    One more important concept. Say a Child inherits from Parent and overrides a parent method, say foo(). Now lets say child invokes some other method, bar(), in parent that child did not override. If in the method bar(), the parent now invokes the overridden method, foo(), the child's version, not the parents, is used. This is one of the reasons one must be very careful in overriding methods. Often, but not always, the first statement in an overridden method is to invoke the parents method using super. In general, overriding methods may have undesirable consequences unless they are written and documented to be overridden.

    Java Code:
    class Parent {
        public void bar() {
             foo();
        }
        public void foo() {
             System.out.println("Parent's method foo");
        }
    }
    
    class Child extends Parent {
        public void foo() {
             System.out.println("Child's method foo");
        }
    }
    
    Child child = new Child();
    child.bar();  // prints - Child's method foo.
    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Replies: 10
    Last Post: 01-10-2012, 05:44 PM
  2. Replies: 2
    Last Post: 11-04-2011, 12:49 AM
  3. Replies: 1
    Last Post: 03-11-2011, 02:55 PM
  4. how to pass parameter from one jsp to another jsp
    By kader_sit in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 06-10-2010, 03:04 PM
  5. I can't seem to pass a file through a parameter
    By daletron3030 in forum New To Java
    Replies: 5
    Last Post: 01-15-2009, 07:07 AM

Posting Permissions

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