Lecture Content Flashcards Preview

Java Programming > Lecture Content > Flashcards

Flashcards in Lecture Content Deck (43)
Loading flashcards...
1
Q

Skeleton (hierarchy) of a Class

A

/* Package declaration */

/* Import declarations */

class House {

/* Field declarations */

/* Constructors */

/* Methods */

2
Q

Why should an object should hide its implementation details?

A
  • To prevent misuse
  • To simplify code (hide stuff you dont need to see)
  • To insolute users from implementation changes
3
Q

Classes should present their users with an interface that

A
  • gives them access to well-specified behaviours
  • hides internal data representation
  • hides algorithmic details
  • preserves data integrity
4
Q

Encapsulation: Classes and Users

A

Classes should allow you to change the internal representation without affecting users

5
Q

A good problem solving strategy: Divide and Conquer

A

Understand problem, find tools, break into small problems.

  1. Try to understand or define the problem
  2. Choose the right tools for the job (and read the documentation)
  3. Break complex problems into smaller sub-problems
  4. Sub-problems can be broken again into yet smaller problems
  5. Simple enough problems can be solved directly
  6. Small solutions combine to solve bigger problems
6
Q

Java Strategy: Imaginary Methods

A

Typically, a solution to a problem of reading, manipulating or writing information is a method.

When writing one method, if you need to do a sub-task that is too complex to solve straight-away.. .

  1. Imagine there is a method that does that sub-task for you
  2. Give it a name, inputs, return type and meaning
  3. Then just use it as if it exists

Compile after each method

  • The compiler will tell you what new methods need defining.
7
Q

What to keep in mind when designing methods (even for imaginary):

A
  • Meaning of inputs (if any):
    • How to interpret the information passed in
  • Preconditions (if any):
    • Things which must be true as method is called
  • Postconditions (if object’s state changes):
    • Things guaranteed by end of method execution
  • Meaning of return value (if any):
    • How to interpret information returned
8
Q

!! variables constrained within some local scope.

A

• method input arguments…. . . within method definition

• locally defined variables…. . . until the end of method

this object . . .
. . . within an instance method

• variables initialised in a for-loop. . .. . . within the for-body

9
Q

due to local scope, within a method block can only directly access local parts of the stack, but….

A

. . . but ‘this’ allows you to access the owning object

. . .‘return’ variables allow information to escape the local scope

10
Q

instance methods

A

a method that is invoked on a specific object, requesting the object to do something, and maybe changing the object’s state on the way

x = myCircle.area();

11
Q

instance attributes/fields

A

myCircle.diameter;

12
Q

static methods

A

aka class methods

13
Q

static fields

A

aka class variables

Only one static object is created per class– not one per object

14
Q

Static Fields as Global Variables: Global object references

A

System.out is a global object reference

15
Q

static fields usually declared final or not?

A

yes, they don’t change

16
Q

how to make methods global

A

static can be used to make methods global

(static methods, like static fields, are underlined in UML)

17
Q

5 features of Arrays

A
  • Arrays are objects
  • Arrays can contain objects
  • Arrays can contain other arrays
  • All arrays have a length
  • Arrays of arrays needn’t be ’rectangular’
    • Not all rows need be the same size
18
Q

Consequences of pass-by-value:

A
  • If the method makes changes to the variable’s value, these changes are not visible to the calling code.
  • However, the variable could point to some shared memory (heap), and changes there will be visible

Method arguments are passed-by-value.

19
Q

Can a method alter the elements of an array without having to return anything (void)

A

yes.

20
Q

what is a recursive method?

A

A recursive method is one that includes a call to itself

21
Q

!! All recursive methods must have the following:

A
  • Base Case: knowing when to stop
  • Work toward Base Case: make the problem simpler (standard case), otherwise infiniute recursion
  • Recursive Call: method calls itself on a simpler problem
22
Q

good use case for recursive methods

A

If a problem can be reduced to smaller instances of thesame problem, then a recursive solution is likely to be easy to find and implement.

23
Q

A recursive method definition normally contains 2 kinds of cases:

A

◦ one or more base cases without recursive calls

◦ one or more cases that include at least one recursive call

24
Q

Abstraction

A

Abstraction is a process of hiding the implementation details and showing only functionality to the user.

25
Q

two ways to achieve abstraction

A
  • Abstract class (0 to 100%)
  • Interface (100%)
26
Q

!! abract classes (5 features)

A

abstract says:

