0% found this document useful (0 votes)
14 views34 pages

GGGG

The document provides an overview of Object-Oriented Programming (OOP) in Java, detailing its principles, features, and applications. It covers key concepts such as classes, objects, inheritance, encapsulation, and polymorphism, as well as the Java Development Kit (JDK), Java Virtual Machine (JVM), and Java Runtime Environment (JRE). Additionally, it discusses Java's security, platform independence, and various editions for different application types.

Uploaded by

dmuvine
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views34 pages

GGGG

The document provides an overview of Object-Oriented Programming (OOP) in Java, detailing its principles, features, and applications. It covers key concepts such as classes, objects, inheritance, encapsulation, and polymorphism, as well as the Java Development Kit (JDK), Java Virtual Machine (JVM), and Java Runtime Environment (JRE). Additionally, it discusses Java's security, platform independence, and various editions for different application types.

Uploaded by

dmuvine
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 34

Debre Markos University institute

of Technology,
Department of Software Engineering

Object Oriented Programming


in Java (SEng2062)
1
Chapter 1
Object Oriented Programming in Java

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.

Ø Object-Oriented Programming is a methodology or paradigm to


design a program using classes and objects.

Ø Everything in OOP is grouped as self-sustainable objects.

Ø The main aim of object-oriented programming is to implement


real-world entities

4
What is Java ?
Ø high-level, general-purpose, object-oriented, and secure
programming language developed by James Gosling at Sun
Microsystems

Ø java is also the most popular, widely used object-oriented


programming language.

Ø The security feature of Java makes it to be popular and widely


used programming language.

Ø By using Java, we can develop a variety of applications such as


enterprise applications, network applications, desktop applications,
web applications, games, android app, and many more. 5
Editions of Java
Ø Java Standard Editions (JSE): used to create programs for a
desktop computer.

Ø Java Enterprise Edition (JEE): used to create large programs


that run on the server and manages heavy traffic and complex
transactions.

Ø Java Micro Edition (JME): used to develop applications for


small devices such as set-top boxes, phone, and appliances.

Ø JavaFX : Uses hardware-accelerated graphics and media engines


and Applications can connect to networked data sources using
high-level APIs. 6
Java Applications
Standalone Application:

Ø desktop applications or window-based applications.

Ø These are traditional software that we need to install on every machine.

Ø Example: Media player, antivirus, etc.

Ø 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

q An application that is distributed in nature, such as banking applications

q It has advantages like high-level security, load balancing, and clustering.

q EJB (Enterprise Java Bean) is used for creating enterprise applications.

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 Object-Oriented: In Java, everything is in the form of the object.

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.

Ø Distributed: Java has networking facilities. It is designed for the


distributed environment of the internet because it supports TCP/IP protocol.
It can run over the internet. EJB and RMI are used to create a distributed
system.

Ø Multi-threaded: Java supports multi-threading to handle more than one


job at a time.
12
Components of Java Programming Language
Java Development kit (JDK)

Ø JDK refers to Software development environment used for making applets and
Java applications.

Ø Java developers can use it on Windows, macOS, Solaris, and Linux.

Ø 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.

Ø JDK includes a compiler, Java application launcher, Appletviewer, etc.

Ø Compiler converts code written in Java into byte code.

Ø Java application launcher opens JRE, loads the necessary class, and executes its
main method. 13
Components of Java Programming Language
Java Virtual Machine (JVM):

Ø an engine that provides a runtime environment to drive the Java Code or


applications.

Ø It converts Java bytecode into machine language.

Ø JVM is a part of the Java Runtime Environment (JRE).

Ø JVM provides a platform-independent way of executing Java source code.

Ø It has numerous libraries, tools, and frameworks.

Ø 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.

Ø self-contained component which consists of methods and properties to


make a particular type of data useful.

Ø 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.

Object creation Syntax in Java


ClassName ReferenceVariable = new ClassName();

17
Basic concepts of OOP: Class
Ø a blueprint or a set of instructions to build a specific type of object.

Ø a basic concept of Object-Oriented Programming which revolve around the


real-life logical entities.

Ø 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

real world entity such as pen, laptop,


group of similar objects
mobile, bed, keyboard, mouse, chair

physical entity logical entity

created through new keyword mainly declared using class keyword


e.g. Student s1=new Student(); e.g. class Student{ }

created many times as per


declared once
requirement

allocates memory when it is created. doesn't allocated memory when it is created.


21
Basic concepts of OOP
Members
Ø A class has members. Members can be fields, methods, or constructors.
Ø Classes can also contain instance initializers and static initializers. Static
initializers run only once, when the class is loaded. Instance initializers and
constructors are run every time an instance is created.
Ø Fields can be instance fields or class fields.
Ø Methods can be instance methods or class methods.
Ø Every instance of the class has its own copies of the instance fields, but all
instances share the class fields.
Ø Class fields belong to the class, not to each instance.

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.

invoked implicitly. invoked explicitly.


name may or may not be same as
name must be same as the class name.
class name.
Java compiler provides a default
not provided by Java compiler in
constructor if you don't have any
any case.
constructor.
31
Access Modifiers/Specifiers in Java
q specifies visibility (scope) of a data member, method, constructor
or class.
1. private access modifier: accessible only within a class.
2. protected access modifier: accessible within a package and
outside the package but through inheritance only.
3. public access modifier: accessible everywhere.
v has the widest scope among all other modifiers.
4. default access modifier: no any modifier and accessible only
within package.

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

You might also like