• Log InLog In
  • Register
Liquid`
Team Liquid Liquipedia
EDT 03:57
CEST 09:57
KST 16:57
  • 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
Team TLMC #5 - Finalists & Open Tournaments0[ASL20] Ro16 Preview Pt2: Turbulence10Classic Games #3: Rogue vs Serral at BlizzCon9[ASL20] Ro16 Preview Pt1: Ascent10Maestros of the Game: Week 1/Play-in Preview12
Community News
Weekly Cups (Sept 8-14): herO & MaxPax split cups4WardiTV TL Team Map Contest #5 Tournaments1SC4ALL $6,000 Open LAN in Philadelphia8Weekly Cups (Sept 1-7): MaxPax rebounds & Clem saga continues29LiuLi Cup - September 2025 Tournaments3
StarCraft 2
General
#1: Maru - Greatest Players of All Time Weekly Cups (Sept 8-14): herO & MaxPax split cups Team Liquid Map Contest #21 - Presented by Monster Energy SpeCial on The Tasteless Podcast Team TLMC #5 - Finalists & Open Tournaments
Tourneys
Maestros of The Game—$20k event w/ live finals in Paris SC4ALL $6,000 Open LAN in Philadelphia Sparkling Tuna Cup - Weekly Open Tournament WardiTV TL Team Map Contest #5 Tournaments RSL: Revival, a new crowdfunded tournament series
Strategy
Custom Maps
External Content
Mutation # 491 Night Drive Mutation # 490 Masters of Midnight Mutation # 489 Bannable Offense Mutation # 488 What Goes Around
Brood War
General
BGH Auto Balance -> http://bghmmr.eu/ Pros React To: SoulKey's 5-Peat Challenge [ASL20] Ro16 Preview Pt2: Turbulence BW General Discussion ASL20 General Discussion
Tourneys
[ASL20] Ro16 Group D [ASL20] Ro16 Group C [Megathread] Daily Proleagues SC4ALL $1,500 Open Bracket LAN
Strategy
Simple Questions, Simple Answers Muta micro map competition Fighting Spirit mining rates [G] Mineral Boosting
Other Games
General Games
Path of Exile Stormgate/Frost Giant Megathread General RTS Discussion Thread Nintendo Switch Thread Borderlands 3
Dota 2
Official 'what is Dota anymore' discussion LiquidDota to reintegrate into TL.net
League of Legends
Heroes of the Storm
Simple Questions, Simple Answers Heroes of the Storm 2.0
Hearthstone
Heroes of StarCraft mini-set
TL Mafia
TL Mafia Community Thread
Community
General
US Politics Mega-thread Things Aren’t Peaceful in Palestine Canadian Politics Mega-thread Russo-Ukrainian War Thread The Big Programming Thread
Fan Clubs
The Happy Fan Club!
Media & Entertainment
Movie Discussion! [Manga] One Piece Anime Discussion Thread
Sports
2024 - 2026 Football Thread Formula 1 Discussion MLB/Baseball 2023
World Cup 2022
Tech Support
Linksys AE2500 USB WIFI keeps disconnecting Computer Build, Upgrade & Buying Resource Thread High temperatures on bridge(s)
TL Community
BarCraft in Tokyo Japan for ASL Season5 Final The Automated Ban List
Blogs
The Personality of a Spender…
TrAiDoS
A very expensive lesson on ma…
Garnet
hello world
radishsoup
Lemme tell you a thing o…
JoinTheRain
RTS Design in Hypercoven
a11
Evil Gacha Games and the…
ffswowsucks
Customize Sidebar...

Website Feedback

Closed Threads



Active: 1902 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
Next event in 3h 3m
[ Submit Event ]
Live Streams
Refresh
StarCraft 2
ProTech69
OGKoka 10
StarCraft: Brood War
firebathero 1290
actioN 917
PianO 185
Leta 163
Dewaltoss 67
soO 66
Sharp 49
Noble 42
Sacsri 16
NaDa 13
Dota 2
NeuroSwarm138
XcaliburYe85
Counter-Strike
Stewie2K717
allub175
olofmeister1
Super Smash Bros
Mew2King34
Other Games
ceh9396
C9.Mang0331
XaKoH 181
SortOf116
Pyrionflax86
Trikslyr23
Organizations
Other Games
gamesdonequick440
StarCraft 2
Blizzard YouTube
StarCraft: Brood War
BSLTrovo
sctven
[ Show 16 non-featured ]
StarCraft 2
• OhrlRock 33
• LUISG 18
• AfreecaTV YouTube
• intothetv
• Kozan
• IndyKCrew
• LaughNgamezSOOP
• Migwel
• sooper7s
StarCraft: Brood War
• iopq 1
• BSLYoutube
• STPLYoutube
• ZZZeroYoutube
League of Legends
• Lourlo1184
• Stunt485
• HappyZerGling132
Upcoming Events
LiuLi Cup
3h 3m
OSC
11h 3m
RSL Revival
1d 2h
Maru vs Reynor
Cure vs TriGGeR
The PondCast
1d 5h
RSL Revival
2 days
Zoun vs Classic
Korean StarCraft League
2 days
BSL Open LAN 2025 - War…
3 days
RSL Revival
3 days
BSL Open LAN 2025 - War…
4 days
RSL Revival
4 days
[ Show More ]
Online Event
4 days
Wardi Open
5 days
Monday Night Weeklies
5 days
Sparkling Tuna Cup
6 days
Liquipedia Results

Completed

Proleague 2025-09-10
Chzzk MurlocKing SC1 vs SC2 Cup #2
HCC Europe

Ongoing

BSL 20 Team Wars
KCM Race Survival 2025 Season 3
BSL 21 Points
ASL Season 20
CSL 2025 AUTUMN (S18)
LASL Season 20
RSL Revival: Season 2
Maestros of the Game
FISSURE Playground #2
BLAST Open Fall 2025
BLAST Open Fall Qual
Esports World Cup 2025
BLAST Bounty Fall 2025
BLAST Bounty Fall Qual
IEM Cologne 2025
FISSURE Playground #1

Upcoming

2025 Chongqing Offline CUP
BSL World Championship of Poland 2025
IPSL Winter 2025-26
BSL Season 21
SC4ALL: Brood War
BSL 21 Team A
Stellar Fest
SC4ALL: StarCraft II
EC S1
ESL Impact League Season 8
SL Budapest Major 2025
BLAST Rivals Fall 2025
IEM Chengdu 2025
PGL Masters Bucharest 2025
MESA Nomadic Masters Fall
Thunderpick World Champ.
CS Asia Championships 2025
ESL Pro League S22
StarSeries 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.