Skip to content

Instantly share code, notes, and snippets.

@Kev-in123
Last active January 12, 2022 21:47
Show Gist options
  • Save Kev-in123/b73d84867e070c393250c3aa13ba604a to your computer and use it in GitHub Desktop.
Save Kev-in123/b73d84867e070c393250c3aa13ba604a to your computer and use it in GitHub Desktop.
Quiz: OOP
Access Control:
private - can only be accessed in the class
protected - can only be accessed in the class or a subclass
public - can be accessed anywhere
Modifiers:
abstract - a non-access modifier, used for classes and methods
final - a non-access modifier used for classes, attributes and methods (not changeable)
finally - executes no matter if the try-except failed or passed
native - specifies that a method is not implemented in the same java source file
private - can only be accessed in the class
protected - can only be accessed in the class or a subclass
public - can be accessed anywhere
static - is a non-access modifier used for methods and attributes
transient - specifies that an attribute is not part of an object's persistent state
synchronized - specifies that methods can only be accessed by one thread at a time
volatile - attribute is not cached thread-locally
strictfp - restrict the precision and rounding of floating-point calculations
Class functions:
abstract - a non-access modifier, used for classes and methods
assert - for debugging
class - used to create a class
extends - extends a class (indicates that a class is inherited from another class)
enum - declares an enumerated type
implements - is used to implement an interface
import - used to import a package, class or interface
instanceof - checks whether an object is an instance of a specific class or an interface
new - creates new objects
package - creates a package
return - what a method returns
interface - used to declare a special type of class that only contains abstract methods
this - the way you reference the class object
throws - what error may be thrown
void - specifies that a method should not have a return value
super - refers to superclass (parent) objects
OOP stuff
OOP stands for Object-Oriented Programming
OOP gives the code a clear structure
Classes and objects are the two main aspects of OOP
A class is a template for objects, and an object is an instance of a class
Classes consist of attributes and methods
Attributes define what a class is and methods define what a class does
Constructors are methods that are called when the class is initialized
The constructor name must match the class name, and it cannot have a return type
Like normal methods, constructors can have parameters
The Class Object itself is referenced by the special attribute `this`
Static methods & attributes:
A static method is a class method can be called directly with
[insert class name here].[insert method name here]() without first creating an object
A static method must not refer to `this` Therefore, it cannot refer to any class attributes
A static attribute, similarly, is referenced by [insert class name here].[insert attribute name here] without first creating an object instance
Enums:
An enum is a special "class" that represents a group of constants
Inner Classes:
In Java, it is also possible to nest classes
Abstract classes & methods:
Data abstraction is the process of hiding certain details and showing only essential information to the user.
Abstraction can be achieved with either abstract classes or interfaces
Abstract class:
- a restricted class that cannot be used to create objects
Abstract method:
- can only be used in an abstract class, and it does not have a body. The body is provided by the subclass
Interface:
Another way to achieve abstraction in Java, is with interfaces
An interface is a completely "abstract class" that is used to group related methods with empty bodies
To access the interface methods, the interface must be "implemented" by another class with the `implements` keyword
Concepts of Object-Oriented Programming
Inheritance:
Inheritance is when a class inherits from another class
In inheritance, you have 2 classes
You have the subclass (child class)
and the superclass (parent class)
To inherit a class, you use the `extends` keyword
Polymorphism:
Polymorphism occurs when we have many classes used in inheritance
In the subclass, you can override the method in the superclass
Encapsulation:
Encapsulation is where you hide certain data from the users
This is done with private variables
Since you cannot view private variables outside of the class, you create getter and setter methods to view/modify these variables
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment