COMP0004-Notes(2): Principles of Java Programming

8 篇文章 0 订阅

Lecture Notes

Abstraction

Less Distinctions, More Commonality!

  • separate the essential from full detail
  • selective, simplified view

Representation

Relationships

  • association between classes, how are different classes related

Partitioning

  • Dividing an app into manageable chunks
  • encapsulation
  • hides information (private) inside the scope of a class
  • provide an architecture

Encapsulation

  • return an object by creating a new copy and return only the reference of the copy, instead of the reference stored in the private instance variable! (This is really different to the idea of in-place in Algorithms)
  • Don’t make attributes public, as this allows potential malicious use, and don’t return the original reference for the same reason! Otherwise, an external client can change an attribute to an illegal value without me actually knowing it, thus can potentially crash the program.
  • Keep dependencies simple, so that changing the implementation of one method doesn’t require dozens of changes to other parts that calls the method in the code.
  • Encapsulation also allows the same interface being used, while its implementation can be totally renovated.

In summary:

  1. Instance variables - Private;
  2. Instance methods - only a minimal amount of Public. That is, to have only a minimal amount of public interfaces;
  3. Limit a variable (name) to a minimal scope.

Inheritance

A class is an extension, a subclass of a parent class. The child class (subclass) shares the properties and methods (i.e. all characteristics) from the parent class (superclass).
e.g. Human is a child class of Creature class.

Polymorphism

A subclass has all the properties of the superclass, but can add more;
Thus, any message accepted by the superclass is also accepted by the subclass.

  • Superclass is generalisation
  • Subclass is specialisation

e.g.
Superclass: Shape
Subclass: Triangle, Circle, Square…
All the subclasses have their unique behaviour and attributes etc, but all share the common behaviours, attributes and interfaces defined in the superclass Shape.

In a inheritance hierarchy, the leave (sub-most) classes are the most concrete ones which objects are created from, all the other more abstract superclasses only provide implementations and an incomplete image.

Some OO Concepts

Objects

  • State: All the data the object currently encapsulates / all the values currently stored in instance variables
  • Identity: A primary key or simply a unique reference to object, that makes it to distinguish objects in an unambiguous way, independent from its state, which can change and be of the same value among different objects.

Classes

Blueprint which specifies a particular kind of object.

Instances & Instantiation

Instance: An object which belongs to a specific class.
Instantiation: The process of creating an instance.

  • At instantiation, the instance should be initialised to a well-defined known state. This is achieved by constructors

Attributes (Instance Variables)

Operations (Instance Methods)

Enables an object to act & react
Receives messages and act upon them
Reaction to a message usually depends on the object’s current state.

Messages (Method Calls)

A message sent to an object to request it to perform certain operations or provide a service.
Consists of:

  1. Selector: Message name
  2. (Optional) Arguments

Set of messages which an object can understand is usually defined in its interface.
A message is dynamically bound to an interface…
“dynamic” means “at runtime”. This means that the question of which specific method is to be invoked is determined by the message sent when the program is run (at runtime).

– in contrast to “dynamic binding” is “static binding”… This means the ways in which private methods that interact with each other are already defined “at compile time”. Meaning that the interaction is fixed.

Interfaces (Methods)

Can be:

  • Public: defines the messages that can be accepted from other objects
  • Private: can only be used by the object itself

An object can conform to a pre-defined public interface, accepting pre-defined messages.

Relationships Between Classes

Association

A subset of various possible relationships:

  • One to One: A Lecturer has an Office;
  • One to Many: A Lecturer teaches many Students;
  • Many to One: many Students attend Lectures.

Composition & Aggregation

The relationship of Ownership. Classes have some other classes. Based on quantifiers, we divide the Ownership into Composition & Aggregation.

Composition

Strong Ownership.
e.g. A Tire is part of a Car and one Car only. A Tire object CANNOT be part of more than one Cars at the same time.

Aggregation

Weak Ownership.
e.g. A Student is part of a Course, but not necessarily of this one Course only. A Student object CAN be part of more than one Course.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值