-
Notifications
You must be signed in to change notification settings - Fork 0
/
oop2.html
176 lines (153 loc) · 9.04 KB
/
oop2.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
<!DOCTYPE html>
<html lang="en">
<head>
<title>OOP: Inheritance</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">Advanced OOP: Inheritance, Polymorphism, Abstract Classes, and Interfaces</h1>
<div class="text">
<h2>Inheritance</h2>
<h3>What is Inheritance?</h3>
<p>Inheritance is a key component of OOP that allows us to create a new class from an existing class. This is for the purpose of increased code reuse, so there is no need to recode applications. Note that all Java classes automatically inherit from the Object class. Inheritance is done using the keyword <span class="textCode">className extends superclassName</span> in the class declaration.</p>
<h3><br>Class Hierarchy</h3>
<p>The existing class is known as the superclass or parent class. The new class that is created is known as the subclass, child class, or the derived. A subclass will inherit all the public member data its parent class, and additional properties unique to the subclass may be added. Private methods of a superclass are not callable from the subclass. In Java, a class can only extend one superclass, but more than one subclass can be derived from one superclass.<br><br>
A subclass is defined as having an is-a relationship with its superclass. A has-a relationship means a class has a data field. Any class higher on the hierarchy is a ancestor class, and any class lower on the hierarchy is known as a descendant class.</p>
<img src="images/hierarchy.png" width="70%" height="auto">
<h3><br>Overriding</h3>
<p>When a method in the subclass has the same name as a method in the parent class, the method in the subclass can override the method in the superclass by redefining the method (reworking what it does).</p>
<h3><br>Super Keyword</h3>
<p>This is used to call the method of the parent class from the child class. If a superclass constructor is used in the subclass, Java differentiates between the different constructors based on the number of parameters.</p>
<h3><br>"this" Keyword</h3>
<p>This is used to refer to the current class. If there is ambiguity between classes, the keyword can help do differentiate between class attributes or parameters with the same name.<br>Uses:</p>
<ol>
<li>As a constructor:</li>
<ul>
<li>Used if a class has 2 or more constructors</li>
<li>Works much like super keyword</li>
<li>Ex. this()</li>
</ul>
<li>As a variable:</li>
<ul>
<li>Used when a variable name is shared across classes (ex. x or y)</li>
<li>Ex. this.variableName</li>
</ul>
<li>As a method:</li>
<ul>
<li>Used when a method name is shared across classes</li>
<li>Ex. this.methodName()</li>
</ul>
<li>As a parameter:</li>
<ul>
<li>Used to passing a reference to the current object</li>
<li>Ex. method(this, arguments)</li>
</ul>
</ol>
<p class="underline">Example:</p>
<div class="code">
<pre>
public class Student{
public Student(){
//implementation
}
}
public class GradStudent extends Student{
public GradStudent(){
super();
}
public GradStudent(parameters){
this(); //uses GradeStudent() constructor
//implementation
}
}
</pre>
</div>
<h3><br>Encapsulation</h3>
<p>Encapsulation is the process of hiding private instance variables and methods from other classes. Variables from another class are accessed and modifies using get and set methods. Set (accessor) methods allow another class to view data stored in an instance variable. Get (mutator) methods allow another class to change data stored in an instance variable.<p>
<h3><br>Rules with Inheritance</h3>
<ul>
<li>If the superclass constructor is used it must be the first line in the constructor</li>
<li>No argument (default) constructor must always be defined in the superclass to avoid errors</li>
<li>Instance variables are to always be declared as private so that they are not accessible by the subclass directly. To access or change them, one needs to use set/get methods</li>
</ul>
<h2><br>Polymorphism</h2>
<p>Polymorphism means many forms. It is the ability of a class to provide different implementations of a method, depending on the type of object that is passed.</p>
<p class="underline">Example:</p>
<div class="code">
<pre>
//works because everyone “is a” Student
Student s = new Student();
Student g = new GradStudent();
Student u = new UnderGrad();
//does not work as not every student is a GradStudent
</pre>
</div>
<h3><br>Static and Dynamic Binding</h3>
<p>Binding is determining the memory addresses for jumps. Static binding is done at compile time, and dynamic is done at run time. An example is when a derived class object call a method in their parent class which calls a method that is overridden in the derived class. The parent class is compiled separately and before the derived classes are even written, and so the compiler cannot possibly know which address to use. Therefore, the address must be determined (bound) at run time.</p>
<h3><br>Polymorphism Example</h3>
<p>Taking the Student class hierarchy from earlier, Student and GradStudent have their own calculateAverage() method that is overriden. Consider the following objects:</p>
<div class="code">
<pre>
Student g = new GradStudent();
g.calculateAverage();
</pre>
</div>
<p>While g is declared as student, it will find the correct calculateAverage() method to use (the one of GradeStudent). Java makes a match based on the actual data type of the actual object not the object reference. This happens during run time and thus is considered late binding.</p>
<h3><br>Downcasting</h3>
<p>Downcasting is used in polymorphism to label an object as a subclass instance.</p>
<p class="underline">Example:</p>
<div class="code">
<pre>
//Student class does not have getID() method but GradeStudent does
Student s = new GradStudent();
GradStudent g = new GradStudent();
int a = s.getID(); //compile error-Student has no getID()
int b = g.getID(); //legal
//downcasting
int a = ((GradStudent) s).getID();
</pre>
</div>
<h2><br>Abstract Classes</h2>
<p>An abstract class is a superclass that represents an abstract concept and should not be instantiated. It will have abstract methods which have no implementation. A class with one or more abstract methods must be declared abstract. An abstract class can have a constructor, which may be used from a subclass. When extending an abstract class, the subclass must provide implementation for the abstract class.</p>
<p class="underline">Example:</p>
<div class="code">
<pre>
public abstract class Music{
public int getTempo();
}
</pre>
</div>
<h2><br>Interfaces</h2>
<p>An interface is a completely abstrct superclass. To extend an interface, use the keyword <span class="textCode">implements</span>. A class can implement multiple interfaces, and polymorphism applies to objects disguised as interface types. Again, when implementing an interface, the subclass must provide implementation for all methods.</p>
<p class="underline">Example:</p>
<div class="code">
<pre>
public interface Music{
public int getTempo();
}
public class Jazz implements Music{
public int getTempo(){
return 10;
}
}
</pre>
</div>
<h2><br>Common Mistakes</h2>
<ol>
<li>Declaring a class as <span class="textCode">superclassName extends subclassName</span></li>
<li>Not putting the <span class="textCode">super()</span> constructor on the first line</li>
<li>Not using <span class="textCode">super.</span> when trying to access a overriden superclass method</li>
<li>Creating a subclass object with type superclass</li>
<li>Calling a method that does not exist with polymorphism</li>
<li>Forgetting to provide implementation for abstract methods</li>
<li>Extending an interface (instead of implements)</li>
</ol>
</div>
<footer id="footer"></footer>
</body>
</html>