An object-oriented program is structured as a community of interacting agents, called objects
Scenario
I (stays at Hyderabad) wants to send a flower bouquet to my friend, who is at Chennai
Objects
Each object has a role to play
Each object provides a service, or performs an action, that is used by other members of the community
Messages & Methods
Actions are initiated in object-oriented programming by the transmission of a message to an agent (an object) responsible for the action
The message encodes the request for an action
It is accompanied by any additional information (arguments) needed to carry out the request
The receiver is the object to whom the message is sent
In response to a message, the receiver will perform some method to satisfy the request
Responsibilities
A fundamental concept in object-oriented programming is to describe the behavior in terms of responsibilities
Object
It is the basic unit of Object Oriented Programming and it represents the real life entities
Objects have two characteristics: they all have state and behavior
Examples of Objects
student
teacher
fan
dog
pen
State
It is represented by attributes of an object
Behavior
It is represented by methods of an object
Identity
It gives a unique name to an object
Class
A class is a user defined blueprint or prototype (template) from which objects are created
It represents the set of properties or methods that are common to all objects of one type
It has definitions of methods and data
Data Hiding & Encapsulation
Using private access modifiers(access specifiers), we can hide data from other classes
Binding data and its operations together into a single unit is known as encapsulation
A java class is the example of encapsulation
Abstraction
Hiding internal details (implementation details) and showing functionality is known as abstraction
In Java, we use abstract class and interface to achieve abstraction
Inheritance
Inheritance in Java is a mechanism in which one object acquires all the properties and behaviors of a parent object
Existing class: Super Class / Base Class / Parent Class
New class: Sub Class / Derived Class / Child Class
The idea behind inheritance in Java is that you can create new classes that are built upon existing classes
When you inherit from an existing class, you can reuse methods and fields of the parent class
Moreover, you can add new methods and fields in your current class also
Inheritance represents the IS-A relationship which is also known as a parent-child relationship
Inheritance Examples
Triangle IS-A Shape
Rectangle IS-A Shape
Why use inheritance in java
For Method Overriding (so runtime polymorphism can be achieved)
For Code Reusability
Polymorphism
Polymorphism is the ability of an object (methods) to take on many forms
Generally it occurs when we have many classes that are related to each other by inheritance
In Java, we use method overloading and method overriding to achieve polymorphism
Java Buzzwords/Features
Simple
Object Oriented
Platform Independent
Compiled & Interpreted but High performance
Architecture-neutral
Robust
Multithreaded
Distributed
Secure
Simple
Java is easy to learn and its syntax is quite simple, clean and easy to understand
Java inherits the C/C++ syntax and many of the object-oriented features of C++
Java has removed many complicated and rarely-used features, for example, explicit pointers, operator overloading, etc.
Object Oriented
In java everything is an Object which has some data and behavior
Un like C++, in java even the function main() has to be part of some class only
We can't have any method outside of the class
Java can be easily extended as it is based on Object Model
Platform Independent
C/CPP compiler produces ".exe" file which is interpreted by OS and the instructions in the ".exe" file are specific to OS(ie platform dependent)
Java Compiler produces ".class" file (byte code), which is interpreted on any platform with corresponding JVM
The instructions in the ".class" file (byte code) is platform independent
JVM(Java Virtual Machine) is a engine that enable the computer to run java programs
JVM is the java interpreter, which converts java byte code into machine language
It starts the java program execution by calling main() method
It is part of JRE(Java Runtime Environment)
Compiled & Interpreted but High performance
Java is compiled and interpreted language
Java Compiler generates '.class' file (Byte code)
Java Interpreter interprets this byte code, converts into an executable file (exe file)
Most previous attempts at cross-platform solutions have done so at the expense of performance
Java byte code was carefully designed so that it would be easy to translate directly into native machine code for very high performance by using a just-in-time compiler
Architecture-neutral
Java is "write once run anywhere anytime" language
Operating system upgrades, processor upgrades, and changes in any core system resources does not make a program to malfunction
Robust
Java uses strong memory management
There is no pointers, avoids security problems
Manages memory allocation and de allocation (In fact, de allocation is completely automatic, because Java provides garbage collection for unused objects)
Java makes an effort to eliminate error-prone situations by emphasizing mainly on compile time error checking and runtime checking(exception handling & type checking)
Multithreaded
With Java's multithreaded feature it is possible to write programs that can perform many tasks simultaneously
Multithreading is a special type of Multitasking
Multitasking
Running multiple tasks simultaneously
Each task is a separate program
If we run programs sequentially one after the other, if the currently running program requires an I/O, the processor will be idle until the i/o operation is completed
The aim of multi tasking is to reduce this idle time of the processor
With the help of CPU Scheduling algorithms(Example- Round Robin), we can assign processor another job when the currently running program requires an i/o
Multitasking Advantages
Throughput(no.of jobs that can be completed in a unit time) will be increased
Quick response time for jobs(i.e no job needs to wait for a long time for the processor)
Thread
Part of a program
Sequence of instructions, which has independent path of execution
Multithreading
Running multiple threads of a program simultaneously
Distributed
Java is distributed because it facilitates users to create distributed applications in Java
RMI (Remote Method Invocation) and EJB (Enterprise Java Bean) are used for creating distributed applications
This feature of Java makes us able to access resources(objects/files) by calling the methods from any machine on the internet
Secure
No explicit pointer
Java Programs run inside a virtual machine sandbox. Java Security Manager determines what resources a java class can access such as reading and writing to the local disk
Primitive Data Types
boolean
byte
short
int
long
float
double
char
Variables
Variables are containers for storing data values
The value depends on the data type of the variable
The value can be altered during program exam execution
There are three types of variables in java: local, instance and static
Syntax: datatype variable_name[=default_value]
Variable Declaration
int x=50
Expressions
Any unit of code that can be evaluated to a value is an expression