Skip to content

Instantly share code, notes, and snippets.

@rish-16
Last active January 21, 2021 09:00
Show Gist options
  • Select an option

  • Save rish-16/ae4a6fbf1b24f651f023b23e5b8e3852 to your computer and use it in GitHub Desktop.

Select an option

Save rish-16/ae4a6fbf1b24f651f023b23e5b8e3852 to your computer and use it in GitHub Desktop.
Notes on CS2040S from Week 1 Lecture 2

CS2040S Week 1 Lecture 2

Notes from Week 1 on Data Structures and Algorithms.

Not covered

  • Lambda expressions
  • Type inference
  • Packages

Basics of OOP

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;
}

Access Control

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 public methods
  • Classes can have both public and private methods

Initializing an object

  • You need to use constructors with attributes that need to be passed in during instantiation

Common Problems

  • You need a main method
  • Ensure editor is running the right main method
  • Ensure class name == file name For solutions to most problems, check out the Coursemology Forum.

Extra Links

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment