-
Notifications
You must be signed in to change notification settings - Fork 34
Create proposal.md (Coding for (Not Quite) Dummies: Object Oriented Programming) #255
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
michaelloughnane
wants to merge
8
commits into
master
Choose a base branch
from
#CodingFor(NotQuite)Dummies-ObjectOrientedProgramming
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
6a4e122
Create proposal.md
michaelloughnane 42a4e1f
Sample Code for Presentation
michaelloughnane 17dc4e2
Create blog.md
michaelloughnane 7759601
Update blog.md
michaelloughnane 1927ccb
Update blog.md
michaelloughnane 00ba70b
Update blog.md
michaelloughnane 1de64e7
Update Person.java
michaelloughnane b59566a
first edits
alliebailey File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -29,7 +29,9 @@ In 1969, Martin M. Broadwell made one of the first references to a psychological | |
|
|
||
|
|
||
|
|
||
| This guide hopes to be different. I’m assuming, if you’re reading this, you’re on your journey from incompetence to competence, and to this end have the fundamentals already down - variables, loops, basic syntax, and other things like that. These lessons will discuss higher level concepts and techniques, and hopefully convince you that the past few hours you spent on Codecademy or whatever your teaching method of choice was wasn’t a waste of time. Even making it to stage 3 in this context - competence with coding - is no simple task, and one that we all are taking steps to achieve, but with any luck these articles can help guide you down that path. | ||
| This guide hopes to be different. I’m assuming, if you’re reading this, you’re on your journey from incompetence to competence, and to this end have the fundamentals already down - variables, loops, basic syntax, and other things like that (if not, give [Codecademy](https://www.codecademy.com/learn/learn-java) or some other coding website a visit, and go through the first few lessons they have. You probably don't need to go through the whole thing, but the more the merrier). These lessons will discuss higher level concepts and techniques, and hopefully convince you that the past few hours you spent on Codecademy or whatever your teaching method of choice was wasn’t a waste of time. Even making it to stage 3 in this context - competence with coding - is no simple task, and one that we all are taking steps to achieve, but with any luck these articles can help guide you down that path. | ||
|
|
||
|
|
||
|
|
||
| ### Part 2: What is an Object? | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Again, for all the headers, please remove everything except the title! |
||
|
|
||
|
|
@@ -47,25 +49,34 @@ To start, let’s make a public class called “Person” (since that’s the ty | |
|
|
||
|  | ||
|
|
||
| To clarify, we’re making this object in its own file. To actually create a “person” object, we’ll just leave the “Person.java” file in the same directory as whatever program is actually using the object, then create an object in that file. How do we actually create the object, though? Well, that uses something we call a **constructor**. A constructor is a bit of code in the object class (the Person.java file) that lets you know how exactly you’re going to create a Person object in your code. Here’s a sample one I wrote: | ||
|
michaelloughnane marked this conversation as resolved.
Outdated
|
||
|
|
||
|  | ||
|
|
||
| To clarify, we’re making this object in its own file. To actually create a “person” object, we’ll just leave the “Person.java” file in the same directory as whatever program is actually using the object, then create an object in that file. How do we actually create the object, though? Well, that uses something we call a **constructor**. A constructor is a bit of code in the object class (the Person.java file) that lets you know how exactly you’re going to create a Person object in your code. Here are two that I wrote: | ||
|
|
||
|  | ||
|
michaelloughnane marked this conversation as resolved.
Outdated
|
||
|
|
||
|
|
||
|
|
||
| So, what’s going on here? | ||
| Whenever we make an object, we’re making a copy of this Person object with its own firstName, lastName, and age values. These lines of code mean that, when we want to make a Person in our code, we need to give it a first name, a last name, and an age - like this! | ||
| Whenever we make an object, we’re making a copy of this Person object with its own firstName, lastName, and (optionally) age values. These lines of code mean that, when we want to make a Person in our code, we need to give it a first name, a last name, and, if we want, an age - like this! | ||
|
|
||
|
|
||
|
|
||
|  | ||
|
|
||
|
|
||
|
|
||
| Here’s an object in action. We’re making a Person object named “John” with a first name input of “John” (shocker), a last name input of “Smith”, and an age input of 26. Note that this is in a completely separate file. We’re just storing Person.java and PersonTest.java in the same directory. | ||
|
michaelloughnane marked this conversation as resolved.
Outdated
|
||
| Here’s an object in action. This line of code creates a new Person object, and runs the code inside the associated constructor for that object. In this case, we’re making a Person object named “John” with a first name input of “John” (shocker), a last name input of “Smith”, and an age input of 26. If we didn't know John's age, we could simply input "John" and "Smith" and the object would be created with the second constructor I wrote, since that one only needs a first and last name. In that case, the "age" value would be set to -1, which I'm using to indicate an unknown age. | ||
|
|
||
| *Note that this is in a completely separate file. We’re just storing Person.java and PersonTest.java in the same directory.* | ||
|
|
||
|
|
||
|
|
||
| With that, we’re 1 for 3 in the things we want this Person object to do. It can store that info, sure, but there’s no magic trick with Objects for accessing the variables within them*. We’ll have to write that in ourselves, via a method. | ||
|
|
||
| *there actually is indeed a trick for this, but it’s bad form. When we initially created those variables, we made them private. If they were instead public, we could then just access, and edit, them from anywhere. This typically isn’t what we’re looking to do though, since granting full access to those variables can be messy. If you’re coding Person.java, do you want the people using it to be able to modify firstName or lastName freely? Probably not.* | ||
| *There actually is indeed a trick for this, but it’s bad form. When we initially created those variables, we made them private. If they were instead public, we could then just access, and edit, them from anywhere. This typically isn’t what we’re looking to do though, since granting full access to those variables can be messy. If you’re coding Person.java, do you want the people using it to be able to modify firstName or lastName freely? Probably not.* | ||
|
|
||
| You most likely already know what a method is, but just in case, a real quick reminder: a **method** is just a fancy name for a function. You (optionally) input a value, it does something, and (optionally) outputs a value (the only required part of those three is that it does something). If we want methods to retrieve the variables, we just need to write some public methods in Person.java that will return the desired values: | ||
| You most likely already know what a method is, but just in case, a quick reminder: a **method** is just a fancy name for a function. You (optionally) input a value, it does something, and (optionally) outputs a value (the only required part of those three is that it does something). If we want methods to retrieve the variables, we just need to write some public methods in Person.java that will return the desired values: | ||
|
|
||
|  | ||
|
|
||
|
|
@@ -80,21 +91,23 @@ You’ll notice I added a getName() method that just returns the whole name, sin | |
|
|
||
|
|
||
|
|
||
|
|
||
| Using the method of an object is as simple as [specific object instance].[method name]. Here’s what the above code outputs: | ||
|
|
||
|  | ||
|
|
||
| Our last task for this is to change the age of the person. This one will use a method too, but it raises an important question: How exactly do we want to approach this? Do we want to give free access to increment the age, or do we want to simply increment it by one? An easy answer is to do both! | ||
| Quick Sublesson: Overloading | ||
|
|
||
|
|
||
| Our last task for this is to modify the age of the person. This one will use methods too, but it raises an important question: How exactly do we want to approach this? Do we want to give free access to increment the age, or do we want to simply increment it by one? An easy answer is to do both! | ||
|
|
||
| ###### Quick Sublesson: Overloading | ||
|
|
||
| *Note: This does not apply to every language! I know it works for C++ and Java, for example, but not Python. Do a quick google search to see if this will work for you. If not, skip this.* | ||
|
|
||
| In some languages, two methods can be given the same name, if they take different inputs - as seen in the code example you’ll see in a second. This is called **overloading**. | ||
| In some languages, two methods can be given the same name, if they take different inputs - as seen in the code example you’ll see in a second. This is called **overloading**. | ||
|
|
||
| I’ve gone ahead and made a function that takes an input and adjusts the age accordingly, and another that takes no input and simply increments the age by one. With that, we have our complete (simple) Person.java file! | ||
| I’ve gone ahead and made a function that sets the age to a new value, takes an input and adjusts the age accordingly, and another that takes no input and simply increments the age by one. With that, we have our complete (simple) Person.java file! | ||
|
|
||
|  | ||
|  | ||
|
michaelloughnane marked this conversation as resolved.
Outdated
|
||
|
|
||
| Our complete test program: | ||
|
|
||
|
michaelloughnane marked this conversation as resolved.
|
||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.