  • The class is incomplete
  • You cannot directly create instances of this class (wil give compile error)
  • An abstract class must be declared with an abstract keyword.
  • It can have abstract and non-abstract methods.
  • It cannot be instantiated.
    • It needs to be extended and its method implemented.
  • It can have constructors and static methods also.
  • It can have final methods which will force the subclass not to change the body of the method.

abstract class A{}

27
Q

how does an interface look like in code

A
28
Q

what is an interface

A

An interface is a group of related method declarations with no bodies

  • These methods describe a set of capabilities
  • A class can implement an interface. If so, the classpromises to provide the capabilities specified by the interface
29
Q

what is the benefit of having an interface

A

Helps us manage complexity by treating members of various classes uniformly:

  • Methods common to all the classes are specified in the interface
  • Each class implements the interface
  • Client code can refer purely to the interface without
  • knowing (or caring) about details of the implementing classes

Duplication can be solved through inheritance.

30
Q

example of an implementation

A

clear?

31
Q

define inheritance

A

Inheritance is the process of deriving a specialised class from an existing class

32
Q

What does “A PhD student is-a-kind-of student.” mean?

A
  • Unlike other students, a PhD student: has a supervisor, must write a thesis, and does not take courses.
  • Like other students, a PhD student: must register, is in a department, and has a login.
33
Q

what generalisation and specialisation in inheritance

A

An electric car is-a car that runs with an electric engine

  • Car is the superclass or parent-class of ElectricCar
  • Car is a generalisation of ElectricCar
  • -
  • ElectricCar is a subclass or a derived-class of Car
  • ElectricCar is a specialisation of Car
34
Q

apparent and actual type

A

Similarly to case of interfaces:

  • The apparent type of an object is that which the compiler can infer at compile time: Lamp l = …
  • The actual type is the type the object really has: new AdjustableLamp(…)

Rules:

  • Apparent type must be a generalisation of the actual type
    • AdjustableLamp l = new Lamp(...); //Compile error
  • Methods and fields available according to apparent type
    • Lamp l = new AdjustableLamp(...); l.dim(); // Compile error
35
Q

binding

A

Binding is how Java knows which method to call on what object.

36
Q

protected methods and inheritance

A

protected methods and fields are both:

  • package visible
  • and can be accessed from subclasses
    • (even in different packages)
37
Q

super keyword

A
  • super refers to the immediate superclass
  • Can be used to call the constructor of a superclass
    • can only be called from subclass constructor
    • must be called on the first line!
    • different superclass constructors called with different inputs:
      • super(on); or super(); or super(arg1, arg2);
  • super() is called implicitly if no other constructor is called
  • Also, used to call an overridden method from a
    • superclass, e.g.super.pressSwitch();
38
Q

to avoid duplication…

A

…pull duplication into the superclass

39
Q

abstract classes and interfaces?

A

BUT the interface may be overkill and can be omitted in some cases, though having one does no harm.

40
Q

interface or abstract claass

A

The interface caters for the case where the abstract class may be too specific

41
Q

abstract methods

A
42
Q

Rules for use of abstract

A
43
Q

summary of abstract classes

A
  • Unlike classes, these cannot be instantiated
  • Like classes, they introduce types
    • but no object can have an abstract class as its actual type

Why use them?

  • When there is a set of common features and implementation for all derived classes but:
    • We want to prevent users from handling objects that are too generic
    • We cannot give a full implementation for the class