You are on page 1of 12

OO Programming Review

1 Java Programming and Applications


What is Class and Object?
 Class is a template for making objects
 It defines what variables and methods its objects possess
 These variables are called “data fields”

 Object is the main component we use for OO programming


 An object has its own data fields and methods
 Usually these methods use and manipulate the data fields in the same
object
 A program can be formed by defining different classes, generating
different objects
 Different objects perform different tasks in the system. So they
collaborate with each other to achieve the goal of an application.

2 Java Programming and Applications


What is Primitive and Reference Types?
 Primitive type refers to the 8 basic data types
 int, float, long, boolean, double, char… etc
 To represent more complex data structure, Java uses object

 Reference type refers to all kinds of object variables


 It is similar to a pointer, holding a reference/address to an
object
 By default, its value is null, if no object is referenced

3 Java Programming and Applications


What is Call Stack and Memory Heap?
 When a method call another method, the context
switches to a new set of variables. In order to replicate
the previous values when the method returns, a record
(all values in the context) is pushed/saved to Call Stack
for later use.
 The memory in Call Stack is allocated statically and will
be released when the record is popped/retrieved from
stack.
 In contrast to Call Stack (where you can find the local
variables), Memory Heap is used to store object. Memory
is dynamically allocated by the “new” operator for a
certain class.
4 Java Programming and Applications
What is Constructor?
 Constructor is a special method which run automatically
when an object is created
 It is mainly used for initializing data fields
 Constructor has the same name as the class.
 Constructor has no return value
 Constructor cannot be called using the method name.
 Constructor is executed when you “new” an object

5 Java Programming and Applications


What is Overloading?
 Because Java identify methods by their “method signature”
which consists of method name and argument list,
methods can share the same name as long as they have
different arguments.
 Due to this reason, when a method is invoked, the
compiler can determined which implementation is used
by matching the passed argument types.

6 Java Programming and Applications


What is Inheritance?
 Java allows a class extends the functionality of another
class. In this case, they are referred as parent and child.
 The child is able to inherit the features from parent,
which includes all non-private data fields and methods
 They are also called superclass and subclass
 It is possible to assign an object of the subclass type to a
reference variable of the superclass type. This is known as
upcasting and is always valid.

7 Java Programming and Applications


What is Overriding?
 An inherited method may not be useful in the subclass. So
the subclass is capable of rewriting the methods. This is
known as overriding.
 To override a method (inherited from superclass), the
subclass must define the same method of the same
signature.

8 Java Programming and Applications


What is this and super?
 A non-static method is able to reference to the data
fields that belongs to the same object. To do so, you can
use the keyword “this”. You can consider “this” as a
reference variable that always holding the reference to
the “current executing object”.
 Similarly, “super” can be used to reference the data fields
or methods from the superclass.
 “this” and “super” can also be used to call a local-
constructor and super-constructor respectively.

9 Java Programming and Applications


What is Constructor Chaining?
 Java forces the super-constructor to run before the sub-
constructor. As a result, when an object is created, its
super-constructor will be invoked, which in turn invokes
the super-super-constructor and so on. This phenomenon
is known as Constructor Chaining.

10 Java Programming and Applications


What is Data Encapsulation?
 Data Encapsulation is a technique that helps to isolate
data representation from direct access of user. The
purpose of this is to reduce the dependency on the data
representation and protect the integrity of the data.
 To achieve data encapsulation, you need to:
1. Set data fields to private
2. Provide public getter/setter methods

 The getter/setter methods provide an interface for the


user to use/access the internal data

11 Java Programming and Applications


What is Interface?
 Interface is a description of the required methods. So it
has a collection of “abstract” methods, which waiting
somebody to implement.
 In practical situation, we can consider the interface as a
contract that specify certain methods you will need to
use.
 Interface is very useful in system design, in which you very
often need to integrate a number of sub-modules built by
different people. To ensure the smoothness and
correctness of this process, you can use interfaces to
define what you exactly want.

12 Java Programming and Applications

You might also like