PPQ Flashcards
What does “private int a = 5” mean?
It means that a which is a type primitive int is only accessible within this class called…
What does the method signature “setA(int b) mean?
setA means it is a setter methods which allows other classes to set the value of a because it is a private variable.
The int b is the value that will be passed to the setA method. It will set the value of a to be that value b.
What is the toString() an example of?
toString() in this class is an example of overriding.
What is being created here. ExamQ2 ee = new ExamQ2(a)
An object of type ExamQ2 with a reference ee.
It is being passed a.
Discuss static methods and variables.
We can have static instance variables and static methods.
Both are associated with the class itself, not the object.
Therefore, statics can be accessed without instantiating the object.
Discuss a static variable
Like a global variable, but on a class by class basis. It is stored in the class. Static variables occur as a single copy in the class ( instance variables occur as multiple copies - one in each instance (object). eg., system.out is a static variable.
Discuss static methods.
Like a "global function" but on a class by class basis. There is no receiver because the static method is associated with the class, there is no object is associated with it, and therefore no receiver. You can think of it as the class being the receiver. Eg., System.arrayCopy()
Explain what makes GUI programs different to programs in which interaction is driven by a command line interface.
- User controls GUIs
- User can close the window and click buttons etc.
- Easy to use
- Consumes more memory & is slower
- User can’t really control command line actions
- Need to follow specific instructions on what happens & write it themselves.
- Difficult to use
- Faster & uses less memory
Eg., compare navigating a directory vs clicking on something
What is an interface?
- Interfaces are a collection of abstract methods with no implementations, constructors and don’t return anything. Can also define constant variables.
- Useful when you want to implement certain functionality, but there are no shared attributes.
- Any class can implement an interface as long as they include the methods from the interface.
- Any objects from a class that implements an interface can be stored in a reference of the type of the interface
- Java classes have single inheritance, interfaces have multiple inheritance
ie., shape interface, rectangle implements interface
Shape rectangle = new Rectangle (2,4)
What are the advantages of interfaces?
- A class can implement more than one interface (can only have one superclass)
- An interface can extends another interface or interfaces (more than one interface) .
- All methods are public & abstract
- Can make a reference of the interface that refers to the object of that class that is implementing it.
What are the advantages of interfaces to the programmer?
- Lightweight & simple mechanism
Programmers are able to respond to a common set of messages, without making the code too complex. This is a lot lighter than a sub class.
What are the disadvatnages of interfaces to the programmer?
- Only gives prototypes, not implementation code which means programmers need to create the code from scratch.
What is the difference between inheritance and interfaces?
- Java classes have single inheritance, interfaces have multiple inheritance.
- Class inheritance usually have shared attributes, but interfaces can be anything.
- Interfaces have only public methods and constant variables, unlike abstract classes.