Notes from Week 1 on Data Structures and Algorithms.
- Lambda expressions
- Type inference
- Packages
Programming paradigms
- Imperative
- Functional
- Declarative
- Object Oriented
Principles behind OOP
- Abstraction – separate interface from implementation (don't worry about details)
- Encapsulation - hide implementation from user-facing interface
- Inheritance - build nw classes by entending functionality
- Polymorphism - Change functionality via implementation
Principles of Java
- Everything is an object.
- Everything has a type
An object has a state (data) and behaviour (methods).
- A class is a template for an object (name of class == name of file)
- Each class has member variables (attributes / fields)
- Each class also has member methods (functions)
Class vs. Object
- Class is a template for how to make an object
- Object is an instantiation of a class ("using the template")
- Regular variables/functions are per object
- Static variables/functions are per class
Interface
How to manipulate an object. Provides methods to interact with a class. Only contains method names with no real code inside. Using implements in your code promises to implement and use the Interface. Also, write your interface names with I in front of it (eg: IFile)
To use an interface,
// Do not need to worry about implementation of IFile or File
IFile copyFile(IFile oldFile) {
File newFile = new File();
FileData data = oldFile.getData();
newFile.setData(data);
return newFile;
}Behaviour is public while data is private. Always specify the access you want.
private: used within the class (attributes and methods)public: used by anyone (attributes and methods)protected: within the same package, and by subclasses
If nothing specified, it can be used within the package.
public class A {
private int secretFunction();
}
public class B {
public int secretFunction(A example) {
int readMe = example.secretFunction(); // crashes because secretFunction is private
}
}- Interfaces have completely
publicmethods - Classes can have both
publicandprivatemethods
Initializing an object
- You need to use constructors with attributes that need to be passed in during instantiation
- You need a
mainmethod - Ensure editor is running the right
mainmethod - Ensure class name == file name For solutions to most problems, check out the Coursemology Forum.