Java Flashcards
Learn Java concepts and patterns
Class
An immutable blueprint for an object with methods and fields (variables). Like a struct in C. Building blocks for components
Object
A stateful and unique instantiation of a class which are stateful and active, so they are and can do something
Instantiation
Creating new objects using constructors and the ‘new’ keyword
Constructor method
Usually comes withtin the class and has the same name as the class. Can take parameters
Keyword: new
Constructor used when creating a new instance of a class
Keyword: private
An access modifier for attributes, methods and constructors making them only accessible within the declared class via getters and setters. Cannont be directly accessed
Keyword: public
An access modifier for attributes, methods and constructors making them visible and accessible to other classes (Globally) . They can also be modified unless they are declared final
Keyword: final
Anything declared final cannont be modified, overriden and not extended if it is a class
Keyword: static
Elements declared as static are associated to a class, not actual object instances e.g. Main. They are shared between all instances of that class and can be accessed without instantiation.
Attribute
Things like variables within a class. Each object has it’s own copies of attributes and can be plain data types like int or char. Capture what objects can be
Methods
Capture what objects can do. Take parameters and return values. Main way to communicate with an object
References
Like standing in for the object itself (e.g. SingleMove move = new SingleMove. move would be the reference). Really just placeholders that signify the presence of an object. If an object is no longer referenced it is slated for garbage collection. References to objects are given particular types
Null Refernces
References that point to nothing and can cause errors when called in the program
Overloading
When two or more methods have the same name but with different parameters in the same class
Overriding
When a method in a subclass (child) has the same method as a parent class. When the subclass needs a different implementation of the method to the parent class
Keyword: this
Provides a reference to the current object whose method is being executed. Cannot be used in a static element but can be used in a method or constructor
Inheritance
When a new child class ‘extends’ a parent class and inherits all the features from the parent class (Attributes, Methods, etc.) but you can add or adapt features so the new class does what you want
Usage of inheritance
Child classes can provide new or alter old functionality by: adding extra attributes/methods or overriding existing methods. Renders parent classes more re-useable as they are extendable and adaptable
Sub-class
A sub-type that supports both inheritance (recives parent features for free) and polymorphism (features of sub class can be used in place of a feature of a class)
Polymorphism
Every reference belongs to a class, but a reference can be made to any object of a sub-class of the references class which does not change the reference’s class. This is the priciple that arises from the fact that one reference can refer to various different classes. It lets you use an object of a sub-class as if it was an object of some super class
Class hierachy
A class is derived from one direct super class only. The root of the class hierarchy is the class object. Every class in Java, directly or indirectly, inherits from the class object
Single dynamic dispatch
When an overridden method is called via a reference, the actual method to execute is selected based on the type of the object referenced, not the reference type. Since the decision cannot be made at compile time, dynamic dispatch refers to the choice of code execution as resolved at runtime. The method to execute is chosen based on the availability of an implementation nearest to the reciver class upwards in the hierarchy. Parameters are treated as static
Keyword: abstract
If a class is abstract, we cannot make instances of it. Often these classes are purely conceptual without instances e.g. shape. Abstract methods are declared but don’t have any implementation (any non-abstract sub-class is forced to implement all these methods). A class with one or more abstract method must also be declared abstract
Benefits of inheritance
DRY code, code may be maintained reliably, faciltitates polymorphism