• Log InLog In
  • Register
Liquid`
Team Liquid Liquipedia
EST 15:12
CET 21:12
KST 05:12
  • 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
RSL Revival - 2025 Season Finals Preview8RSL Season 3 - Playoffs Preview0RSL Season 3 - RO16 Groups C & D Preview0RSL Season 3 - RO16 Groups A & B Preview2TL.net Map Contest #21: Winners12
Community News
Weekly Cups (Dec 15-21): Classic wins big, MaxPax & Clem take weeklies1ComeBackTV's documentary on Byun's Career !10Weekly Cups (Dec 8-14): MaxPax, Clem, Cure win4Weekly Cups (Dec 1-7): Clem doubles, Solar gets over the hump1Weekly Cups (Nov 24-30): MaxPax, Clem, herO win2
StarCraft 2
General
Weekly Cups (Dec 15-21): Classic wins big, MaxPax & Clem take weeklies ComeBackTV's documentary on Byun's Career ! Micro Lags When Playing SC2? When will we find out if there are more tournament Weekly Cups (Dec 8-14): MaxPax, Clem, Cure win
Tourneys
$5,000+ WardiTV 2025 Championship Sparkling Tuna Cup - Weekly Open Tournament $100 Prize Pool - Winter Warp Gate Masters Showdow Winter Warp Gate Amateur Showdown #1 RSL Offline Finals Info - Dec 13 and 14!
Strategy
Custom Maps
Map Editor closed ?
External Content
Mutation # 505 Rise From Ashes Mutation # 504 Retribution Mutation # 503 Fowl Play Mutation # 502 Negative Reinforcement
Brood War
General
BGH Auto Balance -> http://bghmmr.eu/ Klaucher discontinued / in-game color settings Anyone remember me from 2000s Bnet EAST server? How Rain Became ProGamer in Just 3 Months FlaSh on: Biggest Problem With SnOw's Playstyle
Tourneys
[BSL21] LB QuarterFinals - Sunday 21:00 CET Small VOD Thread 2.0 [Megathread] Daily Proleagues [BSL21] WB SEMIFINALS - Saturday 21:00 CET
Strategy
Simple Questions, Simple Answers Game Theory for Starcraft Current Meta Fighting Spirit mining rates
Other Games
General Games
Nintendo Switch Thread Stormgate/Frost Giant Megathread Beyond All Reason Path of Exile 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
Mafia Game Mode Feedback/Ideas Survivor II: The Amazon Sengoku Mafia TL Mafia Community Thread
Community
General
US Politics Mega-thread The Games Industry And ATVI Russo-Ukrainian War Thread Things Aren’t Peaceful in Palestine YouTube Thread
Fan Clubs
White-Ra Fan Club
Media & Entertainment
Anime Discussion Thread [Manga] One Piece Movie Discussion!
Sports
2024 - 2026 Football Thread Formula 1 Discussion
World Cup 2022
Tech Support
Computer Build, Upgrade & Buying Resource Thread
TL Community
TL+ Announced Where to ask questions and add stream?
Blogs
The (Hidden) Drug Problem in…
TrAiDoS
I decided to write a webnov…
DjKniteX
James Bond movies ranking - pa…
Topin
Thanks for the RSL
Hildegard
Customize Sidebar...

Website Feedback

Closed Threads



Active: 1538 users

[Java] Brain teaser! - Page 2

Blogs > Qzy
Post a Reply
Prev 1 2 All
NoUShutUp
Profile Joined January 2008
United States172 Posts
September 22 2010 17:02 GMT
#21
New idea... just type it into a java compiler and test it out >_>
Cambium
Profile Blog Joined June 2004
United States16368 Posts
Last Edited: 2010-09-22 17:10:00
September 22 2010 17:04 GMT
#22
On September 23 2010 01:55 Cambium wrote:
Show nested quote +
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!


Going along with Logo's idea, the problem is that you need to add a try/catch block or make your caller method throw an Exception.


public class Runner {
@SuppressWarnings("unchecked")
public static void main(String[] args) throws Exception {
Student student = new Student();
@SuppressWarnings("unused")
TAStudent ta = new TAStudent();
PhDStudent phd = new PhDStudent();

student = ((Class<Student>) ((Class<TAStudent>) ((Class<PhDStudent>) phd
.getClass()).getSuperclass()).getSuperclass()).newInstance();
student.display();
}
}



Output:

I am a regular student.


Reflection is such a hack =\
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 17:04 GMT
#23
On September 23 2010 02:02 NoUShutUp wrote:
New idea... just type it into a java compiler and test it out >_>


logo's right; it's the basic principles of polymorphism.
When you want something, all the universe conspires in helping you to achieve it.
Qzy
Profile Blog Joined July 2010
Denmark1121 Posts
Last Edited: 2010-09-22 17:31:50
September 22 2010 17:31 GMT
#24
On September 23 2010 02:04 Cambium wrote:
Show nested quote +
On September 23 2010 01:55 Cambium wrote:
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!


Going along with Logo's idea, the problem is that you need to add a try/catch block or make your caller method throw an Exception.


public class Runner {
@SuppressWarnings("unchecked")
public static void main(String[] args) throws Exception {
Student student = new Student();
@SuppressWarnings("unused")
TAStudent ta = new TAStudent();
PhDStudent phd = new PhDStudent();

student = ((Class<Student>) ((Class<TAStudent>) ((Class<PhDStudent>) phd
.getClass()).getSuperclass()).getSuperclass()).newInstance();
student.display();
}
}



Output:

I am a regular student.


Reflection is such a hack =\


Wont work.. Im getting the exception. What does reflection do?


TAStudent ta = new TAStudent("1");
PhDStudent phd = new PhDStudent("2");
Student student = new Student("2");
try {
student = ((Class<Student>) ((Class<TAStudent>) ((Class<PhDStudent>) phd.getClass()).getSuperclass()).getSuperclass()).newInstance();
student.display();
} catch (IllegalAccessException ex) {
Logger.getLogger(PhDStudent.class.getName()).log(Level.SEVERE, null, ex);
}


TG Sambo... Intel classic! Life of lively to live to life of full life thx to shield battery
NoUShutUp
Profile Joined January 2008
United States172 Posts
September 22 2010 17:42 GMT
#25
On September 23 2010 02:04 Cambium wrote:
Show nested quote +
On September 23 2010 02:02 NoUShutUp wrote:
New idea... just type it into a java compiler and test it out >_>


logo's right; it's the basic principles of polymorphism.


I know it is... it just bugs me cause I don't remember :p
FreeZEternal
Profile Joined January 2003
Korea (South)3396 Posts
September 22 2010 17:44 GMT
#26
student = true ? student : phd;
Qzy
Profile Blog Joined July 2010
Denmark1121 Posts
Last Edited: 2010-09-22 17:51:46
September 22 2010 17:51 GMT
#27
On September 23 2010 02:44 FreeZEternal wrote:
student = true ? student : phd;


Read whole thread... . student and phd needs to be on the left and right side, respectively.
TG Sambo... Intel classic! Life of lively to live to life of full life thx to shield battery
zcxvbn
Profile Joined August 2009
United States257 Posts
September 22 2010 18:10 GMT
#28
On September 23 2010 01:52 Qzy wrote:
Show nested quote +
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


It doesn't compile
NA: proberecall
Slithe
Profile Blog Joined February 2007
United States985 Posts
September 22 2010 18:34 GMT
#29
On September 23 2010 02:51 Qzy wrote:
Show nested quote +
On September 23 2010 02:44 FreeZEternal wrote:
student = true ? student : phd;


Read whole thread... . student and phd needs to be on the left and right side, respectively.


I see at least one instance of student on the left and one instance of phd on the right. Isn't that what the rule is saying? Unless you u saying that student cannot be on the right hand side at all?
Qzy
Profile Blog Joined July 2010
Denmark1121 Posts
September 22 2010 18:48 GMT
#30
On September 23 2010 03:34 Slithe wrote:
Show nested quote +
On September 23 2010 02:51 Qzy wrote:
On September 23 2010 02:44 FreeZEternal wrote:
student = true ? student : phd;


Read whole thread... . student and phd needs to be on the left and right side, respectively.


I see at least one instance of student on the left and one instance of phd on the right. Isn't that what the rule is saying? Unless you u saying that student cannot be on the right hand side at all?


I think the point is to apply the object address to student-variable, and then some how call the objects super-super-method .
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 20:17 GMT
#31
On September 23 2010 02:31 Qzy wrote:
Show nested quote +
On September 23 2010 02:04 Cambium wrote:
On September 23 2010 01:55 Cambium wrote:
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!


Going along with Logo's idea, the problem is that you need to add a try/catch block or make your caller method throw an Exception.


public class Runner {
@SuppressWarnings("unchecked")
public static void main(String[] args) throws Exception {
Student student = new Student();
@SuppressWarnings("unused")
TAStudent ta = new TAStudent();
PhDStudent phd = new PhDStudent();

student = ((Class<Student>) ((Class<TAStudent>) ((Class<PhDStudent>) phd
.getClass()).getSuperclass()).getSuperclass()).newInstance();
student.display();
}
}



Output:

I am a regular student.


Reflection is such a hack =\


Wont work.. Im getting the exception. What does reflection do?


TAStudent ta = new TAStudent("1");
PhDStudent phd = new PhDStudent("2");
Student student = new Student("2");
try {
student = ((Class<Student>) ((Class<TAStudent>) ((Class<PhDStudent>) phd.getClass()).getSuperclass()).getSuperclass()).newInstance();
student.display();
} catch (IllegalAccessException ex) {
Logger.getLogger(PhDStudent.class.getName()).log(Level.SEVERE, null, ex);
}




What's the exception? It's probably throwing an exception because the constructors take in parameters (you never specified this in the original problem).
When you want something, all the universe conspires in helping you to achieve it.
Qzy
Profile Blog Joined July 2010
Denmark1121 Posts
Last Edited: 2010-09-22 20:26:49
September 22 2010 20:26 GMT
#32
On September 23 2010 05:17 Cambium wrote:
Show nested quote +
On September 23 2010 02:31 Qzy wrote:
On September 23 2010 02:04 Cambium wrote:
On September 23 2010 01:55 Cambium wrote:
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!


Going along with Logo's idea, the problem is that you need to add a try/catch block or make your caller method throw an Exception.


public class Runner {
@SuppressWarnings("unchecked")
public static void main(String[] args) throws Exception {
Student student = new Student();
@SuppressWarnings("unused")
TAStudent ta = new TAStudent();
PhDStudent phd = new PhDStudent();

student = ((Class<Student>) ((Class<TAStudent>) ((Class<PhDStudent>) phd
.getClass()).getSuperclass()).getSuperclass()).newInstance();
student.display();
}
}



Output:

I am a regular student.


Reflection is such a hack =\


Wont work.. Im getting the exception. What does reflection do?


TAStudent ta = new TAStudent("1");
PhDStudent phd = new PhDStudent("2");
Student student = new Student("2");
try {
student = ((Class<Student>) ((Class<TAStudent>) ((Class<PhDStudent>) phd.getClass()).getSuperclass()).getSuperclass()).newInstance();
student.display();
} catch (IllegalAccessException ex) {
Logger.getLogger(PhDStudent.class.getName()).log(Level.SEVERE, null, ex);
}




What's the exception? It's probably throwing an exception because the constructors take in parameters (you never specified this in the original problem).


Yeah sorry, it takes a string - does that matter?
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 20:39 GMT
#33
On September 23 2010 05:26 Qzy wrote:
Show nested quote +
On September 23 2010 05:17 Cambium wrote:
On September 23 2010 02:31 Qzy wrote:
On September 23 2010 02:04 Cambium wrote:
On September 23 2010 01:55 Cambium wrote:
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!


Going along with Logo's idea, the problem is that you need to add a try/catch block or make your caller method throw an Exception.


public class Runner {
@SuppressWarnings("unchecked")
public static void main(String[] args) throws Exception {
Student student = new Student();
@SuppressWarnings("unused")
TAStudent ta = new TAStudent();
PhDStudent phd = new PhDStudent();

student = ((Class<Student>) ((Class<TAStudent>) ((Class<PhDStudent>) phd
.getClass()).getSuperclass()).getSuperclass()).newInstance();
student.display();
}
}



Output:

I am a regular student.


Reflection is such a hack =\


Wont work.. Im getting the exception. What does reflection do?


TAStudent ta = new TAStudent("1");
PhDStudent phd = new PhDStudent("2");
Student student = new Student("2");
try {
student = ((Class<Student>) ((Class<TAStudent>) ((Class<PhDStudent>) phd.getClass()).getSuperclass()).getSuperclass()).newInstance();
student.display();
} catch (IllegalAccessException ex) {
Logger.getLogger(PhDStudent.class.getName()).log(Level.SEVERE, null, ex);
}




What's the exception? It's probably throwing an exception because the constructors take in parameters (you never specified this in the original problem).


Yeah sorry, it takes a string - does that matter?


Yes it does, because the newInstance() method throws IllegalAccessException if the class or its nullary constructor is not accessible. In this case, because you have an explicit constructor, we cannot access the default constructor.
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 20:43 GMT
#34
To fix it, you need to retrieve the constructor from the Class object and pass in the argument in the Constructor object's newInstance() method


student = ((Class<Student>) ((Class<TAStudent>) ((Class<PhDStudent>) phd.getClass()).getSuperclass()).getSuperclass()).getDeclaredConstructors()[0].newInstance({"1"});
When you want something, all the universe conspires in helping you to achieve it.
Prev 1 2 All
Please log in or register to reply.
Live Events Refresh
Monday Night Weeklies
17:00
#35
RotterdaM1029
TKL 412
SteadfastSC257
IndyStarCraft 226
kabyraGe 164
BRAT_OK 115
EnkiAlexander 27
LiquipediaDiscussion
[ Submit Event ]
Live Streams
Refresh
StarCraft 2
RotterdaM 1029
mouzHeroMarine 472
TKL 412
SteadfastSC 257
IndyStarCraft 226
MaxPax 136
BRAT_OK 115
trigger 33
UpATreeSC 32
StarCraft: Brood War
Larva 374
Dewaltoss 119
firebathero 103
Mini 74
910 29
Yoon 19
Dota 2
syndereN671
Heroes of the Storm
Liquid`Hasu255
Other Games
Grubby5746
FrodaN1984
fl0m946
Beastyqt814
ceh9514
B2W.Neo309
C9.Mang0118
QueenE88
Mew2King75
Trikslyr43
ZombieGrub20
Organizations
Other Games
BasetradeTV68
StarCraft 2
Blizzard YouTube
StarCraft: Brood War
BSLTrovo
sctven
[ Show 18 non-featured ]
StarCraft 2
• StrangeGG 41
• HeavenSC 11
• Reevou 2
• Kozan
• AfreecaTV YouTube
• intothetv
• sooper7s
• IndyKCrew
• LaughNgamezSOOP
• Migwel
StarCraft: Brood War
• 80smullet 20
• Azhi_Dahaki4
• STPLYoutube
• ZZZeroYoutube
• BSLYoutube
Dota 2
• WagamamaTV1622
Other Games
• imaqtpie1552
• Shiphtur243
Upcoming Events
WardiTV Invitational
1d 15h
Gerald vs YoungYakov
Spirit vs MaNa
SHIN vs Percival
Creator vs Scarlett
Replay Cast
2 days
WardiTV Invitational
2 days
ByuN vs Solar
Clem vs Classic
Cure vs herO
Reynor vs MaxPax
Replay Cast
4 days
Sparkling Tuna Cup
5 days
Replay Cast
6 days
Wardi Open
6 days
Liquipedia Results

Completed

YSL S2
WardiTV 2025
META Madness #9

Ongoing

C-Race Season 1
IPSL Winter 2025-26
KCM Race Survival 2025 Season 4
BSL Season 21
Slon Tour Season 2
CSL Season 19: Qualifier 2
eXTREMESLAND 2025
SL Budapest Major 2025
ESL Impact League Season 8
BLAST Rivals Fall 2025
IEM Chengdu 2025
PGL Masters Bucharest 2025
Thunderpick World Champ.
CS Asia Championships 2025
ESL Pro League S22

Upcoming

CSL 2025 WINTER (S19)
BSL 21 Non-Korean Championship
Acropolis #4
IPSL Spring 2026
Bellum Gens Elite Stara Zagora 2026
HSC XXVIII
Big Gabe Cup #3
OSC Championship Season 13
Nations Cup 2026
ESL Pro League Season 23
PGL Cluj-Napoca 2026
IEM Kraków 2026
BLAST Bounty Winter 2026
BLAST Bounty Winter Qual
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.