forked from ClassicallyGeek/gdi-intro-oop-java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
class2.html
1046 lines (1023 loc) · 33.1 KB
/
class2.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>GDI Introduction to Java and OOP Concepts</title>
<meta name="description" content="Intro to Object Oriented Programming with Java curriculum was developed by Ashley Price for Girl Develop It. The course is meant to be taught in 2 4-hour sections. The slides are customizable according to the needs of a given class or audience.">
<meta name="author" content="Girl Develop It">
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent" />
<link rel="icon" href="images/favicon.ico">
<link rel="stylesheet" href="reveal/css/reveal.css">
<link rel="stylesheet" href="reveal/css/theme/serif.css" id="theme">
<link rel="stylesheet" href="css/gdi.css">
<!-- For syntax highlighting -->
<link rel="stylesheet" href="reveal/plugin/highlight/styles/zenburn.css">
<!-- For the slides -->
<link rel="stylesheet" href="css/slides.css">
<!-- If use the PDF print sheet so students can print slides-->
<link rel="stylesheet" href="reveal/css/print/pdf.css" type="text/css" media="print">
<!--[if lt IE 9]>
<script src="lib/js/html5shiv.js"></script>
<![endif]-->
</head>
<body>
<div class="reveal">
<!-- Any section element inside of this container is displayed as a slide -->
<div class="slides">
<!-- Opening slide -->
<section>
<img src="images/gdi_logo_badge.png" alt="" />
<h3>Intro to Java and OOP Concepts</h3>
<h4>Class 2</h4>
<h5 class='title-page'>Developed for GDI by Ashley Price</h5>
</section>
<!-- Welcome-->
<section>
<h3>Welcome!</h3>
</section>
<section>
<section>
<h3>Review - Variables</h3>
<ul>
<li>Strongly typed</li>
<li>Primatives</li>
<li>Objects</li>
</ul>
</section>
<section>
<h3>Review - Flow Control</h3>
<ul>
<li>Choices</li>
<ul>
<li>If/Else</li>
<li>Switch</li>
</ul>
<li>Loops</li>
<ul>
<li>For Loops</li>
<li>While Loops</li>
</ul>
</ul>
</section>
<section>
<h3>Review - Objects</h3>
<ul>
<li>States - Also called properties and data members</li>
<li>Behaviors - actions the object can take. Known as methods</li>
</ul>
</section>
</section>
<section>
<h3>Agenda</h3>
<ul>
<li>Objects and Classes</li>
<li>Defining Fields and Methods</li>
<li>Data Encapsulation</li>
<li>Subclasses and Inheritance</li>
<li>Polymorphism</li>
<li>Abstraction</li>
<li>Interfaces</li>
</ul>
</section>
<section>
<h3>Featured Object</h3>
<p>MotorVehicles which are types of Vehicles and include cars and buses</p>
</section>
<section>
<aside class='notes'>
Create an "Interface" based on this discussion<br>
Have an engine whose state can be on or off<br>
you can turn an engine on<br>
you can turn an engine of<br>
------<br>
Have a current speed<br>
accelerate<br>
decelerate<br>
check your current speed -- accessor method<br>
------<br>
Potential additional members: <br>
color<br>
maximum number of passengers -accessor method<br>
current number of passengers -- accessor method<br>
</aside>
<p>Describe a Motor Vehicle's states and behaviors</p>
</section>
<section>
<section>
<aside class='notes'>
Think of how with a single recipe you can create cookies MANY times...<br>
Note to self: Bring cookies to class
</aside>
<h3>Classes</h3>
<p>A Class is a blueprint of an Object</p>
<p>It tells us how to build an instance of an Object.</p>
<p>From one blueprint we can create many instances of a single Object</p>
</section>
<section>
<h3>Class Declarations</h3>
<p>When defining a class you must include: </p>
<pre><code class='java'>
class ClassName {
...
}
</code></pre>
</section>
<section>
<h3>Create a new project</h3>
<ol>
<li>File > New > Project</li>
<li>Project name: com.gdi.vehicles</li>
</ol>
</section>
<section>
<h3>Create a new class with a main method</h3>
<ol>
<li>File > New > Class</li>
<li>Class Name: tester</li>
<li>Under <em>which methods would you like to create?</em>, check "public static void main(String[] args)"</li>
</section>
<section>
<h3>Create a new class</h3>
<ol>
<li>File > New > Class</li>
<li>Class Name: MotorVehicle</li>
</ol>
</section>
</section>
<section>
<h3>The Code</h3>
<pre><code class='java'>
public class MotorVehicle {
}
</code></pre>
</section>
</section>
<section>
<section>
<aside class='notes'>
Flip back to our notes and list the data members
</aside>
<h3>Data Members</h3>
<p>An Object has one or more data members that describe its state.</p>
</section>
<section>
<aside class='notes'>
public class MotorVehicle {<br>
boolean engineOn;<br>
int currentSpeed;<br>
int maxNumberPassengers;<br>
int numCurrentPassengers;<br>
}<br>
</aside>
<h3>Defining Data Members</h3>
<pre><code class='java'>
boolean engineOn;
int currentSpeed;
</code></pre>
</section>
</section>
<section>
<section>
<h3>Defining a Constructor</h3>
<p>When you create an Object, you should include at least one constructor. If you do not, the compiler automatically provides a default one.</p>
<p>A constructor is a special method that is invoked when you create a new instance of your object</p>
<p>It is common to set default values for your data members in the constructor</p>
</section>
<section>
<h3>Code It!</h3>
<pre><code class='java'>
public MotorVehicle() {
currentSpeed = 0;
engineOn = false;
}
</code></pre>
</section>
</section>
<section>
<section>
<h3>Defining Methods</h3>
<p>Method represent the behavior of the Objects.</p>
<p>A method signature includes the method's name plus the number and type of paramters</p>
</section>
<section>
<aside class='notes'>
addPassenger?<br>
removePassenger?
</aside>
<h3>MotorVehicle Methods</h3>
<pre><code class='java'>
public void accelerate();
public void decelerate();
</code></pre>
</section>
<section>
<h3>Code It! (Adding Methods)</h3>
<p>Let's add an accelerated method to our MotorVehicle class: </p>
<pre><code class='java'>
public void accelerate() {
currentSpeed = currentSpeed + 5;
}
</code></pre>
</section>
<section>
<aside class='notes'>
void decelerate(){<br>
currentSpeed = currentSpeed - 5;<br>
}<br>
<br>
switchEngineState() {<br>
if(engineOn == true) {<br>
engineOn = false;
} else { <br>
engineOn = true;
}<br>
<br>
void addPassenger(){<br>
currentNumPassengers++;<br>
}<br>
<br>
void removePassenger{<br>
currentNumPassengers--;<br>
}
</aside>
<h3><span class='individually'>Individually</span></h3>
<p>Add a decelerate method that slows the motor vehicle down in increments of 5.</p>
<p>Add a method that will swtich the engine's state</p>
<p><em>If</em> the engine is on turn the engine off</p>
<p><em>else</em> the engine is off so turn the engine on</p>
</section>
<section>
<h3>The Code</h3>
<pre><code class='java'>
public void decelerate() {
currentSpeed = currentSpeed - 5;
}
public void switchEngineState() {
if (engineOn == true) {
engineOn = false;
}
else {
engineOn = true;
}
}
</code></pre>
</section>
</section>
<section>
<section>
<p>You build an Object from a class</p>
<p>Objects are instances of your classes</p>
</section>
<section>
<h3>Objects - Creating </h3>
<p>To create an object:</p>
<pre><code class='java'>
ObjectType nameOfInstance = new ObjectType();
</code></pre>
<p>where ObjectType() is the Object's constructor. If the constructor expects parameters you would include them in this statement: </p>
<pre><code class='java'>
nameOfInstance = new ObjectType(String ex);
</code></pre>
</section>
<section>
<h3>Code It!</h3>
<p>Create a MotorVehicle Object</p>
<p>In the main method of the Tester class, add the following line: </p>
<pre><code class='java'>
MotorVehicle myVehicle = new MotorVehicle();
</code></pre>
</section>
</section>
<section>
<section>
<h3>Using Objects</h3>
<p>Once you have created your object, you can call its different methods with the following format: </p>
<pre><code class='java'>
nameOfInstance.methodName();
</code></pre>
<p>Similarly, you can access data members in this format: </p>
<pre><code class='java'>
nameOfInstance.datamember;
</code></pre>
</section>
<section>
<aside class='notes'>
MotorVehicle myVehicle = new MotorVehicle();<br>
myVehicle.switchEngineState();<br>
System.out.println("Engine is On: "+myVehicle.engineOn);<br>
System.out.println("Current Speed: "+myVehicle.currentSpeed);<br>
myVehicle.accelerate();<br>
System.out.println("Current Speed: "+myVehicle.currentSpeed);<br>
myVehicle.decelerate();<br>
System.out.println("Current Speed: "+myVehicle.currentSpeed);<br>
<br>
<strong>output:</strong><br>
Engine is on: true<br>
Current Speed: 0<br>
Current Speed: 5<br>
Current Speed: 0
</aside>
<h3>Code It!</h3>
<p>Now use a method of your motor vehicle class: </p>
<ol>
<li>call switchEngineState to turn on the car</li>
<li>Print engineOn and confirm it's true</li>
<li>Print the currentSpeed</li>
<li>Accelerate the vehicle</li>
<li>Print the new currentSpeed</li>
<li>Decelerate the vehicle</li>
<li>Print the new currentSpeed</li>
</ol>
</section>
<section>
<h3>The Code</h3>
<pre><code class='java'>
MotorVehicle myVehicle = new MotorVehicle();
myVehicle.switchEngineState();
System.out.println("Engine is on: " + myVehcile.EngineOn);
System.out.println("Current Speed: " + myVehicle.currentSpeed);
myVehicle.accelerate();
System.out.println("Current Speed: " + myVehicle.currentSpeed);
myVehicle.decelerate();
System.out.println("Current Speed: " + myVehicle.currentSpeed);
</code></pre>
<p>Output:</p>
<pre><code class='no-highlight'>
Engine is on: true
Current Speed: 0
Current Speed: 5
Current Speed: 0
</code></pre>
</section>
<section>
<aside class='notes'>
public void printDescription(){<br>
System.out.println("I'm a Motor Vehicle and my Engine is on "+engineOn+" and i'm going "+currentSpeed+"mph");<br>
}<br>
</aside>
<h3><span class='individually'>Individually</span></h3>
<p>Create a method called printDescription that will print the following statement:</p>
<pre><code class='no-highlight'>
I'm a MotorVehicle and my engine is on (true/false) and
I'm currently going (#)mph.
</code></pre>
<p>Test it by calling myVehicle.printDescription();</p>
</section>
<section>
<h3>The Code</h3>
<pre><code class='java'>
public void printDescription () {
System.out.println("I'm a MotorVehicle and my engine is on " +
engineOn + " and I'm currently going " +
currentSpeed + " mph.");
}
</code></pre>
</section>
</section>
<section>
<h3>Checkpoint</h3>
<p>Building basic Objects</p>
<ul>
<li>Methods</li>
<li>Data Members</li>
<li>Instances</li>
</ul>
</section>
<section>
<section>
<aside class='notes'>
We've already seen this with the public modifiers on our class declaration and method declaration
</aside>
<h3>Data Members - Access Modifiers</h3>
<p>We can make these data members public or private</p>
<p>Public members can be accessed by all other classes</p>
<p>Private memembers can only be accessed within its own class</p>
</section>
<section>
<h3>Code It!</h3>
<p>Mark your data members public and private</p>
<pre><code class='java'>
public boolean engineOn;
private int currentSpeed;
</code></pre>
</section>
<section>
<h3>Accessing Data Members</h3>
<p>If your fields are declared with the <em>public</em> access modifier you can access the data members directly with the format mentioned before: </p>
<pre><code class='java'>
instanceOfObject.fieldName
</code></pre>
</section>
<section>
<aside class='notes'>
The field MotorVehicle.currentSpeed is not visible<br>
b/c currentSpeed is now private<br>
b/c it's public
</aside>
<h3>Oops!</h3>
<p>What is the error we received?</p>
<p>Why did we receive it?</p>
<p>Why is myVehicle.engineOn not throwing the same error?</p>
</section>
</section>
<section>
<section>
<h3>Data Encapsulation</h3>
<p>With OOP, we have the concept of data encapsulation which discourages the direct, public access to a class's data members.</p>
</section>
<section>
<h3>Data Encapsulation</h3>
<p>Data Encapsulation is to hide or restrict direct access to an object's members</p>
<p>Instead of direct access a set of accessor and modifier methods are used to interact with the data.</p>
</section>
<section>
<h3>Benefits</h3>
<ol>
<li>Control to make fields read-only and write-only</li>
<li>Classes can have absolute control over what is stored in the field</li>
<li>Provides a layer of abstraction. Users of the class (other code or objects) do not need to know the details of the data storage</p>
</ol>
</section>
<section>
<aside class='notes'>
Change from public to private:<br>
private boolean engineOn;
</aside>
<h3>Encapsulating our Data</h3>
<p>Let's make all our data members private</p>
<p>Now we'll need to add accessor methods so we can still access the data</p>
</section>
<section>
<aside class='notes'>
public boolean getEngineOn(){<br>
return engineOn;<br>
}<br>
</aside>
<h3>Code It!</h3>
<p>Add an accessor method to get the current speed: </p>
<pre><code class='java'>
public int getCurrentSpeed() {
return currentSpeed;
}
</code></pre>
<p><span class='individually'>On your own,</span> add an accessor method for the boolean engineOn</p>
</section>
<section>
<pre><code class='java'>
public boolean getEngineOn() {
return engineOn;
}
</code></pre>
</section>
<section>
<aside class='notes'>
System.out.println("Engine is On: "+myVehicle.getEngineOn());<br>
System.out.println("Current Speed: "+myVehicle.getCurrentSpeed());<br>
myVehicle.accelerate();<br>
System.out.println("Current Speed: "+myVehicle.getCurrentSpeed());<br>
myVehicle.decelerate();<br>
System.out.println("Current Speed: "+myVehicle.getCurrentSpeed());<br>
</aside>
<h3>Update our Code</h3>
<p>Now instead of calling on your data members directly, use the accessor methods we just created to get their values.</p>
<pre><code class='java'>
System.out.println("Current Speed: " + myVehicle.getCurrentSpeed());
</code></pre>
</section>
</section>
<section>
<section>
<h3>Static vs Instance</h3>
<p>When a method is declared static, it is <strong>shared</strong> by all instances of the class.</p>
<p>In contrast, the type of methods we've been using have been <strong>unique</strong> to each instance.</p>
</section>
</section>
<section>
<section>
<aside class='notes'>
The constructor
</aside>
<h3>Static: An Example</h3>
<p>How many cars on the road?</p>
<p>Create a static data member to keep count of the MotorVehicles we've created: </p>
<pre><code class='java'>
private static int countCreated = 0;
</code></pre>
<p>Each time a MotorVehicle is created we want to increment the count.</p>
<p>Where would we put the code to do so?</p>
</section>
<section>
<h3>Code It</h3>
<pre><code class='java'>
public MotorVehicle() {
currentSpeed = 0;
engineOn = false;
countCreated++;
}
</code></pre>
</section>
<section>
<h3>Add an Accessor</h3>
<p>We can create a regular accessor method to return the countCreated.</p>
<pre><code class='java'>
public int getCountCreated() {
return countCreated;
}
</code></pre>
<p><span class='individually'>On your own,</span> print the countCreated</p>
</section>
<section>
<h3>The Code</h3>
<pre><code class='java'>
System.out.println("Number of vehicles on the road: " +
myVehicle.getCountCreated());
</code></pre>
</section>
<section>
<aside class='notes'>
Note that they are the same because they are referencing the same data member
</aside>
<h3>Testing our Static Data Member</h3>
<ol>
<li>Create a second MotorVehicle myVehicle2</li>
<li>Print the result of myVehicle.getCountCreated()</li>
<li>Print the result of myVehicle.getCountCreated()</li>
</ol>
</section>
</section>
<section>
<section>
<h3>Static Method</h3>
<p>Since the <em>countCreated</em> is not specific to the instance, we might want to make a method that is not specific to the instance when accessing the data.</p>
<p>We can create static methods that (like a static data member) is shared by all instance.</p>
</section>
<section>
<h3>Code It</h3>
<p>Add the static keyword to your method declaration: </p>
<pre><code class='java'>
public static int getCountCreated() {
return countCreated;
}
</code></pre>
</section>
<section>
<aside class='notes'>
System.out.println("Number of Vehicles: "+MotorVehicle.getCountCreated());<br>
MotorVehicle myVehicle2 = new MotorVehicle();<br>
System.out.println("Number of Vehicles: "+MotorVehicle.getCountCreated());
</aside>
<h3>Accessing the static method</h3>
<p>You can access a static method the same way you access an instance method, but it is discouraged since it doesn't clearly indicate you accessing a class method.</p>
<p>Instead, we will update our code to use this format: </p>
<pre><code class='java'>
ClassName.staticMethodName();
</code></pre>
</section>
<section>
<h3>The Code</h3>
<pre><code class='java'>
System.out.println("Number of Vehicles: " +
MotorVehicle.getCountCreated());
MotorVehicle myVehicle2 = new MotorVehicle();
System.out.println("Number of Vehicles: " +
MotorVehicle.getCountCreated());
</code></pre>
</section>
</section>
<section>
<h3>Checkpoint</h3>
<ul>
<li>Data Encapsulation</li>
<li>Static vs Instance</li>
</ul>
</section>
<section>
<section>
<h3>Inheritance and Subclasses</h3>
<p>A class can be derived from another class</p>
<p>A class that is derived from another class is called a <em>subclass</em></p>
<p>Subclasses inherit fields and methods from their parent class</p>
</section>
<section>
<h3>Why would you want it? Inheritance</h3>
<p>New class is created from an existing class</p>
<p>Reuse code</p>
<p>Can be used to create a hierarchical class structure</p>
</section>
<section>
<h3>MotorVehicle -> Car</h3>
<p>Let's create a car</p>
<p>File > New > Class</p>
<p>Name: Car</p>
<p>Add the extends keyword and the name of the parent class (MotorVehicle)</p>
<pre><code class='java'>
public class Car extends MotorVehicle {
...
}
</code></pre>
</section>
<section>
<h3>What you can do with a Subclass</h3>
<p>You can write a subclass constructor that invokes constructor of the superclass</p>
<p>You can declare new fields in subclass that aren't in the parent class</p>
<p>Inherited fields can be used directly</p>
<p>A subclass can't inherit private members</p>
</section>
</section>
<section>
<section>
<h3>Super</h3>
<p>The Super keyword can be used in a child to call a method or member of the parent class</p>
<p>The syntax is super.methodName() or super.memberName;</p>
<p>If you want to call the constructor of the super class you simply use the syntax: </p>
<pre><code class='java'>
super();
</code></pre>
</section>
<section>
<aside class='notes'>
public Car(){<br>
super();<br>
}
</aside>
<h3>Code It!</h3>
<p>Create a car constructor that calls super()</p>
</section>
<section>
<h3>The Code</h3>
<pre><code class='java'>
public class Car extends MotorVehicle {
public Car() {
super();
}
}
</code></pre>
</section>
</section>
<section>
<section>
<aside class='notes'>
For instance, while a motor boat doesn't necessarily have a door or a trunk all cars do
</aside>
<h3>What else can you do with a subclass?</h3>
<p>You can declare new fields in subclass that aren't in the parent class.</p>
</section>
<section>
<aside class='notes'>
public class Car extends MotorVehicle {<br>
<strong>private boolean trunkOpen;<br>
private int numDoors;</strong><br>
public Car() {<br>
super();<br>
}<br>
}
</aside>
<h3>Code It!</h3>
<ol>
<li>Add a boolean called trunkOpen</li>
<li>Add an int called numDoors</li>
</ol>
</section>
<section>
<h3>The Code</h3>
<pre><code class='java'>
public class Car extends MotorVehicle {
private boolean trunkOpen;
private int numDoors;
public Car() {
super();
}
}
</code></pre>
</section>
<section>
<aside class='notes'>
public Car() {<br>
super();<br>
<strong>trunkOpen = false;<br>
numDoors = 4;</strong><br>
}
</aside>
<h3>Augment the constructor</h3>
<p>Our constructor should also be initialize the subclass-only data members</p>
<p>Initialize trunkOpen to false and numDoors to 4</p>
</section>
<section>
<h3>The Code</h3>
<pre><code class='java'>
public class Car extends MotorVehicle {
private boolean trunkOpen;
private int numDoors;
public Car() {
super();
trunkOpen = false;
numDoors = 4;
}
}
</code></pre>
</section>
<section>
<aside class='notes'>
Car myCar = new Car();<br>
myCar.switchEngineState();<br>
System.out.println(myCar.getCurrentSpeed());<br>
myCar.accelerate();<br>
System.out.println(myCar.getCurrentSpeed());<br>
myCar.printDescription();<br>
System.out.println("Number of Vehicles: "+MotorVehicle.getCountCreated());
</aside>
<h3>Test it Out</h3>
<p>In your HelloWorld class:</p>
<ol>
<li>Create a car "myCar"</li>
<li>Turn the engineOn</li>
<li>Print the car's current speed</li>
<li>Accelerate the car</li>
<li>Print the car's new current speed</li>
<li>Call printDescription</li>
<li>Print the count of MotorVehicles on the road</li>
</ol>
</section>
<section>
<aside class='notes'>
Highlight that we now have 3 motor vehicles<br>
Trace through calling the parent's constructor and methods<br>
Car must be rolling down the street...someone forgot the emergency break
</aside>
<h3>The Code</h3>
<pre><code class='java'>
Car myCar = new Car();
myCar.switchEngineState();
System.out.println(myCar.getCurrentSpeed());
myCar.accelerate();
System.out.println(myCar.getCurrentSpeed());
myCar.printDescription();
System.out.println("Number of Vehicles on the road: " +
MotorVehicle.getCountCreated());
</code></pre>
<p>Output:</p>
<pre><code class='no-highlight'>
0
5
I'm a Motor Vehicle and my Engine is on true and I'm going 5 mph
Number of Vehicles on the road: 3
</code></pre>
</section>
<section>
<h3><span class='individually'>Individually</span></h3>
<p>Add accessor methods for:</p>
<ol>
<li>trunkOpen</li>
<li>numDoors</li>
</ol>
<p>Call the methods in the Tester</p>
</section>
<section>
<h3>Code</h3>
<pre><code class='java'>
public boolean getTrunkOpen() {
return trunkOpen;
}
public int getNumDoors() {
return numDoors;
}
</code></pre>
<pre><code class='java'>
System.out.println("number doors: " + myCar.getNumDoors());
System.out.println("the trunk is open: " + myCar.getTrunkOpen());
</code></pre>
</section>
</section>
<section>
<section>
<aside class='notes'>
Optionally have the @Override notation<br>
Potentially useful since it shows up in all my android projects a # of times..
</aside>
<h3>Overriding Methods</h3>
<p>Instance Methods:</p>
<p>An instance method in a subclass (child class) that has the same signature and return type as an instance method in the superclass <em>overrides</em> the superclass's method.</p>
</section>
<section>
<h3>Code It</h3>
<ol>
<li>In the Tester, call the printDescription() method for your car</li>
<li>Run it</li>
<li>Create a printDescription() method for your car class that says:<em>
I'm a car! I have (#) doors!</em></li>
</ol>
</section>
<section>
<aside class='notes'>
If we comment out the printDescription() code in Car, the code calls the printDescription() of the parent class<br>
uncomment and it uses the car<br>
Any questions?
</aside>
<h3>Overriding Methods Cont.</h3>
<p>The output of myCar.printDescription() now says: </p>
<pre><code class='no-highlight'>
I'm a car! I have 4 doors!
</code></pre>
</section>
</section>
<section>
<section>
<h3>Final Methods</h3>
<p>A Final method cannot be overridden in a subclass</p>
</section>
<section>
<h3>Test it</h3>
<p>Add the <em>final</em> keyword to the printDescription() method inside of the MotorVehicle class</p>
<pre><code class='java'>
public final void printDescription() {...}
</code></pre>
<p>The implication is that the subclass, Car, cannot override the printDescription()</p>
<p>Observe the compiler error</p>
</section>
</section>
<section>
<section>
<h3>A moment to ponder</h3>
<p>Right now we can accelerate and decelrate our car by the default value of "5"</p>
<p>What if we wanted to really slam on the pedal and take a 10 mph jump? Or 20??</p>
<p>We can create another accelerate/decelerate method that will take a parameter specifying how much faster or slower we want to go</p>
</section>
<section>
<h3>Method Overloading</h3>
<p>Doing so is called <em>Method Overloading</em></p>
<pre><code class='java'>
public void accelerate(int amount) {
currentSpeed = currentSpeed + amount;
}
</code></pre>
<p>Overloading is summed up by: Same name and different parameter list</p>
</section>
<section>
<h3>An Error?</h3>
<p><em>The field MotorVehicle.currentSpeed is not visible</em></p>
<p>We've seen something like this before...it has to do with those access modifiers</p>
</section>
</section>
<section>
<section>
<aside class='notes'>
Protected is also accessed by classes in the same package
</aside>
<h3>Fields</h3>
<p>Inherited Fields</p>
<p>A subclass can't access a parent's private members</p>
<p>There is a third access modifier called Protected</p>
<p>Protected fields can be accessed by subclasses</p>
</section>
<section>
<h3>Quick Detour</h3>
<p>Hop back to the MotorVehicle class and update your private member "currentSpeed" to protected.</p>
<p>Now that the error has gone, test out your new method: </p>
<pre><code class='java'>
myCar.accelerate(10);
System.out.println(myCar.getCurrentSpeed());
</code></pre>
</section>
<section>
<h3><span class='individually'>Individually</span></h3>
<p>Overload the decelerate method in the same fashion</p>
</section>
<section>
<h3>The Code</h3>
<pre><code class='java'>
public void decelerate(int amount) {
currentSpeed = currentSpeed - amount;
}
</code></pre>
</section>
</section>
<section>
<aside class='notes'>
Polymorphism can be a bit of an advanced topic since it's really covers a lot of ground with various types<br>
We want to mention it so they can say "ooooh yeah I've heard of that" not much to do with it in java
</aside>
<h3>Polymorphism</h3>
<p>Assuming a subclass has overloaded a method in the parent class:</p>
<p><em>"When an application needs the function, the particular version is dynamically determined at runtime"</em> - William Ford, William Topp Data Structures with C++ using STL</p>
</section>
<section>
<section>
<h3>Abstraction</h3>
<p>Using the <em>abstract</em> keyword when developing a class indicates that the class will never be instantiated</p>
<p>However, other classes that extend (inherit) the abstract class (if not abstract themselves) could be instantiated</p>
</section>
<section>
<aside class='notes'>
No<br>
You have cars<br>
Trucks<br>
Buses<br>
But nothing that's just a generic MotorVehicle
</aside>
<h3>Building an Abstract Class</h3>
<p>Abstract classes can contain partial implementations</p>
<p>Will you ever have just a <em>MotorVehicle</em> on the road?</p>
</section>
<section>
<h3>Code It!</h3>
<p>Convert our MotorVehicle class to an Abstract Class</p>
<p>Add the <em>abstract</em> keyword to the class declaration:</p>
<pre><code class='java'>
public abstract class MotorVehicle { ... }
</code></pre>
</section>
<section>
<h3>Updating our Tests</h3>
<p>Now that MotorVehicle is an abstract class, we can't create an instance of it</p>
<p>We have two choices:</p>
<ol>
<li>Comment out the code</li>
<li>Update the code to create instances of Cars</li>
</ol>
</section>
<section>
<aside class='notes'>
Flexibility -- MotorVehicle could be used as a parameter in methods and any MotorVehicle (regardless of subtype) could be passed when declared like this<br>
<br>
Polymorphism
</aside>
<h3>Updating Cont.</h3>
<p>Since car extends MotorVehicle we can:</p>
<pre><code class='java'>
MotorVehicle myVehicle = new Car();
MotorVehicle myVehicle2 = new Car();
</code></pre>
<p>Why would you want to do this?</p>
</section>
</section>
<section>
<aside class='notes'>
Hiding really only matters if you used the parent class type when declaring a new instace of a subclass<br>
i.e.<br>
MotorVehicle myOtherCar = new Car();<br>
<br>
If we call myOtherCar.staticMethod() it will look at the declared type "MotorVehicle" and call that static method
</aside>
<!--
<h3>Hiding Methods</h3>
<p>Class Methods:</p>
<p>If a subclass defines a class method with the same signature as a class method in the superclass, the method in the subclass hides the one in the superclass</p>
</section>
<section>
-->
<aside class='notes'>
Give them plenty of time to play around with creating their own subclass<br>
Encourage them to use the concepts discussed
</aside>
<h3>Code It!</h3>
<p><span class='individually'>On your own,</span> create a Bus that extends MotorVehicle</p>
<p>Additional Bus specific Methods to include: </p>
<ol>
<li>pickUpPassengers(int numer)</li>
<li>dropOffPassengers()</li>
</ol>
</section>
<section>