Comp200 Exam Prep
Comp200 Exam Prep
A subclass can access all the private members (fields and methods) of its superclass.:
FALSE
*when you assign a superclass to a subclass type it will give you compile-time error!*
*The protected modifier allows a member to be accessed in a subclass from a
different package: TRUE
A class can implement multiple interfaces with conflicting default methods as long as
the class overrides the conflicting method and specifies which implementation to
use: TRUE
CLASSA class is a blueprint that defines the state (variables) and the behaviour (methods) of something that
you want to model. Allows for: Abstraction, Encapsulation, Information Hiding
Overloaded methods and constructors let you use the same method name (or constructor) but with different
argument lists.
Overloaded methods let you reuse the same method name in a class, but with different arguments (and
optionally, a different return type).
RULES:
Overloaded methods must change the argument list.
Overloaded methods can change the return type.
Overloaded methods can change the access modifier.
A method can be overloaded in the same class or in a subclass.
Referred to as “compile-time polymorphism”
OVERRIDEN METHOD:
Overriding lets you redefine a method in a subclass, when you need new subclass-specific behaviour
RULES:
The argument list must exactly match that of the overridden method.
The return type must exactly match that of the overridden method.
The access level must not be more restrictive than that of the overridden method.
The access level can be less restrictive than that of the overridden method.
Referred to as “run-time polymorphism”
INSTANTIATING CLASSES:
OR!
METHOD-LOCAL INNER CLASS MyInner mi = new MyInner(); //instantiate outside inner class but within
method
a static nested class does NOT have access to the instance variables and methods
of the outer class.
INTERFACES:
An interface cannot implement any methods, whereas an abstract class can.
A class can implement many interfaces but can have only one superclass.
An interface is not part of the class hierarchy.
Unrelated classes can implement the same interface.
Describe two (2) benefits of using anonymous classes for event handling in
Java [4]
Enhanced Encapsulation: Anonymous classes help in encapsulating the event handling logic within
the scope of the component that triggers the event.
Concise and Localized Code: Anonymous classes enable you to define and instantiate event
handlers in a single, succinct block of code, directly within the context where the event handling is
needed. This makes the code more readable and maintainable because the event handling logic is
placed right next to the code that triggers the event.
For example, when adding an action listener to a button, you can define the listener immediately:
Describe briefly (in not more than one or two sentences) the following concepts
used in object-oriented programming:
(a) encapsulation Encapsulation is the practice of bundling the data (attributes) and methods (functions)
that operate on the data into a single unit or class, and restricting access to some of the object's components to
protect the object's internal state from unintended interference and misuse.
(b) inheritance Inheritance is a mechanism where a new class (subclass or derived class) inherits the
properties and behaviors (fields and methods) of an existing class (superclass or base class), allowing for code
reuse and the creation of hierarchical relationships between classes.
(c) polymorphism Polymorphism is the ability of objects of different classes related by inheritance to
respond to the same method call in a way that is specific to their own class, enabling a single interface to
represent different underlying forms (data types).
(d) abstract methods Abstract methods are methods declared in an abstract class or interface that do not
have an implementation and must be overridden by subclasses or implementing classes, forcing them to
provide specific behavior for the method.
(e) interfaces (specifically in Java) Interfaces in Java are abstract types that define a set of method
signatures without implementing them, allowing classes to implement multiple interfaces and thereby support
multiple inheritance of behavior.
Aggregation: is a relationship where one class (the whole) contains references to one or more objects of
another class (the parts), but the lifecycle of the parts is independent of the whole. In other words, the parts
can exist independently of the whole. This is often described as a "has-a" relationship. For example, a library
has books, but the books can exist independently of the library.
Composition: is a stronger form of aggregation where the whole class owns the parts, and the lifecycle of the
parts is dependent on the whole. If the whole object is destroyed, the parts are also destroyed. This represents
a "contains-a" relationship with strong ownership. For example, a house contains rooms, and if the house is
demolished, the rooms no longer exist.
Inheritance: is a relationship where a new class (derived or subclass) is based on an existing class (base or
superclass), inheriting its attributes and methods. This allows the derived class to reuse and extend the
functionality of the base class. It represents an "is-a" relationship. For example, a car is a vehicle, meaning a car
class can inherit from a vehicle class.
Explain the purpose of Java’s layout managers in GUI programming and briefly describe how any one such
layout manager operates.
FlowLayout: FlowLayout is one of the simplest layout managers in Java. It arranges components in
a left-to-right flow, much like lines of text in a paragraph. When the container's edge is reached,
FlowLayout wraps the components to the next line.
1. Component Order: Components are added to the container in the order they are specified.
2. Alignment: Components can be aligned to the left, center, or right of the container.
3. Wrapping: If the horizontal space is insufficient to fit all components in a single row,
FlowLayout wraps the components to a new line below.
4. Spacing: FlowLayout allows setting horizontal and vertical gaps between components.
BorderLayout
Description: BorderLayout divides the container into five regions: North, South, East, West, and
Center. Each region can contain only one component, and components added to these regions are
resized to fit the region.
How it operates:
North and South: These regions extend horizontally across the container, at the top and
bottom, respectively.
East and West: These regions extend vertically along the left and right edges of the container.
Center: This region takes up all the remaining space in the center.
GridLayout
Description: GridLayout arranges components in a grid of cells with equal size, each cell containing
one component. The grid is defined by a specified number of rows and columns.
How it operates:
It does any drawing in the super class of the panel in which it is located. In this case,
we could actually leave it out and the code would still work. But it’s very bad practice
to do so, since it makes our code vulnerable to bugs if and when it gets changed. For
example, if we wanted to make a background colour for the panel, we might do this in
the constructor. That’s actually what I did in the solution to Q4 (see Test2.java). This
is drawn by super.paintComponent(). Leaving it out would then mean the background
isn’t drawn.
The ClickListener class extends the MouseAdapter class to handle mouse click
events. A separate class could instead have been written to implement the
MouseListener interface. Explain what the purpose of the MouseAdapter class is,
and why it is used here in preference to a separately written class which
implements the MouseListener interface. (2)
Overriding lets you redefine a method in a subclass, when you need new subclass-specific behavior.
The argument list and the return type must exactly match that of the overridden method. The access
level must not be more restrictive than that of the overridden method.
Polymorphism A method is polymorphic if the action performed by the method depends on the
actual type of the object to which the method is applied. Different objects can respond to the
same message in different ways.
generic programming Generic PROGRAMMING refers to writing code that will work for many
types of data for example sort routines.
static methods Are per-class methods. They are associated with the class and not objects. They
cannot reference instance variables or methods.
State and explain TWO differences between interfaces and abstract classes.
• Abstract classes may contain implemented and abstract methods; while interfaces may only contain
unimplemented or abstract methods.
• Abstract classes may have instance fields (variables) while all variables in an interface are implicitly final
(constants).
• All methods and fields in an interface are implicitly public while methods in an abstract class may be private...
anonymous inner class Anonymous inner classes are local inner classes that have no name. Anonymous
classes can be defined as expressions and instantiated on the fly. An instance of a local (or an anonymous) class
has an enclosing instance associated with it, if the local (or anonymous) class is declared in a non-static context.
composition Composition: (’has-a’ relationship between two classes. One class contains references to objects
of other classes e.g. a A database class contains instances of CD objects.)
State the difference between checked and unchecked exceptions. Is NegativeValueExcepion a checked or
unchecked exception? 4
Checked exceptions describe a problem that is likely to occur at times, no matter how careful you are not
your fault. They are normally due to external failures beyond your control, such as deleted file, server crashing,
network outage, etc. ClassNotFoundException, IOException, EOFException, FileNotFoundException,
UnknownHostException. The compiler insists you handle checked exceptions those that you cannot prevent
Unchecked exceptions fall under the RunTimeException class and are due the fault of the programmer,
RunTimeException, ArrayIndex- OutOfBoundsException, ArithmeticException, NullPointerException,
ClassCastException. You are not compelled to handle checked exceptions.
The class Cat is a subclass of the class Mammal. Explain by means of suitable code
segments when and where the special Java variables this and super will be required
within the subclass. [4 ]
this: is a reference to the current object. Can be used to access members of the class e.g. this.x.
Useful for unambiguous referencing.
Super is a reference to the super class. Used to call super class constructors (super()) or super class
methods and variables super.x().
The application uses an inner class for the button event handling. An anonymous
inner class may be used instead. Write code that adds a button to the interface that,
when clicked, changes the color of the rectangle to yellow. Use an anonymous
class for the event handling. JButton yellow = new JButton(" Yellow ");
yellow.addActionListener(new ActionListener (){
class ButtonHandler implements ActionListener{ public void actionPerformed(ActionEvent e){
public void actionPerformed(ActionEvent e){ current = Color.yellow;
current = Color.green; repaint();
repaint(); }
} });
} add(yellow);