« first day (2853 days earlier)      last day (2312 days later) » 

00:00
"If you want to test your memory, try to recall what you were worrying about one year ago today." -E. Joseph Cossman (source)
I had said that you can use a protected abstract <T> T init(T t) as a way to use abstract classes to restrict the implementation.
Java allows that protected abstract methods can be used in constructors to initialize variables. This is explicitly mentioned in the Java Tutorials (Java Trail).
Suppose you want to use dependency injection explicitly, but you don't want to give direct control of initializing the fields to subclasses.
You also don't want public classes being able to do this either: you just want a specific way to implement a class, and no other way shall be allowed.
Such a method is like self-documenting inheritance, and is a very powerful and useful implementation of inheritance. Using abstract classes in this way creates a partly defined model which can be easily documented. In fact, such classes should be made in this fashion in order to be dedicated to inheritance, but not as pure skeleton classes in such case (though you can use the same model for skeletons).
It should be obvious why this is an excellent usage of abstract classes which Josh Bloch did not mention in EJ, not even 3e.
Suppose you have some API. If you used only an interface, you wouldn't have the flexibility mentioned in the above description that an interface would for dependency injection. An abstract class is a powerful tool in the right hands. It exists for a reason; nothing in this universe is without purpose. This is just one reason to use abstract classes.
One example might be a resource loader class, public abstract class ResourceLoader<T>.
You can define a field, private final Function<O, T> provider, and a public final T loadResource(O o) {...}
you can define in the class a protected abstract Function<O, T> getProvider();, and in ResourceLoader::new which is a default constructor, you have provider = getProvider();.
A better example, however, would be to make an immutable object whose type is not known to the parent class, but is known to whatever class implements it, or whatever code uses the erased type, so perhaps you might instead have a LazilyLoadedVariable<T> with single field private final Supplier<T> lazilyLoadedObject;
protected abstract Supplier<T> getLazyObjectSupplier(); and some public final T get() { return lazilyLoadedObject.get(); }, and voila!
You can now define a network-loaded object, a disk-loaded object, a calculated object, a virtual object, an existing object, etc. You get the point.
I don't know what else to say. I'll end my monologue here.
00:58
Alright, I have a proper question: it's been over a year and I can't create a new question. Why?
According to help center, I am allowed to create a new question after 6 months since my last question. My last question was August 6th, 2017
That is 8 days ago.
/shrug
¯\_(ツ)_/¯
02:01
If I do double p = 1000000000000.0000000000000 I get a double of 1.0E12. But if I do Double.parseDouble("1000000000000.0000000000000"), I get a double of 9.999999999999999E11
Why?
 
1 hour later…
03:27
@DemCodeLines floating point precision errors
@JennaSloan Anyway to work around it in StreamTokenizer?
Here is the SO question:
03:45
hello every one
 
1 hour later…
05:15
@DemCodeLines Please don't use chat as fastlane for your questions. Instead, wait a few days before asking here to give the people on the main site some time. If we want to answer questions there, we will look for them on our own.
1 message moved to friendly bin
06:01
TBH, I'd just use BigDecimal
o/
user8622974
\o
Hey Jenna :)
06:26
\uD83D\uDC4B(?:\uD83C[\uDFFB-\uDFFF])?
06:54
\o/
posted on August 15, 2018

archive - contact - sexy exciting merchandise - search - about ← previousAugust 15th, 2018nextAugust 15th, 2018: I'm travelling this week so hopefully there's no typos in this comic! If there are: I APOLOGIZE, PAST RYAN WAS A BIG IDIOT, AND WHEN HE NOTICES THIS FUTURE RYAN IS GOING TO HAVE TO COVER FOR HIM... *AGAIN*.– Ryan

