OOP - Object & Class Flashcards
Class
DEFINE
it is a template where many instances (objects) can be instantiated
A.K.A. blueprint & prototype
Object
DEFINE
It is an instance of class (object of a class)
HOW TO CREATE A CLASS
SYNTAX: -class keyword helps us to create a new class -class in Java can only be public or default -it CANNOT be protected or private accessModifier class ClassName{ } class Apple{ //class members }
HOW TO CREATE AN OBJECT
SYNTAX:
-new keyword is MUST to use to create an object
-without new keyword, you cannot create a new object
-new keyword is always used together with contructor to create a new object
ClassName variableName = new ConstructorName();
Apple apple1 = new Apple();
Apple apple2 = new Apple();
Scanner scan = new Scanner(System.in);
Constructors:
- Helps us create objects of a class
- It is always used next to “new” keyword (helps to instantiate an object)
- it can be default, or 1 arg, or more args depending on the need
- Once you create your own constructor, Java will no longer provide the default.
- it can be overloaded similar to methods
- Syntax is similar to methods except it doesn’t have a return type or void type
- It should always be named as the ClassName
- Whenever it is invoked it runs a block of code
OOP principles
so far
- Software is created to resolve real life problems
- Programming languages can be used to create software
- Object - oriented is simply known as an approach to programming that breaks a programming problem into objects
- Different Java objects can interact with one another
Method Overloading and Overriding
-Method overloading happens in the same class and it is known as having multiple methods with the same name but different arguments. Constructors can also be overloaded with different arguments similar to methods.
-Method overriding happens in the child class and this is known as child class implementing a different method body for the parent class method.
“extends” keyword
-Creates child parent relationship
public class Car{ public void honks(){ System.out.println("This car honks"); } } public class Mercedes extends Car{ // Inheritance
public void honks(){
System.out.println(“Mercedes honks”);
}
}
Mercedes becomes a child class Car becomes a parent to Mercedes
Class Members
1. Instance variables: defines the attributes of the instance These are declared in the class level
- Methods/functions: defines the behaviors of the instances
- void methods
- return type methods
3.Constructor: helps to instantiate the class with instances (helps create objects)
- Blocks: used to initialize static and non-static instance variables
- instance blocks
- static blocks
Instance variables vs local variables
Local variables:
- They are variables that are declared in: main, if-else, switch, loops, methods, etc.
- Their scope is restricted to the statements or methods they are declared in
Instance variables:
- They are declared in the class level and belongs to the instances of that class.
- Their scope is wider than local variables and determined by access modifiers (public, protected, default, and private)
Final and static keywords
Non-Access Modifiers
Static instance variables: -static instance variables means the variable belongs to the class and hence can be invoked with class name
public static int noOfAnimals = 0;
-non-static instance variables belong to the objects and they can be called with an object name
public int noOfAnimals = 0;
Final instance variables:
- They must be initialized meaning you must assign them some values
- This default value that you initialized CANNOT BE CHANGED anywhere
public final int noOfAnimals = 0;
NOTE; One instance variable can be final and static at the same time known as CONSTANTS examples: pi = 3.14 daysInAWeek = 7 monthsInAYear = 12
public static final int noOfAnimals = 0;
Blocks
- Local blocks: Switch, if-else, loops, methods, constructors
Class Members 2. Instance blocks: used to initialize non-static instance variables { hasEyes = true; } 3. Static blocks: used to initialize static instance variables static{ hasEyes = true; }
NOTE: static instance variables can be initialized in the non-static blocks as well.
NOTE: non-static instance variables can only be initialized in the non-static blocks
Can you run a code before main method in Java?
Yes, we can have static blocks which can run before main method
HOW TO CREATE A CLASS
SYNTAX:
- class keyword helps us to create a new class
- class in Java can only be public or default
- it CANNOT be protected or private
accessModifier class ClassName{ }
class Apple{ //class members }
Instance Variables
OTHER NAMES
attributes - fields - instance variables - states
-WHAT DOES THE OBJECT HAVE-
defines the attributes of the instance
declared in the class level
Methods
OTHER NAMES
methods - functions - behaviors
-WHAT DOES THE OBJECT DO-
drives(), stops(), signals(), brakes(), accelerates(), honks()
HOW TO CREATE AN OBJECT
SYNTAX:
- new keyword is MUST to use to create an object
- without new keyword, you cannot create a new object
- “new” keyword is always used together with constructor to create a new object
ClassName variableName = new ConstructorName();
Apple apple1 = new Apple(); Apple apple2 = new Apple();
Java is an OOP Language
Object-Oriented Programming
Java has an Object class that is the parent to any class by default
The core concept of the object-oriented approach is to break complex problems into smaller
objects.
So, Object-oriented simply known as an approach to programming that breaks a
programming problem into objects
Constant
Instance variable that is final and static at the same time
WHEN DO STATIC AND INSTANCE BLOCKS INVOKE?
Once you create an object both static and instance blocks will be executed.
NOTE:
Static block will be executed once and first.
Instance block will be executed for each object being created
EXECUTION ORDER
SAME CLASS DIFFERENT CLASS
- static block 1. main method (top to bottom)
- main method (top to bottom) 2. static block will be executed
- Instance block, for each object 3. Instance block, for each object
Can you run a code before main method in Java?
-Yes, we can have static blocks which runs before main method
“this” keyword
- this keyword is useful when we have instance variable and method parameter with same name
- this keyword is a reference to the current object of its type
returnType
It specifies what type of value a method returns.
For example, if a method has anintreturn type then it returns an integer value
If the method does not return a value, its return type isvoid
methodName
It is an identifierthat is used to refer to the method in a program, could be any name but should be meaningful to increase readability of your code
method body
It includes the programming statements that are used to perform some tasks. The method body is enclosed inside the curly braces{ }
method parameters
Arguments that we pass inside the method parentheses
Methods are functions or behaviors of the class
FACT
An instance is a single occurrence of a class
FACT