A class is a Template for creating multiple objects. A class defines state and behavior that an object can exhibit.
Object is an instance of class. We can create multiple objects using the same class.
class vs object
Class is a template. Object is the instance created by the template.
Different ways of creating object in Java
- Use the new keyword: The most common should be using the new keyword to create a new instance of class
- Use New Instance: If we know the name of the class and the class has a public default constructor, we can use Class.forName to load the class. Then use the newInstace method to create an object
- Clone method
Encapsulation, Abstraction, Inheritance, Polymorphism
- Encapsulation is a process of binding or wrapping the data and the codes that operates on the data into a single entity. This keeps the data safe from outside interface and misuse.
- Implementation: access modifiers for data like private, protected and operation methods like get and set.
- Abstraction is the concept of hiding irrelevant details. In other words make complex system simple by hiding the unnecessary detail from the user.
- implementation: Abstract class and Interface.
- One class is allow to inherit the features(fields and methods) of another class.
- Same code but have different behavior.
- Example: Animal, Cat, Dog. Run()
Default(No Access Modifiers)
only within the same package (No subclass in different package)
!not for class and interface
only in the same class(No subclass, no same package)
all class in the same package, subclass in different package
- static variables
- static method
- static block
- final variable
- final method
- final class
An abstract class can never be instantiated. If a class is declared as abstract then the sole purpose is for the class to be extended.
The synchronized keyword used to indicate that a method can be accessed by only one thread at a time. The synchronized modifier can be applied with any of the four access level modifiers.
An instance variable is marked transient to indicate the JVM to skip the particular variable when serializing the object containing it.
This modifier is included in the statement that creates the variable, preceding the class or data type of the variable.
The volatile modifier is used to let the JVM know that a thread accessing the variable must always merge its own private copy of the variable with the master copy in the memory.
Accessing a volatile variable synchronizes all the cached copied of the variables in the main memory. Volatile can only be applied to instance variables, which are of type object or private. A volatile object reference can be null.
- The sub class and parent class have the same variable and method. super.variable or method name to get the method or variable in the parent class
- super() call constructor of parent class. Must put in the first line.
- get the method or variable of this class
- create the instance of this class for return or use as parameter in method or argument in constructor.
Association, Composition, Aggregation
Association is relation between two separate classes which establishes through their Objects. Association can be one-to-one, one-to-many, many-to-one, many-to-many.
In Object-Oriented programming, an Object communicates to other Object to use functionality and services provided by that object. Composition and Aggregation are the two forms of association.
It is a special form of Association where:
- It is a special form of Association where:
- It is a unidirectional association i.e. a one way relationship. For example, department can have students but vice versa is not possible and thus unidirectional in nature.
- In Aggregation, both the entries can survive individually which means ending one entity will not effect the other entity
Composition is a restricted form of Aggregation in which two entities are highly dependent on each other.
- It represents part-of relationship.
- In composition, both the entities are dependent on each other.
- When there is a composition between two entities, the composed object cannot exist without the other entity.
Aggregation VS Composition
- Dependency: Aggregation implies a relationship where the child can exist independently of the parent. For example, Bank and Employee, delete the Bank and the Employee still exist. whereas Composition implies a relationship where the child cannot exist independent of the parent. Example: Human and heart, heart don’t exist separate to a Human
- Type of Relationship: Aggregation relation is “has-a” and composition is “part-of” relation.
- Type of association: Composition is a strong Association whereas Aggregation is a weak Association.