Object Oriented Programming Flashcards
How to automate the creation of Getter and Setter?
IntelliJ –> Code –> Generate
Constructors
for you to initialize the object that you are creating and do whatever else you wanna do at the time the object is being created.
- a special method that is used to initialize objects
- it is called when an object of a class is created
- it can be used to set initial values for object attributes
- it must have the same name as the class within which it is defined
Constructor Chaining
- calling one constructor from another constructor
- it can be done using this() keyword for constructors in the same class
- you can also use super() keyword to call constructors from the base class
Encapsulation
- refers to the bundling of data, along with the methods that operate on the data, into a single unit
- may also refer to a mechanism of restricting the direct access to some components of an object
- many programming languages use encapsulation frequently in the form of classes
What are the benefits of encapsulation?
- Data hiding
- Increased Flexibility
- Reusability (Easy to reuse)
- Testing code is easy
Reference variable
- reference type is used to point objects/values
- classes, interfaces, arrays, enumerators, and annotations are reference types in Java.
- reference variable can also store null values. By default, if no object is passed to a reference variable then it will store a null value
- You can access object members using a reference variable and dot syntax
Class
xxx
Object / Instance
xxx
super keyword
- used to access/call the parent class members (variables and methods)
- can’t be used in static areas (the static block or a static method)
- Any attempt to do so will lead to compile-time errors
- is commonly used with method overriding
this keyword
- used to access/call the current class members (variables and methods)
- this is required when we have a parameter with the same name as an instance variable (field)
- can’t be used in static areas (the static block or a static method)
- Any attempt to do so will lead to compile-time errors
- commonly used with constructors and setters
Method Overriding
- if subclass (child class) has the same method as declared in the parent class
- in other words, if a subclass provides the specific implementation of the method that has been declared by one of its parent classes, it is known as method overriding.
Rules for Java Method Overriding
- the method must have the same name as in the parent class
- the method must have the same parameter as in the parent class
- there must be an IS-A relationship (inheritance)
recursive call
the method would call itself forever until the memory is full
What are the differences between a static method and an instance method?
Static Methods
- static methods are methods in Java that can be called without actually creating an object of that class
- it becomes part of the class (a class method: a method that belongs to a class rather than to an object)
- they are designed with the aim to be shared with all objects created within that class
- static methods can’t be overridden but can be overloaded
Instance Methods
- instance methods are methods that require an object of its class to be created before it can be called
- instance methods belong to the object of the class and not the class
- they can be overridden and overloaded
How to pass objects as arguments?
code example
public double distance(Point obj) { int x = obj.getX(); int y = obj.getY(); return Math.sqrt((this.x - x) * (this.x - x) + (this.y - y) * (this.y - y)); }
Inheritance
- one class is allowed to inherit the features (fields and methods) of another class
- the keyword used for inheritance is extends
- the class whose features are inherited is known as superclass (base class or parent class)
- the class that inherits the other class is known as a subclass ( or a derived class, extended class, or child class)
- the subclass can add its own fields and methods in addition to the superclass fields and methods
Composition
- a way to design or implement the “has-a” relationship
- the relationship is possible when one object contains another object and that object is fully dependent on it
- in composition, we use an instance variable that refers to another object
- to ensure the code reusability in the program
Polymorphism
- same name, different behavior
- the ability of an object to take on many forms
- it allows us to perform a single action in different behavior in different situations
- the most common use of it in OOP occurs when a parent class reference is used to refer to a child class object
What are the four basic concepts of OOP?
- Inheritance
- Abstraction
- Polymorphism
- Encapsulation
getSimpleName()
- the method returns the simple name of this class in the form of String
- used to get the simple name of this class, as given in the source code
static field vs. non-static field
static field
- a static field belongs to the class
- dont’ need an instance of that class to access its static field
non-static field
- non-static fields are located in the instances of the class.
- each instance of the class can have its own values for these fields
- you need to have an instance of the class from which you can access it