• Log InLog In
  • Register
Liquid`
Team Liquid Liquipedia
EDT 12:19
CEST 18:19
KST 01:19
  • 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
Code S Season 1 - RO12 Group A: Rogue, Percival, Solar, Zoun11[ASL21] Ro8 Preview Pt1: Inheritors16[ASL21] Ro16 Preview Pt2: All Star10Team Liquid Map Contest #22 - The Finalists22[ASL21] Ro16 Preview Pt1: Fresh Flow9
Community News
2026 GSL Season 1 Qualifiers25Maestros of the Game 2 announced92026 GSL Tour plans announced15Weekly Cups (April 6-12): herO doubles, "Villains" prevail1MaNa leaves Team Liquid25
StarCraft 2
General
Team Liquid Map Contest #22 - The Finalists Code S Season 1 - RO12 Group A: Rogue, Percival, Solar, Zoun Blizzard Classic Cup @ BlizzCon 2026 - $100k prize pool MaNa leaves Team Liquid Maestros of the Game 2 announced
Tourneys
GSL Code S Season 1 (2026) SC2 INu's Battles#15 <BO.9 2Matches> WardiTV Spring Cup RSL Revival: Season 5 - Qualifiers and Main Event SEL Masters #6 - Solar vs Classic (SC: Evo)
Strategy
Custom Maps
[D]RTS in all its shapes and glory <3 [A] Nemrods 1/4 players [M] (2) Frigid Storage
External Content
The PondCast: SC2 News & Results Mutation # 523 Firewall Mutation # 522 Flip My Base Mutation # 521 Memorable Boss
Brood War
General
Data needed Pros React To: Leta vs Tulbo (ASL S21, Ro.8) ASL21 General Discussion [TOOL] Starcraft Chat Translator JaeDong's ASL S21 Ro16 Post-Review
Tourneys
Escore Tournament StarCraft Season 2 [Megathread] Daily Proleagues [ASL21] Ro8 Day 2 [ASL21] Ro8 Day 1
Strategy
Fighting Spirit mining rates Simple Questions, Simple Answers What's the deal with APM & what's its true value Any training maps people recommend?
Other Games
General Games
Daigo vs Menard Best of 10 Stormgate/Frost Giant Megathread Nintendo Switch Thread Dawn of War IV Diablo IV
Dota 2
The Story of Wings Gaming
League of Legends
G2 just beat GenG in First stand
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
Vanilla Mini Mafia Mafia Game Mode Feedback/Ideas TL Mafia Community Thread Five o'clock TL Mafia
Community
General
US Politics Mega-thread European Politico-economics QA Mega-thread Russo-Ukrainian War Thread 3D technology/software discussion Canadian Politics Mega-thread
Fan Clubs
The IdrA Fan Club
Media & Entertainment
[Manga] One Piece Anime Discussion Thread [Req][Books] Good Fantasy/SciFi books Movie Discussion!
Sports
2024 - 2026 Football Thread McBoner: A hockey love story Formula 1 Discussion
World Cup 2022
Tech Support
streaming software Strange computer issues (software) [G] How to Block Livestream Ads
TL Community
The Automated Ban List
Blogs
Sexual Health Of Gamers
TrAiDoS
lurker extra damage testi…
StaticNine
Broowar part 2
qwaykee
Funny Nicknames
LUCKY_NOOB
Iranian anarchists: organize…
XenOsky
Customize Sidebar...

Website Feedback

Closed Threads



Active: 1981 users

The Big Programming Thread - Page 768

Forum Index > General Forum
Post a Reply
Prev 1 766 767 768 769 770 1032 Next
Thread Rules
1. This is not a "do my homework for me" thread. If you have specific questions, ask, but don't post an assignment or homework problem and expect an exact solution.
2. No recruiting for your cockamamie projects (you won't replace facebook with 3 dudes you found on the internet and $20)
3. If you can't articulate why a language is bad, don't start slinging shit about it. Just remember that nothing is worse than making CSS IE6 compatible.
4. Use [code] tags to format code blocks.
Birdie
Profile Blog Joined August 2007
New Zealand4438 Posts
September 27 2016 03:00 GMT
#15341
On September 27 2016 11:26 travis wrote:
Show nested quote +
On September 27 2016 09:49 Birdie wrote:
It seems they cast their object to the type relatable, which is their interface. Their interface requires that a class have the method isLargerThan(); . But in an interface, isLargerThan(); is empty, to be defined in the implementing class, right? So wtf does casting a random object to the interface type actually do? It doesn't have the isLargerThan(); method. So.. I don't get it.

Only classes which implement a particular interface can be cast to that interface. So it isn't a "random" object, it is an object which already implements all the methods in that interface.

It says that in that document: "If you define a reference variable whose type is an interface, any object you assign to it must be an instance of a class that implements the interface."

In the example given, the Object class must be implementing the Relatable interface (and therefore has a method implementation of isLargerThan).

Think of an interface as a contract which the implementing class agrees to follow. The "implement" keyword is the class signing the contract, and the interface's methods are the terms of the contract. The class must follow the contract by creating methods which are the same as the interface's methods. Any class which signs the interface contract can then have the interface's methods called on it, because it guarantees by signing it (with the "implement" keyword) that it will have a version of all the methods in an interface.


So then if the Object class is implementing the Relatable interface and therefore has a method implementation of isLargerThan , then what is the point of casting it to that interface type? It already has the method, so what new thing does casting it to the interface type do?

What meatpudding said, but to re-state: you can have a whole bunch of classes which all implement Relatable, and then can cast any of those class objects to Relatable and be 100% confident that you can then call isLargerThan on that casted object.

You can have a whole bunch of game characters, for example, and they all implement the Movable interface, so you can loop through all game characters, cast to Movable, and call .move(x, y); on each of them in a single loop.
Red classic | A butterfly dreamed he was Zhuangzi | 4.5k, heading to 5k as support!
Manit0u
Profile Blog Joined August 2004
Poland17740 Posts
September 27 2016 10:40 GMT
#15342
http://symfony.com/doc/current/components/process.html

Mind is blown.
Time is precious. Waste it wisely.
Acrofales
Profile Joined August 2010
Spain18283 Posts
September 27 2016 11:16 GMT
#15343
On September 27 2016 19:40 Manit0u wrote:
http://symfony.com/doc/current/components/process.html

Mind is blown.

I don't get it? I'm not up-to-date on developments in PHP, but the presence of a command to execute an external program in a seperate process seems like a pretty basic capability.
Deleted User 101379
Profile Blog Joined August 2010
4849 Posts
September 27 2016 12:55 GMT
#15344
On September 27 2016 20:16 Acrofales wrote:
Show nested quote +
On September 27 2016 19:40 Manit0u wrote:
http://symfony.com/doc/current/components/process.html

Mind is blown.

I don't get it? I'm not up-to-date on developments in PHP, but the presence of a command to execute an external program in a seperate process seems like a pretty basic capability.


While I created something similar not too long ago myself, PHP's complete lack of multithreading beyond fork() doesn't make it easy to write code that asynchronously calls external programs and still retains access to the program's output. It's basic in pretty much every language that isn't PHP, but PHP, as it tends to do, makes the easy things difficult.
RoomOfMush
Profile Joined March 2015
1296 Posts
September 27 2016 13:44 GMT
#15345
On September 27 2016 09:49 Birdie wrote:
Show nested quote +
It seems they cast their object to the type relatable, which is their interface. Their interface requires that a class have the method isLargerThan(); . But in an interface, isLargerThan(); is empty, to be defined in the implementing class, right? So wtf does casting a random object to the interface type actually do? It doesn't have the isLargerThan(); method. So.. I don't get it.

Only classes which implement a particular interface can be cast to that interface. So it isn't a "random" object, it is an object which already implements all the methods in that interface.

Thats not exactly true though. This is perfectly valid java code:
		Object a = new String("A");
Double b = (Double) a;

It compiles without error. Of course, if you actually run this code you will get a ClassCastException.

@travis:
An Interface (in java) is a type just like a class or an abstract class. You can not instantiate an interface, but you can use it as the type for a variable.
Take the List interface as an example. You could have a method like this:
	public static void printList(List aList) {
for (Object element : aList) {
System.out.println(element);
}
}

This code will print out the contents of any arbitrary list. But you never get an instance of type List because these dont exist. Instead you may get a LinkedList or perhaps an ArrayList (or some other obscure implementations). Using an interface as a type is something completely normal and extremely useful. It takes away the burden of choosing an implementation.
Well written code will never actually need to know what List implementation is used. It will always work no matter what kind of List is instantiated. This allows you to swap out the implementation for a better one at any point in time and your code will simply work.



Casting is usually something you should never need to do (except for a few fringe cases). People still do it plenty of times, usually out of laziness. Writing code without casting often requires much more work when you can do a simple solution by typecasting. Even the java code examples and tutorials from the oracle website use typecasting an egregious number of times. Many of these typecasts could have easily been avoided with better written code.
maybenexttime
Profile Blog Joined November 2006
Poland5807 Posts
Last Edited: 2016-09-27 16:05:16
September 27 2016 13:57 GMT
#15346
Let me first introduce myself briefly. I am a mechanical engineering graduate who'd like to learn some Python and MATLAB programming, because I consider programming an important skill in every engineer's repertoire. For the past two months I have studied both languages and worked on some private projects of mine since that seems to be the best way to learn useful things.

I'm currently working on a MATLAB program that is supposed to quantify the degree of banding in the microstructures I simulated with an earlier script (the output is basically a set of (x,y) coordinates), and I've encountered some performance issues when compared with the program whose analysis I am trying to recreate (PASSaGE, link below).

My understanding of how the analysis is supposed to work is based on the paper written by the software's author (link) and the program's manual, but some sections were quite misleading in my opinion, so I had to do several modifications based on some experiments with analysis.

http://www.passagesoftware.net/

http://rosenberglab.net/Pubs/JVegSci2004v15p277.pdf

Initially, my MATLAB script took roughly 15 minutes (more specifically, the function that runs the analysis, not the overarching script) to analyze a set of 1000 points, whereas PASSaGE takes around 1-2 minutes. It relied mostly on loops. After a few days of optimizing, I managed to decrease the calculation time to 160-170 seconds, but I seem to have hit a dead end.

Here's the overarching script (just in case):

+ Show Spoiler +
clc
clear

%%% PARAMETERS %%%

answer = input('Should the default value be used for the edge region parameter? Y/N [Y]: ','s');
if isempty(answer) % If the user simply presses 'Enter', the answer is assumed to be 'Y'.
answer = 'Y';
end

disp(' ') % Empty line for clarity.

if strcmp(answer, 'Y') == 1
edge_parameter = 0.25;
disp('The default value of the edge region parameter (0.25) will be used.')
else
edge_parameter = input('Please, specify the value of the edge region parameter: ');
end

%%% IMPORT OF DATA %%%

filename = 'banding_coordinates.txt';
delimiterIn = ' ';
headerlinesIn = 1;
D = importdata(filename,delimiterIn,headerlinesIn);

%%% EXTRACTION OF COORDINATES %%%

x_extracted = D.data(:,1);
y_extracted = D.data(:,2);

% Transposition into vectors:

x = transpose(x_extracted);
y = transpose(y_extracted);

% Verification of vector lengths:

if length(x) ~= length(y)
disp('The imported data is corrupted: the numbers of the x and y coordinates do not match')
end

%%% BOUNDARY CONDITIONS %%%

% Limits of the analyzed space:

x_min = min(x);
x_max = max(x);
y_min = min(y);
y_max = max(y);

% Edge region parameters for each coordinate:

edge_parameter_x = edge_parameter*(x_max - x_min);
edge_parameter_y = edge_parameter*(y_max - y_min);

% Limits of the focal point space:

x_foc_min = x_min + edge_parameter_x;
x_foc_max = x_max - edge_parameter_x;
y_foc_min = y_min + edge_parameter_y;
y_foc_max = y_max - edge_parameter_y;

%%% EXTRACTION OF FOCAL POINTS %%%

% Generation of focal points:

[in,on] = inpolygon(x, y, [x_foc_min, x_foc_max], [y_foc_min, y_foc_max]);

x_foc = x(in);
y_foc = y(in);

% Focal points plot:

answer = input('Would you like to display the generated focal points? Y/N [N]: ','s');
if isempty(answer) % If the user simply presses 'Enter', the answer is assumed to be 'Y'.
answer = 'N';
end

disp(' ') % Empty line for clarity.

if strcmp(answer, 'Y') == 1
figure

scatter(x,y,'.') % all points
axis equal

hold on
scatter(x_foc,y_foc,'r+') % focal points
hold off
end

%%% ANALYSIS %%%
tic
overall_variance = calculate_variance_test6_3(x, y, x_foc, y_foc, ...
x_min, x_max, y_min, y_max);
toc
%%% OUTPUT %%%

% Original variance peak:

disp('Original variance peak:')
max(overall_variance)

% Plot:

figure

plot(1:1:180, overall_variance)


And here's the function that does the actual analysis (in its most optimal form):

+ Show Spoiler +
function variance_avg_foc = calculate_variance_test6_3(x, y, x_foc, y_foc, ...
x_min, x_max, y_min, y_max)

% Calculation of the number of focal points:

m = length(x_foc); % Number of focal points = number of iterations in the first loop.

% Calculation of the number of scales:

scale = 1:1:45; % All scales.
l = length(scale);

% Preallocation of memory for all focal points:

variance_avg_scale = zeros(m, 180);

% Calculation of the number of analyzed points:

n = length(x) - 1;

% Calculation of transect properties:

p = 1:1:180;
transect_angles = p - 1;
slopes = tand((transect_angles));

% Selection of the focal point

for j = 1:m

% Removal of the focal point from the pool of analyzed points:

index = find( (x(1,:) == x_foc(j)) & (y(1,:) == y_foc(j)) );

if length(index) ~= 1 % If there is more than once point with the x_foc(j), y_foc(j)
index = index(1); % coordinates, only one point will be subtracted from the future
end % local coordinates pool.

x_without_foc = x(1:end ~= index);
y_without_foc = y(1:end ~= index);

% Creation of local coordinates /vectorized:

i = 1:1:n; % One iteration for each (x, y) point.
x_local = x_without_foc(i) - x_foc(j);
y_local = y_without_foc(i) - y_foc(j);

% Limits of the studied area in local coordinates:

x_min_local = x_min - x_foc(j);
x_max_local = x_max - x_foc(j);
y_min_local = y_min - y_foc(j);
y_max_local = y_max - y_foc(j);

% Translation to local polar coordinates:

[angle,radius] = cart_to_pol(x_local,y_local);
angle = rad2deg(angle);

% Consolidation of coordinates data:

data = [angle; radius];

% Preallocation of memory for all scales:

transect_area = zeros(1, 180);

% Calculation of the transect area:

for p = 1:180

transect_angle = transect_angles(p);
slope = slopes(p);

transect_area(p) = calculate_transect_area(x_min_local, ...
x_max_local, y_min_local, y_max_local, transect_angle, slope);
end

% Preallocation of memory for all scales:

variance_scale = zeros(l, 180);

% Selection of scale:

for k = 1:l

% Transect width:

transect_width = scale;

% Preallocation of memory for all positions:

wavelet_transform = zeros(1, 180);
variance_normalized = zeros(1, 180);

% Selection of angular position:

for p = 1:180

% Finding the points inside the specified transect:

points_observed = observe_points(data, p, transect_width(k));

% Extraction of the polar coordinates of the observed points:

angle_observed = points_observed(1,:);
radius_observed = points_observed(2,:);

% Calculation of the number of observed points:

observations = length(angle_observed);

% Calculation of scaled wavelets for each point within the transect /vectorized:

if observations == 0
scaled_wavelet = 0;
else
o = 1:1:observations;
t = (angle_observed(o) - transect_angles(p))./scale(k);
scaled_wavelet = radius_observed.*wavelet_function(t);
end

wavelet_transform(p) = sum(scaled_wavelet)/scale(k);
variance_normalized(p) = wavelet_transform(p)^2/transect_area(p);
end
variance_scale(k,:) = variance_normalized;
end
variance_avg_scale(j,:) = sum(variance_scale)./l; % All specified scales.
end
variance_avg_foc = sum(variance_avg_scale)./m;
end

%--------------------------------------------------------------------------
function [angle, radius] = cart_to_pol(x, y)

angle = arctan(y, x);
radius = hypot(x,y);

end

%--------------------------------------------------------------------------
function angle = arctan(y, x)

n = length(x);
angle = zeros(1, n);
parfor i = 1:n % /vectorized
angle(i) = atan2(y(i), x(i));
if angle(i) < 0
angle(i) = angle(i) + pi;
end
end

end

%--------------------------------------------------------------------------
function points_observed = observe_points(data, p, transect_width)

% Finding the points inside the specified transect:

step_size = 1;

log_ind = ( (p - 1 - (transect_width/2))*step_size <= data(1,:) & ...
data(1,:) <= (p - 1 + (transect_width/2))*step_size ) | ...
( ((p - 1 - (transect_width/2))*step_size + 180) <= data(1,:) & ...
data(1,:) <= ((p - 1 + (transect_width/2))*step_size + 180) );
points_observed = data(:,log_ind);

end

%--------------------------------------------------------------------------
function transect_area = calculate_transect_area(x_min_local, ...
x_max_local, y_min_local, y_max_local, transect_angle, slope)

% Slope variants:

if (transect_angle == 0) || (transect_angle == 180) % Horizontal line.
x_lim_1 = x_min_local;
x_lim_2 = x_max_local;
y_lim_1 = 0;
y_lim_2 = 0;
elseif transect_angle == 90 % Vertical line.
x_lim_1 = 0;
x_lim_2 = 0;
y_lim_1 = y_min_local;
y_lim_2 = y_max_local;
elseif transect_angle > 0 && transect_angle < 90

% Boundary coordinates:

if y_min_local/slope >= x_min_local
x_lim_1 = y_min_local/slope;
y_lim_1 = y_min_local;
else
x_lim_1 = x_min_local;
y_lim_1 = slope*x_min_local;
end

if y_max_local/slope <= x_max_local
x_lim_2 = y_max_local/slope;
y_lim_2 = y_max_local;
else
x_lim_2 = x_max_local;
y_lim_2 = slope*x_max_local;
end
else

% Boundary coordinates:

if y_min_local/slope >= x_max_local
x_lim_1 = x_max_local;
y_lim_1 = slope*x_max_local;
else
x_lim_1 = y_min_local/slope;
y_lim_1 = y_min_local;
end

if y_max_local/slope <= x_min_local
x_lim_2 = x_min_local;
y_lim_2 = slope*x_min_local;
else
x_lim_2 = y_max_local/slope;
y_lim_2 = y_max_local;
end
end

transect_area = (x_lim_1)^2 + (y_lim_1)^2 + (x_lim_2)^2 + (y_lim_2)^2; % This is not real area of the transect.
% It's area divided by omega/2, but that holds
% for all transects and omega is constant.
end

%--------------------------------------------------------------------------
function mexican_hat = wavelet_function(t) % /vectorized

mexican_hat = 2./(sqrt(3)).*pi.^(-1/4).*(1 - 4.*t.^2).*exp(-2.*t.^2); % Wavelet function normalized to have unit energy.
% Vectorizing with "vectorize()" does not work.
% It cannot tell if I want to use ^ for matrices (mpower)
% or scalars (power).

end


According to the MATLAB profiler, there are two lines/functions whose vectorization could yield significant improvements in calculation time, but I failed at vectorizing them properly... Any help would be appreciated.

This line currently requires roughly 95 seconds (it's being called 4 million times):

+ Show Spoiler +
points_observed = observe_points(data, p, transect_width(k));


And these two lines require roughly 30 seconds (they're being called 700 thousand times):

+ Show Spoiler +
t = (angle_observed(o) - transect_angles(p))./scale(k);
scaled_wavelet = radius_observed.*wavelet_function(t);


Additionally, I am looking for a way to make scaled_wavelet call wavelet_function only for non-zero values of radius_observed. I tried the solution below, but it only made the calculations longer:

+ Show Spoiler +
% Calculation of scaled wavelets for each point within the transect /vectorized:

if observations == 0
scaled_wavelet = 0;
else
log_ind = (radius_observed ~= 0);
%o = 1:1:observations;
scaled_wavelet = radius_observed(log_ind).*wavelet_function((angle_observed(log_ind) - transect_angles(p))./scale(k));
end


I also found that spfun() does something similar, but I don't know how to make it work with a custom function like wavelet_function().
Acrofales
Profile Joined August 2010
Spain18283 Posts
September 27 2016 14:10 GMT
#15347
On September 27 2016 22:44 RoomOfMush wrote:
Show nested quote +
On September 27 2016 09:49 Birdie wrote:
It seems they cast their object to the type relatable, which is their interface. Their interface requires that a class have the method isLargerThan(); . But in an interface, isLargerThan(); is empty, to be defined in the implementing class, right? So wtf does casting a random object to the interface type actually do? It doesn't have the isLargerThan(); method. So.. I don't get it.

Only classes which implement a particular interface can be cast to that interface. So it isn't a "random" object, it is an object which already implements all the methods in that interface.

Thats not exactly true though. This is perfectly valid java code:
		Object a = new String("A");
Double b = (Double) a;

It compiles without error. Of course, if you actually run this code you will get a ClassCastException.

@travis:
An Interface (in java) is a type just like a class or an abstract class. You can not instantiate an interface, but you can use it as the type for a variable.
Take the List interface as an example. You could have a method like this:
	public static void printList(List aList) {
for (Object element : aList) {
System.out.println(element);
}
}

This code will print out the contents of any arbitrary list. But you never get an instance of type List because these dont exist. Instead you may get a LinkedList or perhaps an ArrayList (or some other obscure implementations). Using an interface as a type is something completely normal and extremely useful. It takes away the burden of choosing an implementation.
Well written code will never actually need to know what List implementation is used. It will always work no matter what kind of List is instantiated. This allows you to swap out the implementation for a better one at any point in time and your code will simply work.



Casting is usually something you should never need to do (except for a few fringe cases). People still do it plenty of times, usually out of laziness. Writing code without casting often requires much more work when you can do a simple solution by typecasting. Even the java code examples and tutorials from the oracle website use typecasting an egregious number of times. Many of these typecasts could have easily been avoided with better written code.


As you said yourself, that throws a ClassCastException at runtime. If (warning: completely nonsense example) your String class implemented the Double interface, then that would NOT throw a classcastexception, and all the operations defined in your interface would be guaranteed to be implemented. THAT is the point of an interface. Also, this:

List<Double> foo = new ArrayList<Double>();
foo.add(new String("a");

does not compile (and while you can cast that to double and have it only bomb out at runtime, that is some willfully stupid programming; it's of the quality "it compiles, therefore it works flawlessly".

So, in effect, the statement "only classes that implement that interface can be cast to it" is correct, and the fact that it is a runtime error, rather than a compiler error is not a counterargument. As you said yourself, using casts (for this type of thing, as I disagree that casts are categorically bad) is not a good style.

It used to be the case in Java (pre-1.5, I think, but it might have been 6) that Java didn't have generics, and collections contained, by default, objects of class Object. That was a pain in the ass. Java generics have their foibles, but they are a vast improvement over their absense. However, your example is a bad one, because it doesn't depend on interfaces at all. It is predicated upon the fact that the Object class in Java is, by definition, a superclass of every single other class. Object isn't an interface, it is a class. A better example would be the very common interfaces like Serializable, Runnable, or EventListener.

Not only that, but the absense of generics means your code will give a nasty warning (due to the absense of said generics).

PS. Nasty warnings are technically warnings just like all other warnings, but ones that indicate something seriously wrong with your code if you ignore them: you are either an experienced programmer who is willfully ignoring said warning, or you had better fix that shit. While an argument can be made that ALL warnings are of that type, my opinion is that the Java compiler goes a bit overboard on the warnings.
RoomOfMush
Profile Joined March 2015
1296 Posts
Last Edited: 2016-09-27 15:13:28
September 27 2016 15:11 GMT
#15348
On September 27 2016 23:10 Acrofales wrote:
So, in effect, the statement "only classes that implement that interface can be cast to it" is correct, and the fact that it is a runtime error, rather than a compiler error is not a counterargument. As you said yourself, using casts (for this type of thing, as I disagree that casts are categorically bad) is not a good style.

No, its not correct. You are confusing classes with objects. You never cast classes, you may cast references of certain types though. You can cast a reference of any type to any other type. What you can not do is cast an object to a wrong type. The difference between classes, references and objects is important.
Maybe you think I am being pedantic, but I am actually teaching this stuff to first semester CS students and so I am always very careful of using the wrong terms.

On September 27 2016 23:10 Acrofales wrote:
It used to be the case in Java (pre-1.5, I think, but it might have been 6) that Java didn't have generics, and collections contained, by default, objects of class Object. That was a pain in the ass.

This is also wrong. Collections did always contain objects of any arbitrary type. The difference is that the collections of old only ever returned references of type Object, but the actual objects that were returned could have been anything.
The fun fact is: The generic collections of today STILL only hold references to the Object class. They simply do a class cast internally when you call a get method. Generics in java are only a simple form of static code analysis. At runtime they are completely gone and replaced by Object references. This is why you can not declare arrays of generic type parameters in java.

On September 27 2016 23:10 Acrofales wrote:
Java generics have their foibles, but they are a vast improvement over their absense. However, your example is a bad one, because it doesn't depend on interfaces at all. It is predicated upon the fact that the Object class in Java is, by definition, a superclass of every single other class. Object isn't an interface, it is a class. A better example would be the very common interfaces like Serializable, Runnable, or EventListener.

Not only that, but the absense of generics means your code will give a nasty warning (due to the absense of said generics).

PS. Nasty warnings are technically warnings just like all other warnings, but ones that indicate something seriously wrong with your code if you ignore them: you are either an experienced programmer who is willfully ignoring said warning, or you had better fix that shit. While an argument can be made that ALL warnings are of that type, my opinion is that the Java compiler goes a bit overboard on the warnings.

I left out generics in my example because I dont know how much travis knows. Perhaps he didnt learn about generics yet in which case they would only confuse him instead of helping him understand the issue.
Silvanel
Profile Blog Joined March 2003
Poland4751 Posts
Last Edited: 2016-09-27 15:47:48
September 27 2016 15:47 GMT
#15349
On September 27 2016 22:57 maybenexttime wrote:
Let me first introduce myself briefly. I am a mechanical engineering graduate who'd like to learn some Python and MATLAB programming, because I consider programming an important skill in every engineer's repertoire. For the past two months I have studied both languages and worked on some private projects of mine since that seems to be the best way to learn useful things.

.


Heh. Strange coincidence i am also mechanical engineer (material engineering to be precise) and Python also is my langauge of choice (and also Polish ). I cant help You with MATHLAB though, never had time to learn it properly. Will try to help with Python if i can though.

BTW Which Uni did You gradute from?
Pathetic Greta hater.
maybenexttime
Profile Blog Joined November 2006
Poland5807 Posts
September 27 2016 16:08 GMT
#15350
On September 28 2016 00:47 Silvanel wrote:
Show nested quote +
On September 27 2016 22:57 maybenexttime wrote:
Let me first introduce myself briefly. I am a mechanical engineering graduate who'd like to learn some Python and MATLAB programming, because I consider programming an important skill in every engineer's repertoire. For the past two months I have studied both languages and worked on some private projects of mine since that seems to be the best way to learn useful things.

.


Heh. Strange coincidence i am also mechanical engineer (material engineering to be precise) and Python also is my langauge of choice (and also Polish ). I cant help You with MATHLAB though, never had time to learn it properly. Will try to help with Python if i can though.

BTW Which Uni did You gradute from?


Strangely enough, I specialized in structural materials engineering.

I graduated from Wrocław University of Technology/Politechnika Wrocławska. And you? :-)
Deleted User 3420
Profile Blog Joined May 2003
24492 Posts
September 27 2016 16:50 GMT
#15351
Am I correct in thinking that an abstract class is basically an "inbetween" of an interface and a superclass ?

also thank you for everyone who takes time to reply to me. I may not reply to them or just haven't read them yet but I do go over them.
Blitzkrieg0
Profile Blog Joined August 2010
United States13132 Posts
Last Edited: 2016-09-27 17:01:48
September 27 2016 17:01 GMT
#15352
On September 28 2016 01:50 travis wrote:
Am I correct in thinking that an abstract class is basically an "inbetween" of an interface and a superclass ?


An abstract class can contain implemented methods and other data unlike the interface which only has method stubs. The difference between these two things is an actual thing in java as they have different name and are used by the subclass through different keywords. If you look at another language like php you will see that the line is more blurred and there is almost no difference between the two terms.

You are correct in thinking they are similar.
I'll always be your shadow and veil your eyes from states of ain soph aur.
Acrofales
Profile Joined August 2010
Spain18283 Posts
Last Edited: 2016-09-27 17:11:12
September 27 2016 17:03 GMT
#15353
On September 27 2016 11:26 travis wrote:
Show nested quote +
On September 27 2016 09:49 Birdie wrote:
It seems they cast their object to the type relatable, which is their interface. Their interface requires that a class have the method isLargerThan(); . But in an interface, isLargerThan(); is empty, to be defined in the implementing class, right? So wtf does casting a random object to the interface type actually do? It doesn't have the isLargerThan(); method. So.. I don't get it.

Only classes which implement a particular interface can be cast to that interface. So it isn't a "random" object, it is an object which already implements all the methods in that interface.

It says that in that document: "If you define a reference variable whose type is an interface, any object you assign to it must be an instance of a class that implements the interface."

In the example given, the Object class must be implementing the Relatable interface (and therefore has a method implementation of isLargerThan).

Think of an interface as a contract which the implementing class agrees to follow. The "implement" keyword is the class signing the contract, and the interface's methods are the terms of the contract. The class must follow the contract by creating methods which are the same as the interface's methods. Any class which signs the interface contract can then have the interface's methods called on it, because it guarantees by signing it (with the "implement" keyword) that it will have a version of all the methods in an interface.


So then if the Object class is implementing the Relatable interface and therefore has a method implementation of isLargerThan , then what is the point of casting it to that interface type? It already has the method, so what new thing does casting it to the interface type do?


If you already know that, nothing. E.g. if you know you have a Double, you don't have to cast it to a Relatable to call isLargerThan. But look at the implementation of isLargerThan. It requires a parameter of type Relatable. It doesn't care whether that is a double, a different type of number, or a string.

As another example, consider Serializable. For this, you technically don't even have to implement anything, you just declare that the class implements Serializable. Because of inheritance, you can call serialize() on your objects of this type (the Object class gives a default implementation of the serialize method). However everything that does things for which objects need to be serialized (like read/writing them to file, or sending it over the network), wanted an object of type Serializable, and not of type Object, because one can assume that if you implement the interface, you have thought a bit about how your class should be serialized, and if the default implementation doesn't suffice, have provided your own implementation. It makes debugging easier too, because if you try to do something with an object that is not Serializable, you get a decent error, instead of it writing nonsense to a file, and then breaking at some random point in the future to a null pointer, after deserializing the object from file.
RoomOfMush
Profile Joined March 2015
1296 Posts
September 27 2016 17:06 GMT
#15354
On September 28 2016 01:50 travis wrote:
Am I correct in thinking that an abstract class is basically an "inbetween" of an interface and a superclass ?

also thank you for everyone who takes time to reply to me. I may not reply to them or just haven't read them yet but I do go over them.

Since java 8 the only difference between an abstract class and an interface is the fact that the abstract class can have fields and it can override methods from the Object class. Interfaces can not override methods from Object, like toString, equals and hashCode which makes sense from a technical point of view but is quite a bummer from a practical point of view.
Another difference is that abstract classes can not be used for lambdas but interfaces can. I dont know if you have learned about lambdas yet but they have become a big deal in java 8.
Acrofales
Profile Joined August 2010
Spain18283 Posts
September 27 2016 17:08 GMT
#15355
On September 28 2016 02:01 Blitzkrieg0 wrote:
Show nested quote +
On September 28 2016 01:50 travis wrote:
Am I correct in thinking that an abstract class is basically an "inbetween" of an interface and a superclass ?


An abstract class can contain implemented methods and other data unlike the interface which only has method stubs. The difference between these two things is an actual thing in java as they have different name and are used by the subclass through different keywords. If you look at another language like php you will see that the line is more blurred and there is almost no difference between the two terms.

You are correct in thinking they are similar.

And if you look at polymorphism in Android, the line is equally blurred, because often the documentation specifies that if you implement a specific interface, you need to implement a specific field, as well as the methods, but interfaces don't allow one to specify that. In other words, interfaces are being used for multiple inheritance in Android in a very similar way that abstract classes are in other languages.
RoomOfMush
Profile Joined March 2015
1296 Posts
September 27 2016 17:10 GMT
#15356
On September 28 2016 02:03 Acrofales wrote:
Show nested quote +
On September 27 2016 11:26 travis wrote:
On September 27 2016 09:49 Birdie wrote:
It seems they cast their object to the type relatable, which is their interface. Their interface requires that a class have the method isLargerThan(); . But in an interface, isLargerThan(); is empty, to be defined in the implementing class, right? So wtf does casting a random object to the interface type actually do? It doesn't have the isLargerThan(); method. So.. I don't get it.

Only classes which implement a particular interface can be cast to that interface. So it isn't a "random" object, it is an object which already implements all the methods in that interface.

It says that in that document: "If you define a reference variable whose type is an interface, any object you assign to it must be an instance of a class that implements the interface."

In the example given, the Object class must be implementing the Relatable interface (and therefore has a method implementation of isLargerThan).

Think of an interface as a contract which the implementing class agrees to follow. The "implement" keyword is the class signing the contract, and the interface's methods are the terms of the contract. The class must follow the contract by creating methods which are the same as the interface's methods. Any class which signs the interface contract can then have the interface's methods called on it, because it guarantees by signing it (with the "implement" keyword) that it will have a version of all the methods in an interface.


So then if the Object class is implementing the Relatable interface and therefore has a method implementation of isLargerThan , then what is the point of casting it to that interface type? It already has the method, so what new thing does casting it to the interface type do?


If you already know that, nothing. E.g. if you know you have a Double, you don't have to cast it to a Relatable to call isLargerThan. But look at the implementation of isLargerThan. It requires a parameter of type Relatable. It doesn't care whether that is a double, a different type of number, or a string.

As another example, consider Serializable. For this, you technically don't even have to implement anything, you just declare that the class implements Serializable. Because of inheritance, you can call serialize() on your objects of this type (the Object class gives a default implementation of the serialize method). However everything that does things for which objects need to be serialized (like read/writing them to file, or sending it over the network), wanted an object of type Serializable, and not of type Object, because one can assume that if you implement the interface, you have thought a bit about how your class should be serialized, and if the default implementation doesn't suffice, have provided your own implementation. It makes debugging easier too, because if you try to do something with an object that is not Serializable, you get a decent error, instead of it writing nonsense to a file, and then breaking at some random point in the future to a null pointer, after federalizing the object from file.

Its a good thing that you are trying to help people but please fact check what you say before hand. There is no "serialize()" method and Object certainly doesnt define it. And methods that are used in object serialization do not take Serializable as arguments (even though they should). They actually just take Object and instead state in their JavaDoc that a runtime exception is thrown in case the object is not serializable.
Serializable is a really bad example for the use of interfaces because it actually uses Reflection instead.
Deleted User 3420
Profile Blog Joined May 2003
24492 Posts
Last Edited: 2016-09-27 17:19:25
September 27 2016 17:18 GMT
#15357
So from what I have read, if I have a method in a superclass and it has a return type
I can change the return type of the overrided method to a different return type, as long as it's a subclass of the original return type?

like, I can't go from int to string but I can go from Integer to int ?

and I am assuming this means I couldn't go from a return type to void in my overriden method?



And then my next question is about overloading. Overloaded methods must all have the same access level and return type? Only the parameters change? Will I get errors if I try to make methods with the same name but different return types or access levels?


edit: all questions are specific to java since that is what my exam will be on
RoomOfMush
Profile Joined March 2015
1296 Posts
Last Edited: 2016-09-27 18:55:31
September 27 2016 17:35 GMT
#15358
On September 28 2016 02:18 travis wrote:
So from what I have read, if I have a method in a superclass and it has a return type
I can change the return type of the overrided method to a different return type, as long as it's a subclass of the original return type?

like, I can't go from int to string but I can go from Integer to int ?

and I am assuming this means I couldn't go from a return type to void in my overriden method?

What you say is correct but your example is wrong. int is not a subclass of Integer (nor is it the other way around). "int" is not a class at all, its a primitive data type which is something completely seperate from classes. You are still allowed to use "int" and "Integer" almost interchangeably but that has nothing to do with class hierarchy. This is actually compiler magic called "boxing" and "unboxing".

But if your super class has a method named test() which returns Object you can have a subclass with a method named test() which returns String because String is a subclass of Object. Think about it like this: Every String IS an Object. If your method is supposed to return an Object but it returns a String then it IS actually returning an Object like it is supposed to do.


On September 28 2016 02:18 travis wrote:
And then my next question is about overloading. Overloaded methods must all have the same access level and return type? Only the parameters change? Will I get errors if I try to make methods with the same name but different return types or access levels?

edit: all questions are specific to java since that is what my exam will be on

Overloaded methods do not need to have the same access level at all. They also dont need to have the same return type. There is no overloading in the Java specification. The compiler does not care about overloading; for the compiler two methods with the same name but different parameter lists are simply 2 different methods.
Edit: Just quoting the specification: (Source)
If two methods of a class (whether both declared in the same class, or both inherited by a class, or one declared and one inherited) have the same name but signatures that are not override-equivalent, then the method name is said to be overloaded.

This fact causes no difficulty and never of itself results in a compile-time error. There is no required relationship between the return types or between the throws clauses of two methods with the same name, unless their signatures are override-equivalent.


The nice thing about these kinds of questions is: You dont have to ask. Just try it out. Grab a text editor and a java compiler, type in an example and see whether it will compile or not.
Example:
public class OverloadingExample {

public String test() {
return "1984";
}

public Integer test(int param) {
return 42;
}

public String test(double param) {
return "4891";
}

}

(^This compiles perfectly fine for example)
Silvanel
Profile Blog Joined March 2003
Poland4751 Posts
September 27 2016 17:36 GMT
#15359
On September 28 2016 01:08 maybenexttime wrote:
Show nested quote +
On September 28 2016 00:47 Silvanel wrote:
On September 27 2016 22:57 maybenexttime wrote:
Let me first introduce myself briefly. I am a mechanical engineering graduate who'd like to learn some Python and MATLAB programming, because I consider programming an important skill in every engineer's repertoire. For the past two months I have studied both languages and worked on some private projects of mine since that seems to be the best way to learn useful things.

.


Heh. Strange coincidence i am also mechanical engineer (material engineering to be precise) and Python also is my langauge of choice (and also Polish ). I cant help You with MATHLAB though, never had time to learn it properly. Will try to help with Python if i can though.

BTW Which Uni did You gradute from?


Strangely enough, I specialized in structural materials engineering.

I graduated from Wrocław University of Technology/Politechnika Wrocławska. And you? :-)


Technical University of Łódź
Pathetic Greta hater.
Deleted User 3420
Profile Blog Joined May 2003
24492 Posts
Last Edited: 2016-09-27 22:16:48
September 27 2016 22:04 GMT
#15360
edit: nevermind I need to go over enums better they are confusing actually


aaand - I figured it out. I needed to make an array of the type of my enum and assign it to that enum.values() in order to use a for each loop to go through them
Prev 1 766 767 768 769 770 1032 Next
Please log in or register to reply.
Live Events Refresh
Big Brain Bouts
16:00
#114
TriGGeR vs Percival
RotterdaM775
Liquipedia
OSC
13:00
King of the Hill #246
WardiTV678
TKL 261
Liquipedia
[ Submit Event ]
Live Streams
Refresh
StarCraft 2
RotterdaM 775
TKL 261
MindelVK 24
JuggernautJason24
ProTech23
UpATreeSC 21
StarCraft: Brood War
Britney 35576
Calm 5046
Sea 2347
Mini 773
Shuttle 557
firebathero 441
EffOrt 363
BeSt 264
Leta 236
ggaemo 233
[ Show more ]
Hyuk 187
Hyun 108
Dewaltoss 96
Sharp 91
ToSsGirL 51
Sea.KH 45
Hm[arnc] 44
Barracks 32
Free 30
Rock 25
yabsab 22
scan(afreeca) 20
Terrorterran 16
910 16
GoRush 14
Shine 14
IntoTheRainbow 12
SilentControl 12
Sacsri 9
JulyZerg 7
Dota 2
Gorgc5540
qojqva1951
monkeys_forever326
syndereN263
Other Games
Grubby4253
singsing2184
FrodaN913
B2W.Neo857
hiko761
Hui .172
crisheroes171
ArmadaUGS88
QueenE74
ViBE71
Trikslyr44
KnowMe29
Organizations
Dota 2
PGL Dota 2 - Main Stream60
StarCraft 2
Blizzard YouTube
StarCraft: Brood War
BSLTrovo
[ Show 15 non-featured ]
StarCraft 2
• Adnapsc2 14
• AfreecaTV YouTube
• intothetv
• Kozan
• IndyKCrew
• LaughNgamezSOOP
• Migwel
• sooper7s
StarCraft: Brood War
• Michael_bg 4
• BSLYoutube
• STPLYoutube
• ZZZeroYoutube
League of Legends
• Nemesis3911
Other Games
• WagamamaTV301
• Shiphtur211
Upcoming Events
Replay Cast
7h 41m
Replay Cast
16h 41m
RSL Revival
17h 41m
Classic vs GgMaChine
Rogue vs Maru
WardiTV Invitational
18h 41m
Percival vs Shameless
ByuN vs YoungYakov
IPSL
23h 41m
Ret vs Art_Of_Turtle
Radley vs TBD
BSL
1d 2h
Replay Cast
1d 7h
RSL Revival
1d 17h
herO vs TriGGeR
NightMare vs Solar
uThermal 2v2 Circuit
1d 21h
BSL
2 days
[ Show More ]
IPSL
2 days
eOnzErG vs TBD
G5 vs Nesh
Patches Events
2 days
Replay Cast
2 days
Wardi Open
2 days
Afreeca Starleague
2 days
Jaedong vs Light
Monday Night Weeklies
2 days
Replay Cast
3 days
Sparkling Tuna Cup
3 days
Afreeca Starleague
3 days
Snow vs Flash
WardiTV Invitational
3 days
GSL
4 days
Classic vs Cure
Maru vs Rogue
GSL
5 days
SHIN vs Zoun
ByuN vs herO
Replay Cast
6 days
Escore
6 days
The PondCast
6 days
WardiTV Invitational
6 days
Liquipedia Results

Completed

Proleague 2026-04-30
WardiTV TLMC #16
Nations Cup 2026

Ongoing

BSL Season 22
ASL Season 21
CSL 2026 SPRING (S20)
IPSL Spring 2026
KCM Race Survival 2026 Season 2
Escore Tournament S2: W5
KK 2v2 League Season 1
SCTL 2026 Spring
2026 GSL S1
BLAST Rivals Spring 2026
IEM Rio 2026
PGL Bucharest 2026
Stake Ranked Episode 1
BLAST Open Spring 2026
ESL Pro League S23 Finals
ESL Pro League S23 Stage 1&2
PGL Cluj-Napoca 2026

Upcoming

Acropolis #4
BSL 22 Non-Korean Championship
CSLAN 4
Kung Fu Cup 2026 Grand Finals
HSC XXIX
uThermal 2v2 2026 Main Event
Maestros of the Game 2
2026 GSL S2
RSL Revival: Season 5
XSE Pro League 2026
IEM Cologne Major 2026
Stake Ranked Episode 2
CS Asia Championships 2026
Asian Champions League 2026
IEM Atlanta 2026
PGL Astana 2026
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 © 2026 TLnet. All Rights Reserved.