-
Notifications
You must be signed in to change notification settings - Fork 0
/
oop.html
129 lines (111 loc) · 6.13 KB
/
oop.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
<!DOCTYPE html>
<html lang="en">
<head>
<title>Intro to OOP</title>
<meta name="viewport" content="width=device-width, height=device-height, initial-scale=1">
<link rel="stylesheet" href="style.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<script src="index.js"></script>
<link rel="icon" type="image/x-icon" href="images/star.png">
</head>
<body onload="head(); dateTime(); foot();">
<header id="header" style="position:fixed;"></header>
<h1 class="title">Intro to OOP: Classes and Objects</h1>
<div class="text">
<h3>What is OOP?</h3>
<p>OOP, or object oriented programming, is a way of coding using objects. Like physical objects, they have properties and behaviour and are useful to model real-life situations</p>
<h3><br>Objects</h3>
<p>Characteristics of real-life objects </p>
<ul>
<li>Made of tangible material</li>
<li>Has properties</li>
<li>Can do things and can have things done to it</li>
</ul>
<p class="underline">Example:</p>
<img src="images/pen.jpg" style="height:30%; width:30%;">
<p class="caption">An example of a real life object is a pen. It is made of tangible materials (ex. metal, plastic), has properties (ex. black, blue ink, metal tip), and can do things/can have things done to it (ex. write, hold it).</p>
<p>Characteristics of software objects:</p>
<ul>
<li>Identity: Occupies a designated chunk of memory</li>
<li>State: The variables (properties) which may change</li>
<li>Behaviour: The methods that enable the object to perform things</li>
</ul>
<h3><br>What is a Class?</h3>
<p>A class is a description of a type of object, or a template. It realizes the identity, state, and behaviour of an object. To use a class, one must first create an object. Objects of a class have all attributes and behaviour (variables and methods) of that class, but the attributes may differ. A class is inheriently a data structure (Ex. Integer). Class names always start with a capital letter.</p>
<p class="underline">Example:</p>
<div class="code">
<pre>
public class Pen{
private String material;
private String colour;
public void write(){
//write using pen
}
public void holdPen(){
//hold the pen
}
}
</pre>
</div>
<h3><br>Constructors</h3>
<p>A constructor is what is used to create an object. A constructor always has the same name as the class, and is often used with values, called parameters to allow for customization. This is called overloading. However, all constructors of a class create the same type of object. The only difference is that they have different attributes. Note that when an object of a class without a constructor is created, Java will provide a default (no parameter) constructor.</p>
<p class="underline">Example:</p>
<div class="code">
<pre>
public class Pen{
private String material;
private String colour;
public Pen(){
material = "steel";
colour = "black";
}
//customization of Pen
public Pen(String penMat, String penCol){
material = penMat;
colour = penCol;
}
//all other methods not implemented
}
</pre>
</div>
<h3><br>Static vs. Instance Methods</h3>
<p>Static methods are methods that belong to the class definition. They are not part of objects when an object is created, and thus there is only one of that method. Static methods can be run without creating an object of the class.</p>
<p>Instance methods exist only when they are part of on object. Thus, an object needs to be created before they can be executed.</p>
<h3><br>Instance vs. Local Variables</h3>
<p>Instance Variables:</p>
<ul>
<li>Declared inside the class but outside any methods</li>
<li>New instances of these variables are created every time a new object is created from the class</li>
<li>Every instance of a class will have the same types of variables contained within it, but what's stored in those variables will be different from one object to another</li>
</ul>
<p>Local Variables:</p>
<ul>
<li>found within a method and nowhere else</li>
<li>Only visible within the method</li>
</ul>
<h3><br>Accessing Variables and Methods</h3>
<p>The variables and methods of an object are said to be the members of that object. These members can be accessed with dot notation. However, an object of the class needs to first be created. To do so, use the <span class="textCode">new</span> keyword and the specified parameters. Once an object is created, any public members can be accessed.</p>
<p class="underline">Example:</p>
<div class="code">
<pre>
//in a class other than Pen
Pen p1 = new Pen(); //default pen
Pen p2 = new Pen("plastic", "blue"); //specific pen
p1.write();
p2.hold();
</pre>
</div>
<h3><br>Programming Style</h3>
<p>Everything in a Java program is brought together in the main or driver method, which contains the main method. The public keyword allows other classes to access methods in that class, while the private keyword doesn’t allow access to the method/variable in that class.</p>
<h3><br>Unified Modelling Language (UML)</h3>
<p>A UML is a modelling laguage used to demonstrate the design of a class. It contains the class name, variable names and types, and method names, parameters, and return types. A "+" sign represents public, and a "-" sign represents private.</p>
<img src="images/uml.png" width="70%" height="auto">
<h2><br>Common Mistakes</h2>
<ol>
<li>Making a contructor with a name different from the class</li>
<li>Declaring instance variables as public - this may cause problems later on</li>
</ol>
</div>
<footer id="footer"></footer>
</body>
</html>