Classes and stuff? Flashcards
What are three modifiers for accessing privileges?
Public,
Private,
Protected
What are the 2 ways to create objects?
Copy and adjust existing ones.
Instantiate a template
What way does Java create objects?
By instantiating a template
What is a class used for in Object-Orientated Programming?
A class is a template for buliding objects
What three things does a class have when being used in Object-Orientated Programming?
Attributes
Constructors
Methods
How do you create a new object in Java?
ObjectType name = new ObjectConstructor(args);
If an object bob has a private attribute age, can this attribute be accessed using bob.age;?
No, you need to use a getter method inside the class
If an object bob has a public attribute age, can this be accessed using bob.age;?
Yes, but this is bad practice to do, attributes should be private or protected
What is the difference between private and protected?
Private can only be used by that class, protected acts like private from a user’s perspective but can be used by subclasses
What 2 things happen when you instantiate an object with the keyword new?
The JVM allocates memory to store the new object.
A constructor method is called to initialise it.
What is the syntax for creating a constructor for a class called XClass?
public XClass(parameters) { //attribute assingments }
How do you call one constructor from within another?
You use the keyword ‘this’.
What does the keyword static do?
This means the variable belongs to the class rather than the instance and thus is the same for all objects of this class. Static methods can only access static variables.
What are some of the advantages of using inheritance in OOP?
- Introduces more abstraction
- Enhances code re-usability
- Improves code readability
- Can reduce software maintenance costs
- Allows multiple people to program subclasses simultaneously
How does inheritance work in OOP?
Subclasses inherit methods and attributes from the superclass and extend them (for instance to add more specific things)