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