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:
OOP principles
so far
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{ // Inheritancepublic 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
3.Constructor: helps to instantiate the class with instances (helps create objects)
Instance variables vs local variables
Local variables:
Instance variables:
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:
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
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:
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:
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
Can you run a code before main method in Java?
-Yes, we can have static blocks which runs before main method
“this” keyword
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