GGGG
GGGG
of Technology,
Department of Software Engineering
2
Outline
§ Overview of OOP?
§ Why Java?
§ The JVM and Byte Code
§ Basic concepts of OOP
§ Classes
§ Objects
§ Members
§ Class Member Visibility
§ Encapsulation
§ Inheritance
§ Abstraction
§ Polymorphism
3
Object Oriented Programming (OOP)
Ø Object-Oriented Programming: it is a computer programming
design philosophy or methodology that organizes/ models software
design around data, or objects rather than functions and logic.
4
What is Java ?
Ø high-level, general-purpose, object-oriented, and secure
programming language developed by James Gosling at Sun
Microsystems
Ø AWT (Abstract Window Toolkit) and Swing are used in Java for creating
standalone applications which are .
Mobile Application
Ø An application which is created for mobile devices is called a mobile
application. Currently, Android and Java ME (java mobile edition) are
used for creating mobile applications.
7
Java Applications
Web Application
q An application that runs on the server side and creates a dynamic page
q Servlet, JSP, Struts, Spring, Hibernate, JSF, etc. technologies are used for
creating web applications in Java.
Enterprise Application
8
Features of Java
9
Features of Java
q Simple: Java is very easy to learn, and its syntax is simple, clean and easy
to understand.
q Robust: Java makes an effort to check error at run time and compile time.
It uses a strong memory management system called garbage collector.
Exception handling and garbage collection features make it strong.
q Secure: it has no explicit pointer and programs runs in the virtual machine.
Java contains a security manager that defines the access of Java classes.
10
Features of Java
q Platform-Independent: Java provides a guarantee that code writes once
and run anywhere. This bytecode is platform-independent and can be run
on any machine.
11
Features of Java
Ø Portable: Java Bytecode can be carried to any platform. No
implementation-dependent features.
Ø High Performance: Java enables high performance with the use of the
Just-In-Time compiler.
Ø JDK refers to Software development environment used for making applets and
Java applications.
Ø JDK helps them to code and run Java programs. It is possible to install more than
one JDK version on the same computer.
Ø JDK contains tools required to write Java programs and JRE to execute them.
Ø Java application launcher opens JRE, loads the necessary class, and executes its
main method. 13
Components of Java Programming Language
Java Virtual Machine (JVM):
Ø Once you run a Java program, you can run on any platform and save lots of time.
Ø JVM comes with JIT (Just-in-Time) compiler that converts Java source code into
low-level machine language. Hence, it runs faster than a regular application.
14
Components of Java Programming Language
Java Runtime Environment (JRE)
Ø piece of software that is designed to run other software. It contains the class
libraries, loader class, and JVM.
Ø In simple terms, if you want to run a Java program, you need JRE.
Ø JRE contains class libraries, JVM, and other supporting files. It does not include
any tool for Java development like a debugger, compiler, etc.
Ø It uses important package classes like math, swing, util, lang, awt, and runtime
libraries.
15
JVM and Byte Code
Ø Java bytecode is the instruction set for the Java Virtual Machine.
Ø Java bytecode is generated as soon as a java program is compiled.
Ø Java bytecode is the machine code in the form of a .class file.
Ø With the help of java bytecode we achieve platform independence in java.
Ø JVM runs the bytecode without considering a processor.
16
Basic concepts of OOP: Object
Ø Object refers to an instance of a class.
Ø When you send a message to an object, you are asking the object to invoke
or execute one of its methods as defined in the class.
17
Basic concepts of OOP: Class
Ø a blueprint or a set of instructions to build a specific type of object.
Ø Java determines how an object will behave and what the object will
contain.
Syntax of Class in Java
Ø It is a collective representation
class <class_name>{
Of many items.
list of field;
list of method;
}
18
Basic concepts of OOP
Difference between Object and Class in Java
19
Basic concepts of OOP
Difference between Object and Class in Java
Class – Dogs
Data members – size, age, color, breed, etc.
Methods– eat, sleep, sit and run.
20
Basic concepts of OOP
Object Vs. Class
Object Class
blueprint or template from which objects are
instance of a class
created
22
Basic concepts of OOP: Inheritance
Ø one object acquires the properties and behaviors of another object.
Ø creating a parent-child relationship between two classes.
Ø offers robust and natural mechanism for organizing and structure of any
software.
Ø The concept allows us to inherit or acquire the properties of an existing
class (parent class) into a newly created class (child class).
Ø It provides code reusability.
23
Basic concepts of OOP: Polymorphism
Ø the ability of a variable, object or function to take on multiple forms.
Ø Polymorphism (Poly + morphs many + forms)
Ø allows us to create methods with the same name but different method
signatures.
Ø allows the developer to create clean, sensible, readable, and resilient code.
A person plays an employee role in the office, father and husband role in the home.
24
Basic concepts of OOP: Abstraction
Ø an act of representing essential features without including background
details.
Ø technique of creating a new data type that is suited for a specific
application.
Ø Example: while driving a car, you do not have to be concerned with its
internal working. Here you just need to concern about parts like steering
wheel, Gears, accelerator, etc.
25
Basic concepts of OOP: Abstraction
Example: ATM
26
Basic concepts of OOP: Encapsulation
Ø wrapping the data and code //accessing members of a class in other class
Ø the variables of a class are always hidden from other classes.
Ø It can only be accessed using the methods of their current class.
Ø example – in school, a student cannot exist without a class.
Ø mechanism that allows us to bind data and functions of a class into an entity.
Ø protects data and functions from outside interference and misuse. provides
security.
Ø class is the best example of encapsulation. class{
Data members;
Method members;
}
27
28
Java Constructors
qConstructor is special initialization method that:
q is automatically invoked when a class instance is created using
the new operator
q has same name as the class
q has no return type
q is used to initialize the data members of the class.
q Java creates a default constructor if no constructor has been
explicitly written for a class.
29
Java Constructors(1)
1. Default (Non-Parameterized) Constructors
qused to initialize the object with default value.
2. Parameterized Constructors
q has parameter list to receive arguments for populating the
instance attributes.
q can accept values into the constructor that has different
parameters. The value would be transferred from the main ( )
method to the constructor either with direct values or through
variables.
30
Java Constructors Vs. Java Methods
Java Constructor Java Method
used to expose behavior of an
used to initialize the state of an object.
object.
must not have any return type. must have return type.
32
Access Modifiers/Specifiers (1)
Can be Accessed
Access
outside a
Modifier within a within a outside a
package by
class package package
subclass only
private Y N N N
default Y Y N N
protected Y Y Y N
public Y Y Y Y
33
End of Chapter Two
34