• Log InLog In
  • Register
Liquid`
Team Liquid Liquipedia
EST 22:33
CET 04:33
KST 12:33
  • Home
  • Forum
  • Calendar
  • Streams
  • Liquipedia
  • Features
  • Store
  • EPT
  • TL+
  • StarCraft 2
  • Brood War
  • Smash
  • Heroes
  • Counter-Strike
  • Overwatch
  • Liquibet
  • Fantasy StarCraft
  • TLPD
  • StarCraft 2
  • Brood War
  • Blogs
Forum Sidebar
Events/Features
News
Featured News
TL.net Map Contest #21: Winners4Intel X Team Liquid Seoul event: Showmatches and Meet the Pros10[ASL20] Finals Preview: Arrival13TL.net Map Contest #21: Voting12[ASL20] Ro4 Preview: Descent11
Community News
Starcraft, SC2, HoTS, WC3, returning to Blizzcon!21$5,000+ WardiTV 2025 Championship5[BSL21] RO32 Group Stage3Weekly Cups (Oct 26-Nov 2): Liquid, Clem, Solar win; LAN in Philly2Weekly Cups (Oct 20-26): MaxPax, Clem, Creator win9
StarCraft 2
General
TL.net Map Contest #21: Winners Starcraft, SC2, HoTS, WC3, returning to Blizzcon! RotterdaM "Serral is the GOAT, and it's not close" Weekly Cups (Oct 20-26): MaxPax, Clem, Creator win 5.0.15 Patch Balance Hotfix (2025-10-8)
Tourneys
Constellation Cup - Main Event - Stellar Fest $5,000+ WardiTV 2025 Championship Merivale 8 Open - LAN - Stellar Fest Sea Duckling Open (Global, Bronze-Diamond) $3,500 WardiTV Korean Royale S4
Strategy
Custom Maps
Map Editor closed ?
External Content
Mutation # 498 Wheel of Misfortune|Cradle of Death Mutation # 497 Battle Haredened Mutation # 496 Endless Infection Mutation # 495 Rest In Peace
Brood War
General
BGH Auto Balance -> http://bghmmr.eu/ SnOw's ASL S20 Finals Review [BSL21] RO32 Group Stage Practice Partners (Official) [ASL20] Ask the mapmakers — Drop your questions
Tourneys
[Megathread] Daily Proleagues [BSL21] RO32 Group B - Sunday 21:00 CET [BSL21] RO32 Group A - Saturday 21:00 CET BSL21 Open Qualifiers Week & CONFIRM PARTICIPATION
Strategy
PvZ map balance Current Meta How to stay on top of macro? Soma's 9 hatch build from ASL Game 2
Other Games
General Games
Stormgate/Frost Giant Megathread Dawn of War IV Nintendo Switch Thread ZeroSpace Megathread General RTS Discussion Thread
Dota 2
Official 'what is Dota anymore' discussion
League of Legends
Heroes of the Storm
Simple Questions, Simple Answers Heroes of the Storm 2.0
Hearthstone
Deck construction bug Heroes of StarCraft mini-set
TL Mafia
TL Mafia Community Thread SPIRED by.ASL Mafia {211640}
Community
General
Things Aren’t Peaceful in Palestine US Politics Mega-thread Russo-Ukrainian War Thread YouTube Thread Dating: How's your luck?
Fan Clubs
White-Ra Fan Club The herO Fan Club!
Media & Entertainment
Anime Discussion Thread Movie Discussion! [Manga] One Piece Korean Music Discussion Series you have seen recently...
Sports
2024 - 2026 Football Thread NBA General Discussion MLB/Baseball 2023 TeamLiquid Health and Fitness Initiative For 2023 Formula 1 Discussion
World Cup 2022
Tech Support
SC2 Client Relocalization [Change SC2 Language] Linksys AE2500 USB WIFI keeps disconnecting Computer Build, Upgrade & Buying Resource Thread
TL Community
The Automated Ban List Recent Gifted Posts
Blogs
Saturation point
Uldridge
DnB/metal remix FFO Mick Go…
ImbaTosS
Why we need SC3
Hildegard
Career Paths and Skills for …
TrAiDoS
Reality "theory" prov…
perfectspheres
Our Last Hope in th…
KrillinFromwales
Customize Sidebar...

Website Feedback

Closed Threads



Active: 1578 users

[Java] Brain teaser!

Blogs > Qzy
Post a Reply
1 2 Next All
Qzy
Profile Blog Joined July 2010
Denmark1121 Posts
September 22 2010 15:57 GMT
#1
Okay, my uni-teacher likes to give out small brain teasers, to think about. I'm pretty good with java, but this one I couldn't solve - so please do give a hint, if anyone does know the answer!

TAStudent ta; //Extends student
PhDStudent phd; //Extends TAStudent
Student student;

In TAStudent display() { System.out.println(“I am a TA”); }
In PhDStudent display() { System.out.println(“I am a PhD student”); }
In Student display() { System.out.println(“I am a regular student”); }

student = phd; // line 1
student.display(); // line 2

Modify, if necessary, line 1 in order to get the call to display() in line 2 run the method in the base class.

I guess they want us to outprint "I am a regular student"?

***
TG Sambo... Intel classic! Life of lively to live to life of full life thx to shield battery
Cambium
Profile Blog Joined June 2004
United States16368 Posts
September 22 2010 16:00 GMT
#2
use [code] tags please.
When you want something, all the universe conspires in helping you to achieve it.
Cambium
Profile Blog Joined June 2004
United States16368 Posts
Last Edited: 2010-09-22 16:05:19
September 22 2010 16:02 GMT
#3
On September 23 2010 00:57 Qzy wrote:
Okay, my uni-teacher likes to give out small brain teasers, to think about. I'm pretty good with java, but this one I couldn't solve - so please do give a hint, if anyone does know the answer!

TAStudent ta = new TAStudent(); //Extends student
PhDStudent phd = new PhDStudent(); //Extends TAStudent
Student student = new Student(); //assuming Student is not abstract

In TAStudent display() { System.out.println(“I am a TA”); }
In PhDStudent display() { System.out.println(“I am a PhD student”); }
In Student display() { System.out.println(“I am a regular student”); }

student = phd; // line 1
student.display(); // line 2

Modify, if necessary, line 1 in order to get the call to display() in line 2 run the method in the base class.

I guess they want us to outprint "I am a regular student"?


I don't really get the question, must you assign something to student? Otherwise, if you comment out line 1, you'd get exactly what you are looking for? If you can't comment it out, you can just do

student = student;
When you want something, all the universe conspires in helping you to achieve it.
Qzy
Profile Blog Joined July 2010
Denmark1121 Posts
September 22 2010 16:09 GMT
#4
On September 23 2010 01:02 Cambium wrote:
Show nested quote +
On September 23 2010 00:57 Qzy wrote:
Okay, my uni-teacher likes to give out small brain teasers, to think about. I'm pretty good with java, but this one I couldn't solve - so please do give a hint, if anyone does know the answer!

TAStudent ta = new TAStudent(); //Extends student
PhDStudent phd = new PhDStudent(); //Extends TAStudent
Student student = new Student(); //assuming Student is not abstract

In TAStudent display() { System.out.println(“I am a TA”); }
In PhDStudent display() { System.out.println(“I am a PhD student”); }
In Student display() { System.out.println(“I am a regular student”); }

student = phd; // line 1
student.display(); // line 2

Modify, if necessary, line 1 in order to get the call to display() in line 2 run the method in the base class.

I guess they want us to outprint "I am a regular student"?


I don't really get the question, must you assign something to student? Otherwise, if you comment out line 1, you'd get exactly what you are looking for? If you can't comment it out, you can just do

student = student;


It had to be cast, or method call - can't change ie student = student.
TG Sambo... Intel classic! Life of lively to live to life of full life thx to shield battery
fabiano
Profile Blog Joined August 2009
Brazil4644 Posts
September 22 2010 16:10 GMT
#5

public class Student
{
public void display()
{
System.out.println("I am a regular student.");
}
}

public class TAStudent extends Student
{
public void display()
{
System.out.println("I am a TA.");
}
}

public class PhDStudent extends TAStudent
{
public void display()
{
System.out.println("I am a PhD student.");
}
}

TAStudent ta;
PhDStudent phd;
Student student;

student = phd;
student.display();


This way is easier to picture the whole.
Im short on time, gotta get the bus in 5 minutes, so I will be trying to help more when I get to my University, if no one has answered yet.
"When the geyser died, a probe came out" - SirJolt
Qzy
Profile Blog Joined July 2010
Denmark1121 Posts
September 22 2010 16:15 GMT
#6
I'll just this line in (it's an extra line i didn't paste)

"Whatever modification you perform, be it a casting, another method call, etc., the
variable student MUST appear on the left side of the statement and the variable
phd MUST appear on the right side of the same statement."
TG Sambo... Intel classic! Life of lively to live to life of full life thx to shield battery
LastWish
Profile Blog Joined September 2004
2015 Posts
September 22 2010 16:24 GMT
#7
Ok I'm more of a C# programmer, so I might be wrong.
However in C# you must use virtual and override keywords to make inheritance work for methods properly.
So if the same applies for Java, then there is no need to modify line 1 - the program is correct.
The student.display() always calls the base method no matter which class it was instanced to.
- It's all just treason - They bring me down with their lies - Don't know the reason - My life is fire and ice -
NoUShutUp
Profile Joined January 2008
United States172 Posts
September 22 2010 16:30 GMT
#8
Hmm... I remember having something like this in my class a year ago. I'm not sure if I'm right but here is my thought process.

During compile time, Java only checks to see if types are valid and if there is a method call for that type. This means during compile time it checks to see if a Student can be a PhDStudent in line 1 and if the class Student has a display method. I am pretty sure both lines are valid.

During runtime in line 1, you are trying to store a PhDStudent into a Student. (This is where it gets iffy for me). I think you can cast phd to Student so then the variable student will be treated as a Student. Then when it calls on student.display(). It will recognize that student is still Student type and call on the base display method.
Qzy
Profile Blog Joined July 2010
Denmark1121 Posts
September 22 2010 16:31 GMT
#9
On September 23 2010 01:24 LastWish wrote:
Ok I'm more of a C# programmer, so I might be wrong.
However in C# you must use virtual and override keywords to make inheritance work for methods properly.
So if the same applies for Java, then there is no need to modify line 1 - the program is correct.
The student.display() always calls the base method no matter which class it was instanced to.


Yeah usually you write a @override, but that's not the point . The methods _are_ overridden Nicely spotted tho, sir!
TG Sambo... Intel classic! Life of lively to live to life of full life thx to shield battery
Cambium
Profile Blog Joined June 2004
United States16368 Posts
September 22 2010 16:31 GMT
#10
On September 23 2010 01:24 LastWish wrote:
Ok I'm more of a C# programmer, so I might be wrong.
However in C# you must use virtual and override keywords to make inheritance work for methods properly.
So if the same applies for Java, then there is no need to modify line 1 - the program is correct.
The student.display() always calls the base method no matter which class it was instanced to.


Java methods allow override by default.
When you want something, all the universe conspires in helping you to achieve it.
Cambium
Profile Blog Joined June 2004
United States16368 Posts
September 22 2010 16:37 GMT
#11
On September 23 2010 01:30 NoUShutUp wrote:
Hmm... I remember having something like this in my class a year ago. I'm not sure if I'm right but here is my thought process.

During compile time, Java only checks to see if types are valid and if there is a method call for that type. This means during compile time it checks to see if a Student can be a PhDStudent in line 1 and if the class Student has a display method. I am pretty sure both lines are valid.

During runtime in line 1, you are trying to store a PhDStudent into a Student. (This is where it gets iffy for me). I think you can cast phd to Student so then the variable student will be treated as a Student. Then when it calls on student.display(). It will recognize that student is still Student type and call on the base display method.


PhDStudent is a subclass of Student, regardless whether the phd object is casted to Student or TAStudent, the instance will always be of the PhD class. This is the underlying principle of polymorphism.
When you want something, all the universe conspires in helping you to achieve it.
Cambium
Profile Blog Joined June 2004
United States16368 Posts
Last Edited: 2010-09-22 16:48:19
September 22 2010 16:47 GMT
#12
As far as I know, there is no way to invoke Student's display method through the phd object.

This is what I would do:


student = phd == null ? student : student;

It satisfies the requirement of having student on the left side of the = operator, and phd on the right.

You might also be able to do something tricky with reflection by using the phd object to get the Student class's full name.
When you want something, all the universe conspires in helping you to achieve it.
Logo
Profile Blog Joined April 2010
United States7542 Posts
Last Edited: 2010-09-22 16:48:57
September 22 2010 16:47 GMT
#13
Probably cheating but...
student = phd.getClass().getSuperClass().getSuperClass().newInstance();

though as written it seems impossible/pointless because phd is null so you'd just use the logic of phd == null ?... explained in the post above mine.
Logo
AcrossFiveJulys
Profile Blog Joined September 2005
United States3612 Posts
September 22 2010 16:50 GMT
#14
as someone above said, either comment out the first line, or if you need to keep it as student = phd, you can do something cute like

student = (phd.super(phd)).super(phd);
gale_f0rce
Profile Joined June 2009
United States9 Posts
September 22 2010 16:51 GMT
#15
Isn't it just

student = (Student)phd;

Seems really simple
Qzy
Profile Blog Joined July 2010
Denmark1121 Posts
September 22 2010 16:52 GMT
#16
On September 23 2010 01:50 AcrossFiveJulys wrote:
student = (phd.super(phd)).super(phd);


Okay, give me like 1 hour to figure out what the heck that does :D
TG Sambo... Intel classic! Life of lively to live to life of full life thx to shield battery
Qzy
Profile Blog Joined July 2010
Denmark1121 Posts
September 22 2010 16:54 GMT
#17
On September 23 2010 01:51 gale_f0rce wrote:
Isn't it just

student = (Student)phd;

Seems really simple


Nooo, that would just throw out "I'm a phd student" .
TG Sambo... Intel classic! Life of lively to live to life of full life thx to shield battery
Cambium
Profile Blog Joined June 2004
United States16368 Posts
September 22 2010 16:55 GMT
#18
On September 23 2010 01:47 Logo wrote:
Probably cheating but...
student = phd.getClass().getSuperClass().getSuperClass().newInstance();


I like this, except you have to fight Java generics to get it working properly!
When you want something, all the universe conspires in helping you to achieve it.
gale_f0rce
Profile Joined June 2009
United States9 Posts
September 22 2010 17:00 GMT
#19
On September 23 2010 01:54 Qzy wrote:
Show nested quote +
On September 23 2010 01:51 gale_f0rce wrote:
Isn't it just

student = (Student)phd;

Seems really simple


Nooo, that would just throw out "I'm a phd student" .


No, the variable student is now of type Student, since (Student)phd is of type Student. This means that calling student.display() prints “I am a regular student.”
Logo
Profile Blog Joined April 2010
United States7542 Posts
Last Edited: 2010-09-22 17:05:04
September 22 2010 17:02 GMT
#20
On September 23 2010 02:00 gale_f0rce wrote:
Show nested quote +
On September 23 2010 01:54 Qzy wrote:
On September 23 2010 01:51 gale_f0rce wrote:
Isn't it just

student = (Student)phd;

Seems really simple


Nooo, that would just throw out "I'm a phd student" .


No, the variable student is now of type Student, since (Student)phd is of type Student. This means that calling student.display() prints “I am a regular student.”


It doesn't work like that =/. The reference to the object thinks it's a student, but the object itself is still a phd student.

Put it like this if you had
PhdStudent phd = new PhdStudent();
Student s = (Student)phd;
phd.displayName();

What would the 3rd line put out? Clearly not "I Am a regular student". Yet Both line 1 and line 2 point to the same object so how could they have different outputs.
Logo
1 2 Next All
Please log in or register to reply.
Live Events Refresh
Replay Cast
23:00
WardiTV Mondays #58
LiquipediaDiscussion
OSC
22:00
Masters Cup 150 Open Qual
Liquipedia
LAN Event
18:00
Day 3: Ursa 2v2, FFA
LiquipediaDiscussion
[ Submit Event ]
Live Streams
Refresh
StarCraft 2
RuFF_SC2 203
NeuroSwarm 137
ProTech128
StarCraft: Brood War
Calm 8081
Shuttle 690
Artosis 649
Sharp 422
actioN 422
Noble 93
Icarus 8
Dota 2
LuMiX1
Other Games
tarik_tv11441
summit1g10164
JimRising 447
WinterStarcraft271
C9.Mang0227
ViBE171
FrodaN134
Organizations
Other Games
gamesdonequick753
Counter-Strike
PGL152
StarCraft 2
Blizzard YouTube
StarCraft: Brood War
BSLTrovo
sctven
[ Show 14 non-featured ]
StarCraft 2
• Sammyuel 14
• AfreecaTV YouTube
• intothetv
• Kozan
• IndyKCrew
• LaughNgamezSOOP
• Migwel
• sooper7s
StarCraft: Brood War
• BSLYoutube
• STPLYoutube
• ZZZeroYoutube
Dota 2
• masondota21562
League of Legends
• Stunt304
Other Games
• Scarra526
Upcoming Events
OSC
8h 27m
LAN Event
11h 27m
Korean StarCraft League
23h 27m
CranKy Ducklings
1d 6h
LAN Event
1d 11h
IPSL
1d 14h
dxtr13 vs OldBoy
Napoleon vs Doodle
BSL 21
1d 16h
Gosudark vs Kyrie
Gypsy vs Sterling
UltrA vs Radley
Dandy vs Ptak
Replay Cast
1d 19h
Sparkling Tuna Cup
2 days
WardiTV Korean Royale
2 days
[ Show More ]
LAN Event
2 days
IPSL
2 days
JDConan vs WIZARD
WolFix vs Cross
BSL 21
2 days
spx vs rasowy
HBO vs KameZerg
Cross vs Razz
dxtr13 vs ZZZero
Replay Cast
3 days
Wardi Open
3 days
WardiTV Korean Royale
4 days
Replay Cast
5 days
Kung Fu Cup
5 days
Classic vs Solar
herO vs Cure
Reynor vs GuMiho
ByuN vs ShoWTimE
Tenacious Turtle Tussle
5 days
The PondCast
6 days
RSL Revival
6 days
Solar vs Zoun
MaxPax vs Bunny
Kung Fu Cup
6 days
WardiTV Korean Royale
6 days
Liquipedia Results

Completed

BSL 21 Points
SC4ALL: StarCraft II
Eternal Conflict S1

Ongoing

C-Race Season 1
IPSL Winter 2025-26
KCM Race Survival 2025 Season 4
SOOP Univ League 2025
YSL S2
Stellar Fest: Constellation Cup
IEM Chengdu 2025
PGL Masters Bucharest 2025
Thunderpick World Champ.
CS Asia Championships 2025
ESL Pro League S22
StarSeries Fall 2025
FISSURE Playground #2
BLAST Open Fall 2025
BLAST Open Fall Qual

Upcoming

BSL Season 21
SLON Tour Season 2
BSL 21 Non-Korean Championship
Acropolis #4
IPSL Spring 2026
HSC XXVIII
RSL Offline Finals
WardiTV 2025
RSL Revival: Season 3
META Madness #9
BLAST Bounty Winter 2026: Closed Qualifier
eXTREMESLAND 2025
ESL Impact League Season 8
SL Budapest Major 2025
BLAST Rivals Fall 2025
TLPD

1. ByuN
2. TY
3. Dark
4. Solar
5. Stats
6. Nerchio
7. sOs
8. soO
9. INnoVation
10. Elazer
1. Rain
2. Flash
3. EffOrt
4. Last
5. Bisu
6. Soulkey
7. Mini
8. Sharp
Sidebar Settings...

Advertising | Privacy Policy | Terms Of Use | Contact Us

Original banner artwork: Jim Warren
The contents of this webpage are copyright © 2025 TLnet. All Rights Reserved.