Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- My liberal-arts (think about programming lanugage like natural language) approach to software engineering and thinking about object orientation
- - use clear language to reduce cognitive overhead
- - blow off details until you need them
- - call things what they are so that what they are is what you call them
- - Think of parts of programming languages as a spoken language (Nouns, verbs, etc...)
- - Solve the easiest problems first to build emotional wellbeing and momentum
- - methods are verbs and verb phrases
- - classes/objects are our proper nouns like User, Transaction, Accounts, BlogPost, etc...
- - properties on our objects are lil' nouns or adjectives of our proper nouns
- - interfaces allow us adverbs/adjectives
- ---
- Ok, that sounds nice, but show me the code!
- If we have inheritance, then we do the following:
- - Specify the data type of a variable by the base/parent class
- `Person a = new Person("John Wayne");`
- `Person b = new User("Captain", "Janeway", true);`
- - we can also use our interface name to specify a variable data-type b/c person class implements the methods from the interface
- `Greeter c = new Person("Billy Bob");`
- `Greeter d = new User("Ada", "Lovelace", true);`
- Polymorphism == many forms
- - Suppose we have a class called Guest.
- - Guest does not inherit from person or user
- - Guest does, however, implement the Greeter interface
- `Greeter e = new Guest(); // compiler is good w/ this if the Guest class implements a sayHello()`
- `Person f = new Guest(); // will not pass the compiler b/c guest never inherited from person`
- parent classes allow us to specify a datatype
- interface allow us to specify a datatype
- - java is single inheritance which means only one parent class
- - but interfaces allow more flexibility
- ----
- Consider the following:
- Make an interface called Feedable with a feed method on it
- Make another interface called Petable with a pet method on it
- Make a parent class called Pet
- Make a Cat class that implments Feedable and Petable. Cat inherits from Pet.
- Make a PetRock class that implements Petable only. PetRock inherits from Pet.
- Make a Porcupine class implements Feedable. Porcupine inherits from Pet
- ```java
- Pet cat = new Cat();
- Pet rock = new PetRock();
- Pet porcupine = new Porcupine();
- ```
- ```
- // by ensuring that cat implements the petable interface, we're forcing whoever builds the Cat class to built out a pet method.
- cat.pet()
- cat.feed()
- Petable cat = new Cat(); // passes the compiler
- Petable porcupine = new Porcupine() // does not compile
- Feedable porcupine2 = new Porcupine() // compiles a-ok
- Feedbable rock = new PetRock() // does not compile
- ```
- all of this polymorism stuff is about balancing strictness of data types with flexibilty.
- interfaces are an IOU to yourself/team to figure out how to do things later.
- ```
- List a = new ArrayList();
- List b = new LinkList();
- ```
- ----
- BIG PICTURE:
- - variables abstract away values. (instead of hard coding everything, variables are placeholders)
- - conditionals abstract away decisions (so the code decides instead of a human)
- - loops abstract away repetion (so we tell the loop how many times to run rather than duplicating code)
- - functions abstract away process (so we don't duplicate processes)
- - classes abstract away particulars (because classes are about groups) just like CSS classes (one or multiple of things that have stuff in common) (stuff -> either behavior or properties)
- - objects allow us to create particular indiduals
- - interfaces abstract functionality even further (blow off the details (implementation) for later)
- all of this polymorism stuff is about balancing strictness of data types with flexibilty.
- all of this polymorism stuff is about balancing strictness of data types with flexibilty.
- all of this polymorism stuff is about balancing strictness of data types with flexibilty.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement