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)
How is a class declared as a subclass of a superclass?
modifiers class SubClass extends SuperClass { //class body}
Can you call a superclass constructor from within the subclass?
Yes, you use the keyword ‘super’. It must be the first thing in the constructor method.
What is overwriting/overriding?
When you redeclare a method from the superclass in the subclass. The version in the subclass is the version that will be used.
What types of methods/attributes will a subclass inherit (private/public/protected)?
Subclasses will only inherit public and protected attributes and methods.
Anything private are not inherited
What is an abstract class?
An abstract class is a class which can be extended to a subclass but not instantiated by itself.
How do you declare an abstract class?
public abstract class ClassName { //class body}
What is the naming convention for interfaces?
____able
Do abstract methods have the method body written when declared?
No, the method body is created when the subclass overrides the method (which it must do for abstract methods)
What does an interface contain?
Method signatures.
The bodies are written in clases implementing the interfaces.
They can also contain attributes for constants.