OOP Flashcards
ways to copy the values of one object into another in Java:
- By constructor
- By assigning the values of one object into another
- By clone () method of Object class
Is the mechanism of wrapping up of data and code action on the
methods together as a single unit.
Encapsulation
We can create classes inside a package and import it in our main class so that we can use that class in our program
true
It should start with the uppercase letter.
It should be an adjective such as
Runnable, Remote, ActionListener.
Use appropriate words, instead of
acronyms.
Interface
Usage of Java super Keyword:
- super can be used to refer immediate parent class instance variable.
- super can be used to invoke immediate parent class method.
- super() can be used to invoke immediate parent class constructor.
Rules for using of Static Variables:
- Variables or methods belong to class rather than to any particular instance.
- A static method cannot access a non-static variable of a class nor can directly invoke non-static method.
- Static members can be applied without creating or referencing an instance of the class.
- A static variable will be shared by all instances of that class which will result in only one copy.
means that this method doesn’t return any value.
void
Java follows camel-case syntax for naming the class, interface, method,
and variable.
If the name is combined with two words, the second word will start with
uppercase letter always such as actionPerformed(), firstName,
ActionEvent, ActionListener, etc.
CamelCase
is used to denote constants. It can be used with variables,
methods, and classes.
Final keyword
is a method of out that can be used to print a line.
To achieve encapsulation in Java:
- Declare the variables of a class as private.
- Provide public setter and getter methods to modify and view the variables values.
3 ways to initialize object:
Initialization through reference
Initialization through method
Initialization through a constructor
If the reference variable of Parent class refers to the object of Child class, it is known as
Upcasting
Importing Packages:
- Importing Specific Class
ex: import packagename.classname - Import Whole Package
ex: import packagename.*
uses variables to define data fields and methods to define
actions, and provides methods of a special type, known as
constructors, which are invoked to create a new object.
class
is a pre-defined class that Java provides us and it holds some
useful methods and variables.
System
is represented by data fields with their current values.
The state of an object
It should be a lowercase letter such as
java, lang.
If the name contains multiple words, it
should be separated by dots (.) such as
java.util, java.lang.
Package
means that you can run this method without creating an instance
of Main.
static
is a way of thinking about and structuring a
program’s functionality.
programming paradigm
Static keyword is unacceptable to the following:
- Class
- Constructor
- Local inner classes
- Inner class methods
- Instance variables
- Local Variables
- Interfaces
Rules in Creating Java Constructor:
- Constructor name must be the same as its class name
- A Constructor must have no explicit return type.
- A Java constructor cannot be abstract, static, final, and
synchronized.
means that anyone can access it.
public
specifies the accessibility or scope of a field,
method, constructor, or class.
access modifiers
two ways to overload the method in java:
Method Overloading: changing no. of arguments
Method Overloading: changing data type of arguments
is a block of code which only runs when it is called.
method
It should start with a lowercase letter
such as id, name.
It should not start with the special
characters like & (ampersand), $
(dollar), _ (underscore).
If the name contains multiple words,
start it with the lowercase letter
followed by an uppercase letter such as
firstName, lastName.
Avoid using one-character variables
such as x, y, z.
Variable
- The process by which objects can acquire the properties of
objects of other class. - Provides reusability, like adding additional features to an existing
class without modifying it.
Inheritance
is used to create an instance of the class. In other
words, it instantiates a class by allocating memory for a new object and
returning a reference to that memory.
New Keyword
Information can be passed to methods as parameter. act as
variables inside the method.
Parameters or Arguments
- Refers to the ability to process objects differently depending on
their data type or class. - The ability to redefine methods to derived classes.
- Request for an operation can be made without knowing which
specific method should be invoked. - A task performed into different ways.
Polymorphism
is an instance of a class.
object
Are variables declared within a class, it can be accessed within the whole class
Global Variables
- can be invoked without having an instance of the
class. - can be used to refer to the common property of all
objects
Static variable
are those types of methods that don’t require
implementation for its declaration.
Abstract methods
When a class inherits another class, it is known as
Single Inheritance
Advantages of Encapsulation in Java
-By providing only a setter or getter method, you can make the class read-only or write-only.
- It provides you the control over the data.
- It is a way to achieve data hiding in Java because other class will not be able to access the data through the private data members.
is a constructor that creates an object
using another object of the same Java class.
copy constructor
When type of the object is determined at run-time
Dynamic binding
If a class has multiple methods having same name but different in parameters, it is known as
Method Overloading.
is a reference variable which is used to refer
immediate parent class object.
super keyword
- belongs to the class rather than the object of a class.
- can be invoked without the need for creating an instance
of a class.
Java Static Method
Advantages of static variable
It makes your program memory efficient
can have constructors like the regular class.
abstract class
A variable which is created inside the class but outside the method is
known as an
Instance Variable
represents an entity in the real world that can be distinctly
identified.
object
The process of creating an Instance of a class so we can use it on our program.
Class Instantiation
This constructor is
implemented by default by the Java compiler if there is no explicit constructor
implemented by the user for the class.
Default Constructor
We can create multiple packages inside our java project, we can also reuse it in other projects if we choose to do so.
true
Different ways to create object:
New Keyword
New Instance
Clone Method
Deserialization
By Factory method
If a class has multiple methods having same name but different in
parameters, it is known as
Method Overloading.
When there is a chain of inheritance, it is known as
Multilevel Inheritance
- Refers to the class itself.
- Enables you to access global variables inside the class if you have
the same variable names in a parameter.
This Keyword
is a static variable within System that represents the output of your
program (stdout).
out
New Keyword:
- It is used to create the object.
- It allocates the memory at runtime.
- All objects occupy memory in the heap area.
- It invokes the object constructor.
- It requires a single, postfix argument to call the constructor
is a template, blueprint, or contract that defines what an object’s
data fields and methods will be.
class
It should start with the uppercase
letter.
It should be a noun such as Color,
Button, System, Thread, etc.
Use appropriate words, instead of
acronyms.
Class
Are variables declared inside a method, condition, loops and any other block of
code, it can only be accessible within that block of code.
Local Variables
is a technique of having more than one
constructor with different parameter lists.
Constructor overloading
designed to perform initializing
actions, such as initializing the data fields of objects.
constructors
has a unique identity, state, and behavior.
object
methods used to protect your data and make your
code more secure.
Getters and Setters
Usage of Method Overriding
- Method overriding is used to provide the specific implementation of a method which is already provided by its superclass.
- Method overriding is used for runtime polymorphism
- A constructor which has a specific number of parameters
- used to provide different values to
distinct objects.
parameterized constructor
- Class that is not used to create (instantiate) objects.
- Designed to act as a base (to be inherited by other class).
- Design concept in program development and provides a base upon
which other classes are built. - Can only specify members that should be implemented by all
inheriting classes. - It is the process of hiding the implementation details from the user.
- Only the functionality will be shown to the user to reduce the
complexity and increase efficiency.
Abstraction
is an object-oriented programming technique that combines data
and instructions for processing that data into an object that can be used within
the program.
Object-Oriented Programming System(OOP)
A class in Java can contain:
- Fields
- Methods
- Constructors
- Blocks
- Nested class and interface
Types of Inheritance in Java:
Single Inheritance
Multilevel Inheritance
It is a full Abstract Class that implemented in other classes.
Interfaces
acts as a non-access modifier in Java that is used
mainly to manage memory.
Static Keyword
is a process in which a call to an overridden method is resolved at runtime rather than compile-time.
Runtime polymorphism
It should start with lowercase letter.
It should be a verb such as main(),
print(), println().
If the name contains multiple words,
start it with a lowercase letter followed
by an uppercase letter such as
actionPerformed().
Method
Advantage of Method:
- Code Reusability
- Code Optimization
When type of the object is determined at compiled time
compiler doesn’t decide the method to be called.
Static binding
is a group of similar types of classes, interfaces and sub-
packages.
Java Package
It should be in uppercase letters such
as RED, YELLOW.
If the name contains multiple words, it
should be separated by an
underscore(_) such as MAX_PRIORITY.
It may contain digits but not as the first
letter.
Constant
It is a rule to follow as you decide what to name your identifiers such as
class, package, variable, constant, method, etc.
Java Naming Convention
If subclass (child class) has the same method as declared in the parent class, it is known as
method overriding