Академический Документы
Профессиональный Документы
Культура Документы
I. BASIC DEFINITION
1. What is an object?
2. How an object is constructed?
3. How objects interact with each other?
4. What happen during object creation?
5. Constructors
a. Properties:
b. Cannot
c. Can
d. Type
i. No-argument: formal parameters
ii. Default: created by compiler if no constructor exists
iii. Parameterized
Note: Implementing only parameterized constructor but create a class
with no-arg will throw error.
6. Method call? Its components?
7. Methods? Components
8. Quality of methods: small, testable, readable, do one thing and do it well
9. Overload
10. Overriding
11. Access Modiifier
1. Class 2. Package 3. Subclass 4. World
b. Public x x x x
c. Protected x x x
d. Private x x x
e. No modifier x
12. Super vs This
- Super: allows us to access attributes and behavior of super class in inheritance
relationship
Must be the first line if it is used in a constructor
- This: refer to the object typed as the current class
o Allows us to differentiate between class and formal parameters
13. Inheritance
a. Def:
b. Keyword
c. No limit to the number of inheritance level
d. Does it violate encapsulation?
Yes. Inheritance allows subclass to access to the members of base class. A
change such as adding a new method in the super class that is overridden in the
subclass may lead to potential security risk. For example, if the purpose of
encapsulation is to protect program from outside behaviors, calling a method of
a superclass that is overridden in subclass would cause the program to behave
differently.
e. Why multiple inheritance is not supported in java?
Java supports multiple inheritance but not through classes, it supports only
through its interfaces. The reason for not supporting multiple inheritance is to
avoid the conflict and complexity arises due to it and keep Java a Simple Object
Oriented Language. If we recall this in C++, there is a special case of multiple
inheritance (diamond problem) where you have a multiple inheritance with two
classes which have methods in conflicts. So, Java developers decided to avoid
such conflicts and didn’t allow multiple inheritance through classes at all.
14. Abstraction
a. Def: hiding unwanted implementation details from the user.
b. Happens during design when deciding on what to implement
c. Focus on what the object does instead of how it does it
Hiding the unwanted data and giving relevant data
d. Achieved through abstract classes and interfaces
e. Outer layout, used in terms of design
f. Benefits:
i. Provide a generalization that can be built upon more specific classes.
ii. Ties together common behavior for related objects
iii. Separate the user and the coder
g. Example: Coffee: abstract with attributes: milk., sugar, shots..
15. Encapsulation
a. Def: wrapping data(attributes) and methods(behavior) into a single unity
Hide the internal working of the class from other classes and only accessible
through a public API
b. Happens during implementation
c. Hide the implementation (how it does it)
Means hiding the internal details or mechanics of how an object does
something so that we can change later without affecting outside client under
the assumption that the API will not change
d. Achieved through access modifier ( public, private, protected) and getters and
setters
e. Focus on inner layout, used in term of implementation
f. Ex: inner implementation of a mobile home: how keypad button and screen are
connect with each other using circuits
16. Abstraction vs Encapsulation vs Data Hiding
a. Abstraction provides a layer over basic functionalities. It hides the
implementation details, let users focus on higher idea
Achieved through abstract classes and interfaces
b. Encapsulation groups codes together on basic of functions into a class
Achieved through set attributes to private and provide public getters and setters
c. Data hiding: hides the data affected by the implementation
It uses private and public to control access to instance variables
Achieved through abstraction and encapsulation.
Provide data security by assuring that someone cannot accidentally modify a
variable.
18. Interface
a. Contain method signatures
b. Methods are not defined
c. Static methods cannot be overridden
d. Define fields that can be accessed similarly to a static variable
e. Do not allow for constructor
f. Uses to group related methods with empty body
g. Signing a contract
h. Can implement multiple interfaces
i. Can extend another interface
19. Why programming to an interface?
20. Abstract class vs Interface
21. Composition
a. Represents as has-a relationship
i. A Car IS-A VEHICLE (inheritance: Car extends Vehicle)
ii. A Car HAS -An engine( composition: engine is an attribute of car)
b. Focus on what the object is comprised of instead of what it is
c. Strongest form of association
i. When the object that holds the comprising object is destroyed, those
objs are destroyed as well.
d. When to use Inheritance or Composition?
e. Composition over Inheritance
f. Achieve by interface
g.
22.
(Left) The Person class represents a composition relationship between the Heart and Hand class.
(Right)The City class represents an aggregation relationship between the Tree and Car classes,
which can exist without the City class.
23. Aggregation
a. Has – a relationship where the child obj can exist independently of the parent
object
b. Represented by a data field in the aggregating class
24. Association
a. When two object interacts, there is an association
b. 1 – many, many-1, many-many
c. Comprise of composition and aggregation
Aggregation
Association
Composition
Exception Error
Def Object program, that should not
catch because it may cover
up underlying problem
Type Checked/RuntimeE(unchecked) Unchecked
ex Checked: IO, FileNOtFound.. out of memory
Unchecked: NullPointer, Stackoverflow
IndexOutOfBound..