07:29
Morn
user8622974
Morn
morn
Hey Gimby :)
G'day. There are no explosions so I take it things are smoother sailing for you today? ;)
I didn't have the chance to code any today so far :/
Have to write up on what I did the last two weeks for a meeting today (I am an apprentice and that is required to do in Germany -.-) and wrote on my proposal for my final project.
07:33
If you work on a project basis than that write up never really goes away, only it changes to booking hours in the most godawful user interfaces known to man...
*then, jebus. Need coffee.
@Zoe Does Alisha have a coffee command by now?
Zoe
Zoe
Nope
Zoe
Zoe
Other projects
I see :3 Even SO knows xD
07:42
I like how in the screenshot the 7 magically disappeared from your screenname. Mysterious.
Also: that was an impressively fast edit, upload, link.
@Gimby That is really how it is displayed to me
@Gimby I failed three times ._.
Zoe
Zoe
@geisterfurz007 xd
08:23
@AMDG so... i still see no concrete example
Zoe
Zoe
08:53
@Wietlol Would a cement example be acceptable?
I really, really, really pulled myself together to not do that... But of course you had to.
Zoe
Zoe
Yep :>
@Zoe at least it would be an example
Zoe
Zoe
@geisterfurz007 But I have confidence in my puns though. They're really solid.
Go away.
08:58
I'm so proud of you Zoe
Don't give her backup >.>
Zoe
Zoe
x3
she's a kid genious
In her own ways :D
Zoe
Zoe
:D
Zoe
Zoe
09:35
Come to think of it, I think these puns are waterproof too @gei!! :p
Zoe
Zoe
09:46
GitHub's octocat comes with an octodex :o
your puns are like a brick shithouse.
conversationally, my favorite Placebo song.
10:08
Good morning, fellow humans.
This might be a very stupid little question, but is there a way to do a generic "extends"?
Like class `NormalOrFormattedTextfield<T> extends T` where T could be JTextField or JFormattedTextfield
Because otherwise I'd have to copy a huge ton of code for that matter
not really
nice :3
Zoe
Zoe
You don't really need to
10:11
nvm I'm delusional
Yeah just noticed that
I could just use JFormattedTextField initially for that purpose -
Zoe
Zoe
If you have an Object return type, that method could also return an Integer. So if you have a JTextField, you could return a JFormattedTextField assuming JFormattedTextField extends JTextField
just JTextField
I mean - it shouldn't really matter whether I choose a JTextField or a JFormattedTextField until I apply a format to the second one, I guess...
JTextField field = new JFormattedTextField(format)
Yee I know, the problem's just that I have something like WeirdCustomField f = new WeirdCustomField() - where that one extends another class, which extends JTextField
10:18
maybe try composition instead of inheritance
But which should extend JFormattedTextField in some cases, but I guess it won't make a difference if it always does that
Yup, works fine to that point - thanks anyway :)
@Unknown even with very strict type bounds, you can't usually even compile generic inheritance
it would only work if you would have a low bound and an upper bound
which is basically stupid
however, such a thing is a real thing, it is called a decorator, (in Groovy, Traits, in Scala Mixins, etc)
in languages that do not support such a thing, you can still get very close by implementing all interfaces of the things you want to use, but there will always be downsides on that
Zoe
Zoe
10:36
<T extends JTextField> is valid though
yes, but not MyClass<T extends JTextField> extends T
10:49
@Wietlol you do realize I gave a canonical example right? You should be able to figure it out.
morn
user8622974
morn
morn
WHOA
The site. It changed
Now I can see all of my java chat replies from the left side of the screen instead of the right.
Oh wait... nvm it was just in user profile
I only accept examples that show in code how a certain problem exists and how it solves the problem
i also accept mastercard
I gave you code
I presented a problem
I gave you a solution
no excuses
10:56
you gave several pieces of code which form a puzzle
a puzzle that I have to solve
.-.
Zoe
Zoe
@Wietlol What about bitcoin, litecoin, etherium, and dogecoin?
> Suppose you want to use dependency injection explicitly, but you don't want to give direct control of initializing the fields to subclasses.
I dont like puzzles
The problem doesn't get more clear than that
> It should be obvious why this is an excellent usage of abstract classes which Josh Bloch did not mention in EJ, not even 3e.
10:57
@AMDG I wouldnt want DI on an abstract class
^ my only citation to EJ
that defeats most of DI's purposes
Hey, look, I'm just looking at the features and using them productively. Can't we all just get along?
Alright, so what do you suggest we do with abstract classes @Wietlol?
i wouldnt use abstract classes at all
there are rare cases where they can solve a very specific problem, but you generally never use them
Name that very specific problem
11:09
hmm... should have said "very specific problems"
situation specific problems
such as...
duplication of code
mostly
This sounds like improper use of abstract classes.
Then we should just use concrete classes and abstract abstract classes.
hence why I asked for an example of an abstract class
inheritance. Restricting the target implementation.
11:12
that is not an example, that is an idea
An example is also an idea
an example shows why something is a problem and shows why this particular solution solves that problem
I gave an example with dependency injection. We might never use it, but it exists.
I can give a better example though
your talk about that was still an idea, not an example
Alright.
Zoe
Zoe
11:15
var was added in Java 9, right?
10
afaik
Zoe
Zoe
Hm, weird... I'm using JDK 10 and set language features to Java 10 in IntelliJ, and it still doesn't recognize it
Problem: you are building an API, and you want to provide interfaces so that you can increase maintenance. However, you want to restrict users of the API in how they implement your interfaces. Let this be the canonical example for all possible abstract classes for this scenario:
Zoe
Zoe
Time to nuke the cache then.
AMDG!!
You shouldn't be here! Leave! Quickly! Before they find you >.<
11:21
What? I'm still typing!
You are building a driver API in Java using JNI (cough LWJGL cough). You provide a Driver interface. You want this interface to be implemented by anyone intending to extend your driver API. After all, your API isn't everything for every driver.
/afk
@AMDG Cya later
@Zoe hmm... this is rather annoying... i cant open the project structure
Zoe
Zoe
Just gonna leave var alone for now. Java 11 comes out in a month, hopefully IntelliJ gets a patch before then
@Zoe Yeah this is stange sometimes. IntelliJ complains that I didn't set the language level to 10 and I still get some methods marked as not existing in my currently selected version. Using the hotfix worked well tho for some reason.
/back
11:28
@AMDG Welcome back!
sorry for the delay
couldn't hold it
Anyways...
In Driver, you have various abstract method declarations, each of which takes in interfaces for parameters, and returns an interface.
This analogy should make it clear:
You are building a building
The interface is the blueprint. It defines the basic structure, but what are you going to build with? What is the floor made of? What color are you going to paint the building with? Or should it be made of glass? I don't know; you only defined Material, but you didn't specify what kind!
An abstract class can be used to restrict a Set<Material> to a subset of those Materials. You want a house, but it can only be made of either brick or wood, not concrete.
You can't define this in an interface without your rules being overridden, so you need an abstract class.
The same can be said for various kinds of drivers, or any kind of interface representing a very broad array of objects.
An abstract class can be a subset of the interface for a particular portion of your API that implements it explicitly and does not make the abstract class a (package-private?) skeleton class.
If you don't need to enforce any kind of functionality in your rules, only types, then you can make an interface, but if you need logic, or code enforcement, abstract classes are the only possibility, and if that weren't so, you'd need a concrete class, but that would be something which you would need to look at the documentation for, as usually you expect concrete classes to have public constructors, not protected or private, unless they have a public factory to expose the constructors.
When I say you can make an interface, I mean an interface that overrides the parent interface, and an abstract class that overrides the parent interface. Where appropriate, an abstract class should be used in favor of an interface overriding an interface.
It can also be said that, considering you can use virtual multiple inheritance with interfaces, you can prevent hybrid classes. For example, in the driver API, you should not have a class that is the composition or hybrid of a graphics driver and a network driver. There should be the graphics driver object, the network driver object, and a bridge object for communication between drivers anonymously so that drivers remain decoupled and agnostic to other hardware.
Hence, in such case, defining separate GraphicsDriver and NetworkDriver abstract classes would be more efficient and maintainable than a mere interface.
Q.E.D.
Quod erat demonstratum
@Wietlol are you satisfied now? I'm not trying again.
This Java 1.2 class I suppose tells what abstract classes were meant to do as I explained above.
/javadoc java.security.Permission
Ok I have to admit that Permission seems better as an interface, so nvm.
All of the methods in this class are abstract except for checkGuard and toString
i still see no code
for example, ask me a question
any question
Am I the only philosopher in here? Am I the only one imaginative enough to design an object in my head? Because that's usually what I do...
@Wietlol you may as well be a mathematician saying "I only accept equations; I don't do word problems."
I am a programmer, I only accept code
I ask you, then, the exact same question that I just explained above.
11:57
which is?
(besides, this argument is more philosophical and abstract than it is concrete. If you wanted something concrete, then we should have moved on to a concrete argument that doesn't involve canonical answers or questions.)
Using the building analogy I gave above: how could I restrict a Building interface to only a specific subset of building Materials?
The oldest known dog bones were found in Asia and date as far back as 10,000 B.C. The first identifiable dog breed appeared about 9000 B.C. and was probably a type of Greyhound dog used for hunting. (source)
i dont think you can
But with abstract classes, you can!
i still think you cant
12:01
building interface: void walls(Material material); void floor(Material material);... and a material for every structure and aesthetic part of the building.
that is not an interface
this is an interface:
public interface Building
{
	void walls(Material material);

	void floor(Material material);
}
Um... I know what an interface is, but we are wise, wizened, experienced programmers here; I wrote the shorthand version.
your shorthand version is missing crucial parts though
but continue
WoodBuilding abstract class: @Override public final void walls(WoodMaterial material) {...} @Override public final void floor(WoodMaterial material) {...}
You are now restricted to using materials made of wood.
have you tried to compile that?
12:05
Well it probably compiles on Java 9's REPL...
but it is a code fragment
probably = no ;)
You are allowed to override the type of an interface if it holds
That or I'm thinking of generics...
No, I distinctly recall overriding the type as well in a method.
Ok, then it was definitely generics, in which case it is due to type erasure.
you are allowed to change the parameter types to supertypes and the return types to subtypes
12:07
Yeah, that is what I meant
It was in there somewhere! :P
but you just changed a parameter type to a subtype, which creates a more restricted api, which doesnt satify the interface
ok, so we have this
WoodMaterial extends Material, wouldn't it?
yep
missed that
Zoe
Zoe
I'd assume there's concrete evidence to support that? I mean, the foundation is important to think about here.
no cement?
@AMDG but continue, I am still wondering how we can create a more restricted version of an interface (using abstract classes)
12:15
Indeed. A contract is a contract...
yeah, so composition, not through the interface.
im not sure what you mean though @Gimby
this is basically input validation on the overridden methods
Zoe
Zoe
@Wietlol nah
however I can still give AMDG a building that can use other materials than wood
and I dont want to couply my buildings to a class
also, i fail to see why WoodBuilding is abstract
it has no abstract members
12:21
@Wietlol indeed, I got distracted by the addition of constructors
No, but you could easily add abstract members, as wood has specific properties which vary based on the kind of wood used, such as birch, oak, or spruce.
such as?
it sounds like a very strong violation of LoD
LoD?
Law of Demeter
Level of Detail? What?
oh
12:30
also known as Law of Diaper for some people
You'll notice upon reexamination that there are two package-private methods for accessing both fields, implying it is intended that the members are to be read only by package members, and outside members only enjoy initialization.
WoodBuilding is effectively write-only from outside the package. It also makes more sense if you make this class serializable, or if this object is a proxy data transfer object along a pipeline and you only want the final results visible to the target of the object.
that answers my question perfectly... if my question was if you wanted to tell a nice story
> but you could easily add abstract members, as wood has specific properties which vary based on the kind of wood used, such as birch, oak, or spruce.
> such as?
For one, the kind of wood
give me a method!
if you don't want that to be an enum, then you can put all those members as private fields and... oh fine...
12:56
Forgot to add package access to Wood enum's members, but you get the point, I hope.
Anyways, I'm an hour late for my prayers all because of some coffee.
/afk
@AMDG Cya later
it still makes no sense -_-

« first day (2853 days earlier)      last day (2312 days later) »