Lecture 2 and 3 objects Flashcards

1
Q

What do objects do?

A

Objects allow us to bring together related variables and methods into a single thing.
eg a Scanner object.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What are objects defined by?

A

Objects are defined by a class. Classes do not always need to include the main method. In general classes have:

1) a name (starting with a capital letter)
2) some variables (known as class attributes)
3) methods
4) some special methods, called constructors

If you don’t create a constructor, the system will create one for you with no arguments.

Each class is defined in its own specific file

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How do we use a class that we create?

A

We have to create another class to call it

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How do you create a new object? What is the general format if a class already exists?

A

= new (input1, input2);

input1 and input2 are the arguments passed to the constructor.

From there, you can call methods within the class by:
.();

You can change the value of the attributes in a class, from another class, by:

. = ;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How can you tell if a method is a constructor by looking at it?
There are two main tell-tales.

A

The name of the constructor is the same as the name of the class and there is no return type.

e.g. public Circle(double r){
radius = r;
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What does “private” mean?

A

These classes, attributes, and methods can only be used by the class in which they are defined.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What does “protected” mean when defining a class?

A

These classes, attributes and methods can be used only by other classes in the same package or that inherit from this class

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What are getter and setter methods?

A

These are public methods that allow other programmers to access the class and change variables, when the class attributes are private.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Why is it a convention to make the class attributes private?

A

The person who created the class has control over other people accessing and modifying the variables.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Can you generate getters and setters in Eclipse?

A

In eclipse, you can do:

Source –> generate getters and setters

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Why do we use “this”?

e.g this.radius = radius

A

Often the arguments to a method are named the same as the class attributes, but these are not the same thing. If we say this.radius, we are saying that the radius is the class attribute (not the input argument to our constructor). Using “this” avoids ambiguity.

Similar to “Self” in Python.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What does static mean?

A

Static is an optional keyword that can be used to create variables or methods that will exist independently of any instances created for the class.

Therefore, they can be accessed without creating an instance of the class (i.e. an object).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What’s another word for decorator?

A

modifier

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Why is the main method static?

A

It involves creating objects from other classes and isn’t applied to an object itself.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is the reference of an object?

A

You can think of references like the “address” of a “house”

We can have many references going to the same object.
A single reference can only refer to one object.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What does it mean when methods have been overloaded?

A

They have the same name but different arguments.

Can be anything.
Eg., System out println()
or it could just be a method name used more than once with different arguments

17
Q

what are the two types of classes?

A

Objects and static

18
Q

When we create a new object, we also create a reference to that object. Is it the same when we create variables of a primitive type?

A

No, when we create a variable to a primitive type, we’re just creating a single value. An object has got lots of values associated with it (e.g. name and age) so we just store the reference (i.e. the “address”)

19
Q

What happens if we test for equality of two references that are:

a) the same object
b) multiple objects with the same values for their attributes

A

a) Equality is true if they refer to the same object.

b) Equality is false if multiple objects with the same values for their attributes

20
Q

What is meant by the equals(Object o) method?

A

All objects have a method to check if an object equals another object.

21
Q

How can we inherit a class from another existing class, so that we automatically inherit everything and can add extra attributes and methods?

A
We can use extends in the class declaration, e.g.
public class B extends A (){
super();
}

B has all the methods and attributes that A has but a different toString method.

We need to change the attributes from private to protected in the A class.

22
Q

Can immutable objects be changed once created?

A

No.

All wrapper classes including String, Integer etc are all immutable.

23
Q

Are Strings objects? What happens when we check for equality of string a and string b:

String a = “hello”
String b = “hel” + “lo”

A

They are not equal as they are different objects.

24
Q

what’s the most common way of drawing classes (in particular the hierarchy of super classes and subclasses)?

A

UML diagrams

25
Q

What’s an example of an immutable object?

A

A String is immutable. For example, if we have
String b = “Hel”;
a=b;
b+=”lo”;
You think that we are just changing the string b, but in fact we are creating a new object. a still points to “Hel” the original object.

26
Q

What are abstract classes?

A

Abstract classes are classes that you can’t make objects from.

27
Q

What are interfaces?

A

Interfaces define a set of methods. Any class that implements the interface must have these methods in it.

In Java, any class can only extend one other class but your class can implement as many interfaces as you like.

This allows other bits of code to know formally what you’re going to do

28
Q

What is polymorphism?

A

We can store an object in a reference for its parent class. Useful when we get to arrays.

29
Q

What does instanceof do?

A

instanceof checks whether an instance is of a specific type or not

30
Q

What is the Object class?

A

Every single class inherits from the Object class.

31
Q

How do we use equals(Object o)?

A

Since we inherit the equals(Object o) method from the Object class, we don’t need to pass in a type.

1) check that the Object equals the class that you are in
2) If so, we need to create a new reference and cast the class type to o
3) From there we can use methods etc in the class

32
Q

How does Java know to use a toString() method when trying to print out an object?

A

System.out.println() is part of Print Stream object

33
Q

In the Book class with two class attributes int code and String name, what would a copy constructor be?

A

Add this to the Book class:

public Book(Book b){
code = b.code;
name = b.name;
}

In the main class, suppose you create a new book:

Book b1 = new Book(35, "pooh bear");
Book b2= new Book(b1); //copy constructor

Depending on the equals method, they could be equal

34
Q

If we have

String a = “hel”
a = “hel” + “lo”

Does the original “hel” disappear?

A

Yes, the original object disappears usually with the garbage collector (which comes round periodically)

35
Q

Can a private attribute be inherited by the new class?

A

Yes, but we can’t access it unless it’s changed to “protected”

36
Q

Are there any arguments for the super class?

A

No. The default constructor of the super class doesn’t have any

37
Q

Describe immutable objects in Java?

A

Once immutable objects are created, its object values and state can not be changed.

For example, primitive objects such as int, long, float, double, all legacy classes, Wrapper class, String class, etc.

It only supports get() method to pass the value of the object.

38
Q

Give an example of a method that can be overridden?

A

A common example is a toString method.