ECM2414 Nested Loops Flashcards
OOP benefits
- Protected private and public variables in created objects (Encapsulation)
- (Inheritance)
- Organises code into classes and objects
- (Polymorphism)
Issue with encapsulation
- Classes in OOP should usually be specialised, should only include code relating to that type’s specific function
- With two tightly coupled classes
○ Rely heavily on the members in each class
○ Lots of work involved in creating functions to access those members
○ Occurs more frequently in GUI programming
Nested classes overcome this issue by allowing classes to be defined within classes
Static nested class
does not have access to instance members
inner class
does not have access to instance members
Instance members
refers to variables that are specific to that specific object
What modifiers can be used to declare a standard outer class (top-level class)
public
package private (the default/implicit)
modifiers for nested classes
public
private
protected
package private
protected modifier
an access modifier that limits the visibility of functions and variables to a specific class and its derived classes
package private
other members of the same package have access to the item. package-private is the default access modifier and does not have a keyword, because package is used to specify the package for a class or interface.
Why use nested classes
Loosely linked classes located in one place
- Links can be made explicit between classes that are linked to only one other class by making it a nested class
Increases encapsulation on occasion (hiding data for private purposes, you are not able to take access of every data at any time in your code, which is safe)
It makes code more readable (classes pertaining to a top level class are all found under that top level class, looking stately)
Nested classes will produce two class files on compilation
Take the example of a top-level class OuterClass and its nested class InnerClass. What would the class files be named?
OuterClass.class
OuterClass$InnerClass.class
How can static nested classes be accessed?
using the enclosing class name
e.g. OuterClass.InnerClass
from within the class however, you can construct the nested class directly with just ‘InnerClass’
“this”
refers to the inner type instance
OuterClass.this refers to the instance of the surrounding (outer) class, when within the inner class code blocks
In a similar vain to how regular public variables are accessed within a class, not just nested classes
Local classes
Local classes are defined and exist solely within a block (usually a method)
Local class declaration cannot contain public, protected, private or static access modifiers
Cannot access method local variables (unless passed as arguments) unless they are declared as final
Can access all the members of the outer class instance like standard inner class members
External access to the class cannot be made
explicit finals initialised outside the class within the block can be accessed
Anonymous classes
Potentially one of the most convoluted aspects of java
Classes that are declared without any class name at all (hence the name anonymous)
Not only may these be defined within a method, they may also be defined within an argument to a method
Uses the ‘new’ keyword, then uses curly bracers to extend the base implementation
When you implement an interface, there is no constructor, so you use an empty pair of parentheses