lectures.alex.balgavy.eu

Lecture notes from university.
git clone git://git.alex.balgavy.eu/lectures.alex.balgavy.eu.git
Log | Files | Refs | Submodules

commit e7a225e906cc01d373eb844a7f54ddb903cbc681
parent ec24c6c93b81031c1f87ddb905fb4c36aa46b5ef
Author: Alex Balgavy <alex@balgavy.eu>
Date:   Tue, 17 Aug 2021 11:08:27 +0200

Advanced programming (Java)

Diffstat:
Mcontent/_index.md | 1+
Acontent/ap-notes/_index.md | 17+++++++++++++++++
Acontent/ap-notes/classes-and-instantiation.md | 44++++++++++++++++++++++++++++++++++++++++++++
Acontent/ap-notes/encapsulation.md | 32++++++++++++++++++++++++++++++++
4 files changed, 94 insertions(+), 0 deletions(-)

diff --git a/content/_index.md b/content/_index.md @@ -30,6 +30,7 @@ title = "Alex's university course notes" ## Subject notes: Year 2 * [Data Structures & Algorithms](dsa-notes/) +* [Advanced Programming](ap-notes/) * [Statistical Methods](stats-notes) * [Operating Systems](os-notes) * [Intelligent Systems](is-notes/) diff --git a/content/ap-notes/_index.md b/content/ap-notes/_index.md @@ -0,0 +1,17 @@ ++++ +title = 'Advanced Programming' ++++ + +# Advanced Programming +- [Classes and instantiation](classes-and-instantiation) +- [Encapsulation](encapsulation) +- Inheritance +- Polymorphism +- Packages +- Inner/nested classes + - private, protected +- Abstraction +- Interfaces +- Exceptions +- Data structures + - enum, hashmap, arraylist, iterator diff --git a/content/ap-notes/classes-and-instantiation.md b/content/ap-notes/classes-and-instantiation.md @@ -0,0 +1,43 @@ ++++ +title = 'Classes and instantiation' ++++ +# Advanced Programming +Classes have the form: + +```java +public class ClassName { +} +``` + +Instantiate classes with `new`: + +```java +ClassName instance = new ClassName(); +``` + +The name of the Java file should match the class name. +One (main) class per file. + +Access variables via dot syntax: `car.brand` + +Instance variables are declared in the body of a class. +`static` variables and methods are class variables/methods. + +Constructors are public, and have the same name as the class: + +```java +public class Main { + int x; + + public Main() { + x = 42; + } + + public static void main(String[] args) { + Main myMain = new Main(); + System.out.printf("The constructor sets the value: %d", myMain.x); // 42 + } +} +``` + +There are no destructors, as Java is garbage-collected.+ \ No newline at end of file diff --git a/content/ap-notes/encapsulation.md b/content/ap-notes/encapsulation.md @@ -0,0 +1,31 @@ ++++ +title = 'Encapsulation' ++++ +# Encapsulation +Encapsulation makes sure that sensitive stuff is hidden from users. + +Idea: declare class variables/attributes as private, provide public getters and setters to use the variable. + +```java +public class Safe { + private String superPrivateInfo; + + public String getSuperPrivateInfo() { + return superPrivateInfo; + } + + public void setSuperPrivateInfo(String newInfo) { + this.superPrivateInfo = newInfo; + } +} +``` + +Classes can be: +- `public`: accessible by any other class +- default: only accessible in same package + +Attributes/methods/constructors can be: +- `public`: accessible by any other class +- `private`: only accessible within declared class +- default: only accessible in same package +- `protected`: only accessible in same package and subclasses+ \ No newline at end of file