# How does Snap! OOP work as compared to Java?

I'm used to OOP in Java, and I'm creating a custom curriculum in OOP and other programming concepts in Snap to make it easier for young children to understand. However, I am not exactly sure how to work with OOP in Snap, even after reading the manual. I am trying to represent a sprite as a class, and am trying to create an instance of that class in another class. However, my getter methods do not return any instance variables I supposedly instantiated in a Constructor. Apologies if this is a dumb question. Code is attached below.

<?xml version="1.0"?>

-<project version="1" app="Snap! 5.4, http://snap.berkeley.edu" name="oop">

<notes/>

-<stage name="Stage" id="1" scheduled="false" sublistIDs="false" inheritance="true" codify="false" ternary="false" lines="round" pan="0" volume="100" penlog="false" threadsafe="false" tempo="60" color="255,255,255,1" costume="0" height="360" width="480">

+<costumes>

-<sounds>

<list id="3" struct="atomic"/>

</sounds>

<variables/>

<blocks/>

<scripts/>

-<sprites>

-<sprite draggable="true" name="Dog" id="8" pan="0" volume="100" color="0,99.55199999999995,155.54999999999998,1" costume="1" pen="tip" rotation="2" scale="1" heading="90" y="9" x="-112" idx="2">

-<costumes>

-<list id="9">

-<item>

</item>

</list>

</costumes>

-<sounds>

<list id="11" struct="atomic"/>

</sounds>

<blocks/>

-<variables>

-<variable name="name">

<l/>

</variable>

-<variable name="breed">

<l/>

</variable>

</variables>

<scripts/>

</sprite>

<watcher color="243,118,29" y="10" x="10" hidden="true" style="normal" var="name" scope="Dog"/>

<watcher color="243,118,29" y="31.000001999999995" x="10" hidden="true" style="normal" var="breed" scope="Dog"/>

-<sprite draggable="true" name="DogRunner" id="17" pan="0" volume="100" color="80,80,80,1" costume="1" pen="tip" rotation="1" scale="1" heading="90" y="-53" x="-177" idx="1">

-<costumes>

-<list id="18">

-<item>

</item>

</list>

</costumes>

-<sounds>

<list id="20" struct="atomic"/>

</sounds>

<blocks/>

-<variables>

-<variable name="myDog">

-<context id="23">

<inputs/>

<variables/>

-<block s="newClone">

<l>Dog</l>

</block>

<ref id="17"/>

-<origin>

<ref id="17"/>

</origin>

-<context id="28">

<inputs/>

-<variables>

-<variable name="name">

<l>Bob</l>

</variable>

-<variable name="breed">

<l>Poodle</l>

</variable>

</variables>

<ref id="17"/>

-<origin>

<ref id="17"/>

</origin>

</context>

</context>

</variable>

</variables>

-<scripts>

-<script y="145" x="112">

<l>What's the name?</l>

</block>

-<block s="doDeclareVariables">

-<list>

<l>name</l>

</list>

</block>

-<block s="doSetVar">

<l>name</l>

</block>

-<block s="doDeclareVariables">

-<list>

<l>breed</l>

</list>

</block>

<l>What's the breed?</l>

</block>

-<block s="doSetVar">

<l>breed</l>

</block>

-<block s="doSetVar">

<l>myDog</l>

-<block s="reifyReporter">

-<autolambda>

-<block s="newClone">

<l>Dog</l>

</block>

</autolambda>

<list/>

</block>

</block>

-<block s="doTellTo">

<block var="myDog"/>

-<block s="reifyScript">

-<script>

-<block s="doRun">

-<block s="reifyScript">

-<script>

-<custom-block s="Constructor %s %s">

<block var="name"/>

<block var="breed"/>

</custom-block>

</script>

<list/>

</block>

<list/>

</block>

</script>

<list/>

</block>

<list/>

</block>

-<block s="doSayFor">

<block var="myDog"/>

-<block s="reifyReporter">

-<autolambda>

<custom-block s="getName"/>

</autolambda>

<list/>

</block>

<list/>

</block>

<l>3</l>

</block>

-<block s="doSayFor">

<block var="myDog"/>

-<block s="reifyReporter">

-<autolambda>

<custom-block s="getBreed"/>

</autolambda>

<list/>

</block>

<list/>

</block>

<l>3</l>

</block>

</script>

</scripts>

</sprite>

</sprites>

</stage>

<hidden/>

<code/>

-<blocks>

-<block-definition s="Constructor %'inputName' %'inputBreed'" category="other" type="command">

<code/>

<translations/>

-<inputs>

<input type="%s"/>

<input type="%s"/>

</inputs>

-<script>

-<block s="doSetVar">

<l>name</l>

<block var="inputName"/>

</block>

-<block s="doSetVar">

<l>breed</l>

<block var="inputBreed"/>

</block>

</script>

</block-definition>

-<block-definition s="getName" category="other" type="reporter">

<code/>

<translations/>

<inputs/>

-<script>

-<block s="doReport">

<block var="name"/>

</block>

</script>

</block-definition>

-<block-definition s="getBreed" category="other" type="reporter">

<code/>

<translations/>

<inputs/>

-<script>

-<block s="doReport">

<block var="breed"/>

</block>

</script>

</block-definition>

</blocks>

<variables/>

</project>


You should use JS OOP.There is a JS funct block in the op cat.

Ah I see. Thanks, I will look into it!

No, no, no, please do not use JS OOP!

I'm sorry, but there are some overly-eager participants in this forum - mostly kids I suspect - who always come up with the "use JS" answer, just because Snap! also supports expanding it with JS. But that's totally not the answer. I think we might have to introduce a teachers-only section in the forums at one point, so serious questions don't always get diluted by half-informed single-line "use JS" answers.

If you wish to access variables of another object you can use the OF block. Remember to first select the target object ("receiver") in the right input slot, then you'll see both that object's variables and it's custom blocks in the drop-down of the left input slot.

Is that what you were looking for?

Oh,Sorry...

Yes, that makes sense. Thank you!

Bear in mind that our OOP implementation isn't class/instance based. Instead we use prototypes, so every object can serve as the parent of other objects. This is the right thing to encourage the tinkering style of work, where you make a house sprite with, I don't know, lights on and lights off methods, and you play with it until it works the way you like it, maybe you add a smoke-from-chimney method, and then you clone it a few times to make more houses. But the first one is still a house, not an abstracted house class. So that's quite different from Java, whose idea is that your team of 500 programmers will design the entire class structure before actually instantiating the house (or any other) class. (If it really bothers you, you can tell the parent house to HIDE and then it acts like a class.)

You can use my Objects library(actually,just from the manual)You should use the new Object block first,to create a blank obj(actually,not very blank because there are some methods:parent,method,set,del-var,and del-method)(store it in a variable or it will be lost in RAM )and do changes to the prototype(What can you do:

• Give it a method
• Give it a property
• Take away a method
• Take away a property
• Clone it

)Finally,you use these methods and properties to:

• Create a namespace(which is very good so you dont need to tell your partener what did you define)
• Pack data(which is also very good so you will declare less variables,with more readableness than using lists)
• Convert oop code(Help,no Dicts in snap!)(Help,no free objs in snap!)
• Make your code like other oop code

Very important! You should not use "assoc,found?,insert,delete"These are ASSOC functions,and you dont need them unless you want to simulate a dict.

Please do not include XML files in forum posts. It makes it very hard and offputting for other people to read the forum, and it's not that easy for the intended reader to use. Instead, make and share a project containing just the blocks you want to share, and put the URL in your post. Then the recipient can load your project and use Export Blocks to get them as a library.