1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Java Method and Subclass

  1. Mar 1, 2015 #1
    1. The problem statement, all variables and given/known data

    Suppose I have a subclass called, Cashier, and a super class named, Clerk. How do I make a method that is called on a Cashier object and takes in an instance of Cashier? What method has a structure like this?


    2. Relevant equations

    None


    3. The attempt at a solution

    This method is going to place a competition between the subclass Cashier and the superclass Clerk and returns Cashier member that won the competition.

    I have my particular competition created, by creating a helper method but I just don't know how to apply the problem statement above.

    Code (Text):
    public class Cashier {
    ...
    }

    public class Clerk extends Cashier {
    ...
    }
     
  2. jcsd
  3. Mar 1, 2015 #2

    jedishrfu

    Staff: Mentor

    The first problem is you said the super class (aka parent class should be Clerk) and the subclass (aka child class) should be Cashier.

    However you've written them the other way around. Basically in your code example you've said a Clerk is a kind of Cashier.

    In contrast, my code says a Cashier is a kind of Clerk

    Code (Java):
    public class Clerk {
    ...
    }

    public class Cashier extends Clerk {
    ...
        public boolean isWinner(Cashier employee) {
            boolean winStatus = false;
            ... code to set winStatus as true or false...
            return winStatus;
        }
    ...
    }
    The above code shows the isWinner() method take in as input the Cashier employee and return a boolean value.

    Is that what you mean?
     
    Last edited: Mar 1, 2015
  4. Mar 1, 2015 #3
    Opps, on the first part. You're right, Cashier should be a kind of clerk.

    Hm, what does it mean to make a method that is called on a subclass object and takes in an instance of Subclass? Also, I was told that, how can I handle the case where the object that the method is called on needs to be returned. Which doesn't really make much sense to me. Is there some type of keyword that refers to the current object being acted upon?
     
  5. Mar 1, 2015 #4

    jedishrfu

    Staff: Mentor

    Do you mean like the 'this' instance?
     
  6. Mar 1, 2015 #5
    That's the first thing that popped into my head but I am not sure how that "this" applies to the question that was asked.
     
  7. Mar 1, 2015 #6

    jedishrfu

    Staff: Mentor

    I'm not sure how I can help you without giving you the answer but here's the Shapes example that may give you some clues about how java
    can be used.

    Code (Java):

    public interface Shape { // interface declaration
        double area ();
    }

    class Circle extends Shape { // class declaration

        private double radius; // instance variable

        Circle (double radius) { // constructor
            this.radius = radius;
        }

        public double area () { // dynamic method
            return Math.PI * radius * radius;
        }
    }

    class Rectangle implements Shape {

        private double length; // length of one side
        private double width; // length of the other side

        Rectangle (double length, double width) {
            this.length = length;
            this.width = width;
        }

        public double area () {
            return length * width;
        }
    }

    // FindAreasOfShapes.java

    public class FindAreasOfShapes {
        public static void main (String[] args) {

            Shape circle = new Circle (2.0);
            Shape rect = new Rectangle (3.0, 5.0);

            System.out.println (circle.area());
            System.out.println (rect.area());
        }
    }
     
     
  8. Mar 1, 2015 #7
    THank you very much!
     
  9. Mar 1, 2015 #8

    jedishrfu

    Staff: Mentor

    How is it helpful?

    The key notion in the code is that you can use shape to reference the objects even though they are actually Circles and Rectangles. The Shape interface could also be defined as a super class and its subclasses would then need to override the area method.
     
  10. Mar 1, 2015 #9
    Lol, I thought you said that you couldn't help any further so I was thanking you for your time.

    I don't get the point in having the interface? If you are going to override it anyways in both scenarios then doesn't that make the interface pretty useless?
     
  11. Mar 1, 2015 #10

    jedishrfu

    Staff: Mentor

    You're missing the point of the interface. It defines a contract that says any class that wants to be like the interface MUST implements the methods the interface defines. It is used extensively in Java. The best example is the Runnable interface that should a class implement it must implement the run() method.

    In concert with Runnable, the Thread class can then be used to create a thread that runs the run() method of the class. The Thread method only needs to know that the instance it's given implements Runnable and thus has a run() method that can be called.

    Here's a great tutorial on Runnable and you might see uses in your own code

    http://tutorials.jenkov.com/java-concurrency/creating-and-starting-threads.html
     
  12. Mar 2, 2015 #11

    jedishrfu

    Staff: Mentor

    While a little off track from your original post, I thought you might like to see this on why interfaces are so important

    http://www.javaworld.com/article/2076841/core-java/designing-with-interfaces.html

    The idea is that interfaces can help you tighten up and manage your program design better and even allow you to make your code "plug-able" ie swap one class out that implements your interface with another that does things differently but still supports the same interface. In a sense design with interfaces and organize your classes by interfaces.
     
  13. Mar 2, 2015 #12
    Thank you very much. I will be reading those links and also YouTubing it as well, to try and understand what exactly the problem wants me to do.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted



Similar Discussions: Java Method and Subclass
  1. Java object method (Replies: 23)

Loading...