Any real-life entity/object has two parts:
- Properties (Describe its height, weight, width etc.)
- Functionality (Describes how it behaves)
Object-oriented programming removes some constraints of procedural programming language. It introduces:
- Modularity: Logic is modular
- Reusability: Code is reusable
- Extendability: Code is extendable
Object
An object is an entity which has
- Identity (Names)
- State (Values)
- Behavior (Performance) An object is an individual representation of a real-world entity.
Class
A class is a template that helps to describe common attributes and activities between various objects. Objects are just instances of a class (the template):
- Attributes (e.g. customer id, name, telephone no…)
- Activities (e.g. purchasing an item, etc.) Example of a real-world entity (Customer)
Definition of a class from different perspective:
- A class is a blueprint/real-world entity
- A class is a generalized representation
- A class is an encapsulation of properties and corresponding functionality A program is a set of instruction to solve a problem UML (Unified Modeling Language). A class diagram is a notation used to represent a class in a document. Class diagram is a kind of UML. UML is a language for visualizing, specifying, constructing and documenting the software.
Access modifier/specifies
(-) Private: Only can be accessed from inside the class where it is declared.
(+) public: Can be accessed from anywhere
It is recommended that in a class all the variable/properties should be private and all the method/functions should be public.
Object Oriented Programming
Five features of object-oriented programming:
- Classes and objects
- Abstractions
- Encapsulations
- Inheritance
- Polymorphism
Abstraction
Hiding unnecessary details and showing only necessary details. The concept of identifying the essential details to be known and ignoring the non-essential details from the perspective of an end user. For example, Method name and parameter need to be known; we need not know the whole process.
Encapsulations
Combining all elements into a capsule (E.g. : A class, which contains attributes and functions and all those attributes and functionality need not be exposed using access modifier we can achieve this). The concept of hiding internal details and providing a simple interface to the user.
Inheritance
Concepts of which allows defining generalized and specialized characteristic and behavior. The specialized entities automatically inherit all the properties and behavior of the generic one.
- All the generic details will be present in a parent class
- All the specific details will be present in a child class
- A child class will extend the parent to get the generic properties.
Polymorphism
If the same method behaves different ways in different situations
Object-oriented system development
3 main phases
- Object-oriented analysis (OOA)
- Object-oriented design (OOD)
- Object-oriented programing (OOP)
OOA
Analysis the problem structure and identifying the number and the name of the class
OOD
Elaborate class structure, what should contain (attribute and functionality)
OOP
The process of implementing the design using the object-oriented programing language
Member variables: Used for representing the state of an object/attribute of a class called member variable also called global variables.
Access modifier: Used to access or hide the attribute and the behavior of the class
The following syntax can be used to create an object of a customer class and refer is using a reference variable.
Customer custobject = new Customer();
custobject is the reference variable and new Customer()– is the object.
Java Architecture
JAVA architecture is composed of 3 components:
- JAVA programming language
- JAVA byte code
- JAVA virtual machine
.java -> .javac -> .class -> Interpreter (Win32) or Interpreter (Linux) or Interpreter (Mac)
JAVA is a byte code level platform independent
Platform independency depends on
- Source code level
- Byte code/ executable level
.cpp or c++ is a source code level that is platform independent, but at executable level, it is not. The only component in JAVA which is platform dependent is JVM (Java virtual machine)
JVM – Converts bytecode to corresponding machine level language and executes the instructions.
JDK – Create/develop JAVA in runtime
JRE – Runtime/ Kit just to run and compile or convert the code to bytecode
JVM – Interpreter/ Read line by line and convert byte to executable
During the execution of a program, the storage of a program and data happens as follows:
- All the java code stored in the memory is called code segment.
- All the global variables we declare for a class are stored in the data segment.
- All the variables which we declare inside the class and outside of all the functions are called member variables/ global variables.
- Life of a member variable depends on an object.
- The reference which points to a newly created object is called a reference variable.
- All the details of a class should be present in the { }
Private int a;Public void geta(){}
When we create an object it resides in the heap memory.
New keyword returns a newly created object.
The reference variable is simple variable use to access the object which is created in a heap memory.
Main is the starting point of execution of any Java program.