Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass a derived class to a function accepting the parent type

Tags:

java

android

I have a Sport base class that has a couple variables. I also have a few different classes derived from Sport such as Volleyball, Soccer, etc. that have additional methods and variables. They look somewhat like this.

public class Sport {
    public String commonVar;
    public Sport(String c) {
        this.commonVar = c;
    }
}

And then Volleyball:

public class Volleyball extends Sport {
    public String onlyVolleyball;
    public Volleyball(String c, String thisVar) {
        super(c);
        this.onlyVolleyball = thisVar;
    }
}

Now I want to be passing the derived classes of Sport into a function elsewhere, but making the parameters

public otherFunction(int one, int two, Sport sport) { }

Only treats sport as a Sport object (it will accept the derived classes but I cannot access any of the variables and methods not common to Sport). My question is how do I create a function that I am able to send all types of Sport derivatives to without losing any data?

like image 281
Ahorner Avatar asked Dec 20 '12 21:12

Ahorner


People also ask

How do you call a parent class from a derived class?

A derived class is created, which is inheriting parent class p1 and overloading the parent class function first(). class d1 : public p1 { public: void first() { cout << "The derived class d1 function is called."; p1::first(); } }; The function of d1 class is calling the function of p1 class.

Can a derived class access its parent class's private field?

Derived class can not access the private members of it's base class. No type of inheritance allows access to private members.

Can a derived class access the parent's private data members?

Private members of the base class cannot be used by the derived class unless friend declarations within the base class explicitly grant access to them.

Does a derived class inherit all methods of its parent base class?

Data Abstraction and Object Orientation Here queue is said to be a derived class (also called a child class or subclass); list is said to be a base class (also called a parent class or superclass). The derived class automatically has all the fields and methods of the base class.


3 Answers

You can't, but you can check if the object is actually a member of the extending class, then cast it and use it, as follows:

public otherFunction(int one, int two, Sport sport)
{
    if(sport instanceof Volleyball)
    {
        Volleyball volley = (Volleyball) sport;
        //Do Volleyball things
    }
    else
    {
        //Only do sport things.
    }
}
like image 154
MrLore Avatar answered Oct 05 '22 14:10

MrLore


You shouldn't. If that function is to work with the abstraction Sport then all the information it needs should be available in Sport. Else you should really have functions (or strategies/handlers or similar) for each.

You are breaking the Open Closed Principle (the O in SOLID) in object orientation principles.

You CAN do it though, with instanceof checks and then cast:

if (sport instanceof Volleyball) {
  Volleyball v = (Volleyball)sport;
}
like image 20
Mattias Isegran Bergander Avatar answered Oct 05 '22 14:10

Mattias Isegran Bergander


Basically, solution to your problem depends on what do you really want to achieve.

At the same time, considering your post, in my opinion you are attempting to mix different responsibilities, which are:

  1. Handling Sport instances.
  2. Handling derivatives of Sport instances.

Possibly this means that your method does not require Sport instances at all. What does it actually need is access to specific sports. Correct me if I'm wrong.

Therefore most simple solution would be to declare otherFunction like following:

public otherFunction(int one, int two, VolleyBall sport)
public otherFunction(int one, int two, Soccer sport)
public otherFunction(int one, int two, BasketBall sport)
public otherFunction(int one, int two, AnyOtherAwsomeSport sport)

In this way you are clearly telling what kind of sports your function is handling and moreover you have quite clear interface that is easy to understand and maintain.

like image 28
rawo Avatar answered Oct 05 '22 15:10

rawo