OO Fundamentals in Java

0.0(0)
studied byStudied by 0 people
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/17

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

18 Terms

1
New cards

Core OO Concepts in Java

encapsulation principles and keywords remain the same as C++

  • classes

  • attributes

  • methods

  • constructors

  • public

  • private

  • composition

2
New cards

Classes

  • Use the class keyword to define

  • Create in a .java file matching the name of the class

  • Almost always declare as public

  • There is no header file 

3
New cards

Attributes

  • Create as variables inside the class

  • Almost always declare as private

4
New cards

Methods

  • Create inside the class

  • May be declared public or private

  • Implementation also goes inside the class definition

5
New cards

Constructors

  • Create as a standard method inside the class 

  • Name must match the name of the class 

  • No return type defined

  • Multiple constructors are permitted, provided the parameters are different 

6
New cards

public

  • Access modifier that states the following method/variable is accessible to all code

  • Add the public keyword explicitly before each method/variable 

7
New cards

private

  • Access modifier that states the following method/variable is accessible only to code inside the class

  • Add the private keyword explicitly before each method/variable

8
New cards

Composition

Classes may hold attributes of class type, not just primitive types…

9
New cards

Creating Object Instances - new

  • Just like C++, once defined, classes are types and can be realized into object instances

    • By yourself, or by other programmers you give your class file to

    • A realized class is called an object instance and is treated just like any variable 

  • In Java object instances are built from a class by using the new keyword:

Car c = new Car("White");

10
New cards

Java Object Creation

Car c = new Car("White");

Car c is a reference to Car object

11
New cards

Java Object Referencing

Java objects are automatically garbage collected (deleted) when they have no references…

Car c = new Car();
Car x = new Car();

12
New cards

Value Types and Reference Types

it is the data itself you are accessing/modifying or passing into a function

use pure lower case

E.g. int, char, float, double, boolean…

13
New cards

Reference Type

use object references to objects

whenever they are used, it is the reference that you are accessing/modifying

should use camelCase

E.g. Car, String, FluffyElephant, Minion, CatDog…

14
New cards

The this keyword in Java

  • a special keyword that returns an object reference to the object instance which the current method is executing in

  • can be used to unambiguously refer to methods and variables:

  • which can be particularly useful in constructors

  • but also anywhere we want to be explicit

  • or anywhere we want to pass the current object as a parameter

  • (C++ also has this, but it's a pointer)

<ul><li><p>a special keyword that returns an object reference to the object instance which the current method is executing in</p></li><li><p>can be used to unambiguously refer to methods and variables:</p></li><li><p>which can be particularly useful in constructors</p></li><li><p>but also anywhere we want to be explicit</p></li><li><p>or anywhere we want to pass the current object as a parameter</p></li><li><p>(C++ also has this, but it's a pointer)</p></li></ul><p></p>
15
New cards

The Java Class Library

a vast set of pre-written classes 

e.g. String, System, Math

16
New cards

API Documentation

  • Programmers document their classes, so other programmers can learn how to use them in their applications

    • Java uses a standard called JavaDoc

      • All Java programs use this, so they all have documentation in the same common format

      • This makes it very easy to learn about other people classes which promotes reuse

17
New cards

JavaDoc

  • Documentation is embedded inside your code using comment blocks

    • Remember Java can use /* */ comments like C

  • JavaDoc uses /** **/ to identify a JavaDoc comment

    • Additional ‘*’ characters on following lines just make it look pretty

  • JavaDoc is used to document all public classes, methods and constructors 

  • Comment block goes immediately before the item it is documenting

/*Car class represents all drivable vehicles with 4 wheels
the class is also capable of modelling the outward appearance, car model and the number of miles driven*/
public class Car{
}
  • Keywords allow structured documentation of methods, return values and required parameters

    • @param documents the meaning of the parameter called ‘name’ 

    • @return documents the meaning of a value returned from a method 

    • Many more: https://www.oracle.com/technical-resources/articles/java/javadoc-tool.html

/**
*creates a new Car with the given characteristics.
*@param col = the colour of the car being created.
*@param miles = the number of miles the car has driven.
**/
public Car(String col, int miles) {
	colour = col;
	milesDriven = miles;
}
  • JavaDoc can then generate written documentation automatically

    • The semi-structured comments and the Java code are processed to build web pages documenting your classes

    • These can then be posted online let other programmers know how to use your classes

  • Simply run the javadoc tool from the command line to generate the web pages

    • javadoc –d doc *.java 

    • The “-d” parameter specifies the directory (folder) to store the web pages in

    • After running javadoc, just double click ‘index.html’ in this folder

    • Recommend always specifying a folder – Javadoc produces a lot of html

18
New cards

The String Class Example

public static void main(String[] args) {
	String s = "Hello World";
	
	//examples of invoking methods on an instance
	int len = s.length();
	String l = s.toLowerCase();
	char[] myArray = s.toCharArray();
}