-2

While studying the oracle docs I ran into Casting objects and I don't understand the purpose of why people would use it.

so lets say in main you have

Person p2 = new Student();
Person p3 = new GraduateStudent();

and then you have separate classes

class Person
{

}

class Student extends Person
{

}

class GraduateStudent extends Student
{

}

I understand that a student is person and that a graduate student is a person but why would a person write

Person s1 = new Student()

instead of

Student s1 = new Student()
Rob
  • 35
  • 1
  • 6
  • Consider the reasons why you might ever cast an integer to a float or vice versa. – zero298 Sep 17 '18 at 20:17
  • The purpose is that sub-classes often have extra methods, and you want to access those methods. APIs are often made to be general, and will use a super-class. But you may need to cast to get at methods you know exist. Examples are casting a `List` to an `ArrayList`, or cast a `Graphics` object to a `Graphics2`. – markspace Sep 17 '18 at 20:17
  • What if you also had `class Professor extends Person` and you wanted to send an email to all teachers and students? You could just collect a list of Person instead of keeping separate collections of Student and Professor. – Roddy of the Frozen Peas Sep 17 '18 at 20:17
  • 2
    "but why would a person write `Person s1 = new Student()` instead of `Student s1 = new Student()`" possibly related: [What does it mean to “program to an interface”?](https://stackoverflow.com/q/383947) – Pshemo Sep 17 '18 at 20:18
  • 1
    Side note: what you describe is _not_ casting but rather simple ploymorphous assignment. Casting would be if you'd later do `Student casted = (Student)s1;`. – Thomas Sep 17 '18 at 20:18

2 Answers2

4

I think what you are asking is regarding polymorphous assigning in Java. A very good example that I've heard goes a little something like this:

Say for example you need to generate a payroll for every type of employee in your organization. Without polymorphism, it would be that you would need to have a function for every type of Employee in the organization. For example: For every Manager, you would need

public void generatePayroll(Manager manager)

For an Account employee, you would need

public void generatePayroll(Accountant accountant)

So then, you would almost immediately realize that you would be doing some tedious work by applying functionality of generating a payroll to every Employee. Instead, (and thank God for polymorphism), you can have a single method which then just takes in an Employee type since both Accountant and Manager both extend the Employee class. The signature would then change as such:

public void generatePayroll(Employee employee)

That way, you will be able to eliminate the need for having a generatePayroll method for every Employee level, and just stick to this one! (Remember, DRY is best!)

nishii24
  • 41
  • 2
0
Person s1 = new Student()

means s1 is a Person but maybe you don't need the compiler to know that it's also a Student

after declaring s1, if not final, you can redefine s1 like this :

s1 = new GraduateStudent()

Imagine, at school you lunch only in a room reserved to students, when people pass their pass card to enter the room, the machine only needs to know if the pass card is valid (i.e belongs to a student), but never search if he is graduated or not.

Mead
  • 100
  • 10