Skip to main content
deleted 42 characters in body
Source Link
Amir Popovich
  • 30.1k
  • 9
  • 58
  • 102

1.In the first example, why am I down casting Object types into Relatable types? What happens if the first method doesn't include the first two statements?

Answer
You are "up casting" and not "down casting". Every object has some basic functionality and you want a specific object write now. You are updown casting your object into a "Relatable" so you can use the "isLargerThan" method(an object wont have it since it has only basic common stuff). If you didn't up-castdown cast, you would not pass compilation.

2.Let's say I wrote a Rectangle class that implements the Relatable interface and has the "findLargest" method. If I know that I'm comparing two Rectangle objects, why not just make the first method downcast the objects into Rectangles instead?

Answer
Since you want to create something generic. Lets say you have a Student and a Driver. Both of them are People. You can create an interface called IPeople and make both the Student and the driver implement it. IPeople will have a method called "getAge()" that each of them will implement. IPeople will have all the functionality that you need for "People". That's how you create cross object functionality under the "same hat".

1.In the first example, why am I down casting Object types into Relatable types? What happens if the first method doesn't include the first two statements?

Answer
You are "up casting" and not "down casting". Every object has some basic functionality and you want a specific object write now. You are up casting your object into a "Relatable" so you can use the "isLargerThan" method(an object wont have it since it has only basic common stuff). If you didn't up-cast, you would not pass compilation.

2.Let's say I wrote a Rectangle class that implements the Relatable interface and has the "findLargest" method. If I know that I'm comparing two Rectangle objects, why not just make the first method downcast the objects into Rectangles instead?

Answer
Since you want to create something generic. Lets say you have a Student and a Driver. Both of them are People. You can create an interface called IPeople and make both the Student and the driver implement it. IPeople will have a method called "getAge()" that each of them will implement. IPeople will have all the functionality that you need for "People". That's how you create cross object functionality under the "same hat".

1.In the first example, why am I down casting Object types into Relatable types? What happens if the first method doesn't include the first two statements?

Answer
Every object has some basic functionality and you want a specific object write now. You are down casting your object into a "Relatable" so you can use the "isLargerThan" method(an object wont have it since it has only basic common stuff). If you didn't down cast, you would not pass compilation.

2.Let's say I wrote a Rectangle class that implements the Relatable interface and has the "findLargest" method. If I know that I'm comparing two Rectangle objects, why not just make the first method downcast the objects into Rectangles instead?

Answer
Since you want to create something generic. Lets say you have a Student and a Driver. Both of them are People. You can create an interface called IPeople and make both the Student and the driver implement it. IPeople will have a method called "getAge()" that each of them will implement. IPeople will have all the functionality that you need for "People". That's how you create cross object functionality under the "same hat".

added 243 characters in body
Source Link
Amir Popovich
  • 30.1k
  • 9
  • 58
  • 102

1.In the first example, why am I down casting Object types into Relatable types? What happens if the first method doesn't include the first two statements?

Answer
You are "up casting" and not "down casting". Every object has some basic functionality and you want a specific object write now. You are up casting your object into a "Relatable" so you can use the "isLargerThan" method(an object wont have it since it has only basic common stuff). If you didn't up-cast, you would not pass compilation.

2.Let's say I wrote a Rectangle class that implements the Relatable interface and has the "findLargest" method. If I know that I'm comparing two Rectangle objects, why not just make the first method downcast the objects into Rectangles instead?

Answer
Since you want to create something generic. Lets say you have a Student and a Driver. Both of them are People. You can create an interface called IPeople and make both the Student and the driver implement it. IPeople will have a method called "getAge()" that each of them will implement. IPeople will have all the functionality that you need for "People". That's how you create cross object functionality under the "same hat".

1.In the first example, why am I down casting Object types into Relatable types? What happens if the first method doesn't include the first two statements?

Answer
You are "up casting" and not "down casting". Every object has some basic functionality and you want a specific object. You are up casting your object into a "Relatable" so you can use the "isLargerThan" method(an object wont have it).

2.Let's say I wrote a Rectangle class that implements the Relatable interface and has the "findLargest" method. If I know that I'm comparing two Rectangle objects, why not just make the first method downcast the objects into Rectangles instead?

Answer
Since you want to create something generic. Lets say you have a Student and a Driver. Both of them are People. You can create an interface called IPeople and make both the Student and the driver implement it. IPeople will have a method called "getAge()" that each of them will implement.

1.In the first example, why am I down casting Object types into Relatable types? What happens if the first method doesn't include the first two statements?

Answer
You are "up casting" and not "down casting". Every object has some basic functionality and you want a specific object write now. You are up casting your object into a "Relatable" so you can use the "isLargerThan" method(an object wont have it since it has only basic common stuff). If you didn't up-cast, you would not pass compilation.

2.Let's say I wrote a Rectangle class that implements the Relatable interface and has the "findLargest" method. If I know that I'm comparing two Rectangle objects, why not just make the first method downcast the objects into Rectangles instead?

Answer
Since you want to create something generic. Lets say you have a Student and a Driver. Both of them are People. You can create an interface called IPeople and make both the Student and the driver implement it. IPeople will have a method called "getAge()" that each of them will implement. IPeople will have all the functionality that you need for "People". That's how you create cross object functionality under the "same hat".

Source Link
Amir Popovich
  • 30.1k
  • 9
  • 58
  • 102

1.In the first example, why am I down casting Object types into Relatable types? What happens if the first method doesn't include the first two statements?

Answer
You are "up casting" and not "down casting". Every object has some basic functionality and you want a specific object. You are up casting your object into a "Relatable" so you can use the "isLargerThan" method(an object wont have it).

2.Let's say I wrote a Rectangle class that implements the Relatable interface and has the "findLargest" method. If I know that I'm comparing two Rectangle objects, why not just make the first method downcast the objects into Rectangles instead?

Answer
Since you want to create something generic. Lets say you have a Student and a Driver. Both of them are People. You can create an interface called IPeople and make both the Student and the driver implement it. IPeople will have a method called "getAge()" that each of them will implement.