0% found this document useful (0 votes)
20 views20 pages

Java Programming: OOP Concepts Overview

The document provides an overview of object-oriented programming (OOP) concepts, including definitions of objects, classes, encapsulation, inheritance, polymorphism, and messaging. It also discusses the evolution and features of Java, highlighting its differences from C and C++. Additionally, it outlines the benefits and applications of OOP in various software development contexts.

Uploaded by

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

Java Programming: OOP Concepts Overview

The document provides an overview of object-oriented programming (OOP) concepts, including definitions of objects, classes, encapsulation, inheritance, polymorphism, and messaging. It also discusses the evolution and features of Java, highlighting its differences from C and C++. Additionally, it outlines the benefits and applications of OOP in various software development contexts.

Uploaded by

scasshanmu
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

MUTHAYAMMAL COLLEGE OF ARTS & SCIENCE, RASIPURAM

DEPARTMENT OF COMPUTER SCIENCE

UNIT : I CLASS: I IT
PAPER NAME: JAVA PROGRAMMING PAPER CODE:23M2UITC02

Object oriented paradigm - Basics concept of object oriented programming- Benefits of object
oriented programming- Application of object oriented programming. Java Evolution: History –
Features- how java differs from c and c++ -java and internet – java and www – Web browser.
Overview of java: Simple Java Program – Java program structure – Java Tokens – Java Statements –
JVM
OBJECT ORIENTED PARADIGM:
OBJECT :

 object is a real-world element in an object–oriented environment


 Each object has –
o Identity that distinguishes it from other objects in the system.
o that determines characteristic properties of an object as well as values of properties
that the object holds.
o that represents externally visible activities performed by an object in terms of changes
in its state.

CLASS:
 A class represents a collection of objects having same characteristic properties that exhibit
common behavior.
 the blueprint or the description of the objects that can be created from it
 Creation of an object as a member of a class is called instantiation.
 An object is an instance of a class.

ENCAPSULATION:
 The process of wrapping up data and functions into a single unit.
 Data is not accessible to the outside world, and only those functions which are wrapped in
the class can access it.
 It provides the interface between data objects and the program.

1
Abstraction:
 It does not represent the background details and explanation.
 Classes use the concept of abstraction and define the list of abstract attributes such as name,
age, gender, etc.,
 to operate on these attributes.
 They encapsulate all the essential properties of the object.

Inheritance:
 Inheritance is the property whereby one class extends another class’s properties, including
additional methods and variables.
 The original class is called a superclass,
 class that exceeds the properties are called a subclass.
 As the subclass contains all the data of the superclass.

Polymorphism:
 In geek terms.
 polymorphism means the ability to take more than one form.
 An operation may exhibit different behavior in a different instance.
 Behavior depends on the types of data used for the operation.

Messaging:

 Object oriented system consists of sets of objects that communicate with each other.

 Object communicate with one another by sending and receiving data much the same way as

people pass messages to one another.

 A message for the object is a request for execution of a method and, therefore, will invoke a

method in the receiving object that generates the desired result.

BASICS CONCEPT OF OBJECT ORIENTED PROGRAMMING


 Class Example: class Car { String color; void accelerate() { ... } }
 Object Example: Car myCar = new Car();
 Inheritance Example: class ElectricCar extends Car { ... }
 Polymorphism Example: Method overloading and overriding.
 Abstraction Example: Abstract classes and interfaces.
 Encapsulation Example: Private fields with public getters and setters.

2
Object
 Objects are always called instances of a class which are created from a class
 objects always correspond to things found in the real world
 It contains addresses and takes up some space in memory.
 examples of objects are a dog, chair, tree etc.

Public class Mybook {


int x=10;
Public static void main (String args []) {
Mybook Myobj= new Mybook (); // object creation
[Link](MyObj.x); // return the value of x of an object.
}
}

Class
 Classes are like object constructors for creating objects.
 The collection of objects is said to be a class.
 Classes don’t consume any space in the memory.
 Class is also called a template of an object.
 Classes have members which can be fields, methods and constructors.
 A class declaration consists of:

1. Modifiers: These can be public or default access.


2. Class name: Initial letter.
3. Superclass: A class can only extend (subclass) one parent.
4. Interfaces: A class can implement more than one interface.
5. Body: Body surrounded by braces, { }.

Eg:
3
class classname {
type instance variable 1;
type instance variable 2;
.
.
type instance variable n;
type methodname 1 (parameter list)
{
// body of method
}
type methodname 2 (parameter list)
{
// body of method
}
type methodnamen (parameter list)
{
// body of method
}
}

Inheritance
Single level :
In this one class
the derived class inherits properties from its parental class.
Class a {

}
Class b extends class a {

}
Hierarchical level

Class a {

}
Class b extends class a {
..
}
Class c extends class a {
..
}

Polymorphism
public class Bird {

Public void sound ( ) {
[Link] ( “ birds sounds “ );
}
}
public class pigeon extends Bird {

@override

4
public void sound ( ) {
[Link]( “ cooing ” ) ;
}
}
public class sparrow extends Bird ( ) {
….
@override
Public void sound ( ){
[Link]( “ chip ” ) ;
}
}

BENEFITS OF OBJECT-ORIENTED PROGRAMMING

1. Offers Security - In this method, developers bundle data to encapsulate information inside
an object.
2. Improves Collaboration - to divide a complex software system into small, manageable
objects.
3. Allows Reuse of Code - The concept of inheritance allows OOP to promote the reuse of
code.
4. Makes Changes Seamlessly - The abstraction of complex systems into simplified models is
one of the pivotal benefits of object-oriented programming.
5. Locates and Fixes Problems Effortlessly - The ability to easily pinpoint bugs is among the
most vital advantages of object-oriented programming.
6. Ensures Flexibility - different types of objects as objects of a common superclass.
7. Drives Productivity - improve software quality at an affordable cost.
8. Maintains Code Consistently - The code is more readable and maintainable under OOP
because it models real-world objects.
9. Encourages Scalability - The effort to scale software systems when they grow in
complexity is smooth in OOP.
10. Reduces Development Cost - advantage to having code reusability— the reduction of
development costs.

APPLICATION OF OBJECT ORIENTED PROGRAMMING

 Desktop applications
 Mobile applications (particularly Android based)
 Games
 Graphical User Interface Design
 Big Data applications
 Web applications
 Parallel Programming
 Machine Learning Applications
 Artificial Intelligence
 Expert Systems

5
 CAD/CAM Software
 Real Time Systems
 e-Business Applications
 Database connection
 Object Oriented Database
 Web servers and application servers

INTRODUCTION TO JAVA:
 Java is a true object oriented language.
 Java is similar to C++.
 Java features can be modified from C++.
 There are no header files in JAVA.
 Java doesn’t support multiple inheritance, operator overloading, global variable, pointers.

JAVA EVOLUTION:
JAVA HISTORY:
 The following table shows the milestones in the development of java.
Year Development
1990 Sun Microsystems decided to develop special software that could be used to
manipulate consumer electronic devices.
A team of Sun Microsystems programmers headed by James Gosling was formed.
1991 The team announced a new language named “Oak”.
1992 The green project team by Sun, demonstrated the application of their new language
to control the list of home appliances using a hand-held device with a tiny touch
sensitive screen.
1993 The World Wide Web (WWW) appeared on the internet into a graphical-rich
environment. The green project team came up with the idea of developing web
Applets using the new language. It can run on all types of computers connected to the
Internet.
1994 The team developed a Web browser called “HotJava”.
1995 Oak was renamed “Java” due to some legal reasons.
1996 Sun releases Java Development Kit 1.0.
1997 Sun releases Java Development Kit 1.1 (JDK 1.1).
1998 Sun releases the Java 2 with version 1.2 of the Software Development Kit (SDK 1.2).
1999 Sun releases Java 2 platform. Standard Edition (J2SE) and Enterprise Edition(J2EE).

6
2000 J2SE with SDK 1.3 was released.
2002 J2SE with SDK 1.4 was released.
2004 J2SE with JDK5.0 was released. This is known as J2SE 5.0.
2006 Java SE 6 was released. With this release, Sun changes the name form ‘J2SE’ to
‘Java SE’ and also removed ‘0’ from the version name.
2011 Java SE 7 was released.

Features of JAVA:
 Compiled & Interpreted
 Platform Independent & Portable.
 Object –Oriented.
 Robust & Secure.
 Distributed
 Familiar ,Simple & Small
 Multithreaded & Interactive.
 High Performance.
 Dynamics and Extensible.

Compiled & Interpreted:


 Java combines both approaches.
 So java is a two-stage system.
 Java compiler translates source code into byte code.
 Byte codes are not machine instructions.
 Java interpreter can generates machine code.
 It can be directly executed by machine.
 So java is compiled & an interpreted language.

Platform Independent & Portable


 It is portable.
 Java programs can be easily moved from one system to another anywhere and anytime.
 Changes in OS, processors & system resources will not force any in java programs.
 So java has become a popular language for programming on internet.
Portable:
 It ensures portability in 2 ways:
 Java compiler generates byte code instructions that can be implemented on any machine.

7
 Size of primitive datatypes is machine independent.

Object Oriented:
 It is true object oriented language.
 Everything in Java is an object.
 All codes & data are placed inside of objects & classes.
 It is an extensible set of classes arranged in packages use by inheritance.
 So java is simple & easy to extend.

Robust:
 Java is a robust language.
 It Provides Safeguards to ensure reliable code.
 It has strict compile time & run time checking for data type.
 It has concept of execution handling which captures series errors & eliminates risk.

Secured:
 Java is a secure language.
 Java is used on Internet.
 Threat of viruses & abuse of resources are everywhere.
 The absence of pointers in Java ensures the restriction for unauthorized access.

Distributed:
 It is a distributed language for creating applications on Internet or network.
 It has the ability to share both data & programs.
 It can open, access remote objects on Internet as easily.

Simple, Small, Familiar:


 Java is simple & small language.
 It also eliminates operator overloading & multiple inheritance, goto statement & preprocessor header
& pointers.
 It is a familiar to existing programmers.

Multithreaded and Interactive


 Multithreaded means handling multiple tasks simultaneously. Java supports multithreaded
programs.
8
 This feature improves interactive performance of graphical application.
Dynamic and Extensible
 Java is a Dynamic language. Java is capable of dynamically linking in new class libraries,
methods and objects.
 Java support functions in other languages like C & C++.
 These functions are called Native Functions.
 Native Functions are linked dynamically at runtime.
HOW JAVA DIFFER FROM C AND C++
C
C is a general-purpose,
structured, procedural,
high-level programming language developed by Dennis MacAlistair Ritchie in 1972 at Bell Laboratories.
Features of C Language:
 Machine independent and portable
 Modern Control Flow and Structure
 Rich set of operators
 Simple, Fast, and efficient
 Case-sensitive
 Low memory use
 Easily extendable
 Statically-typed

C++
 C++ is an object-oriented, general-purpose, programming language developed by Bjarne
Stroustrup at Bell Labs in 1979.

Features of C++ Language


 Case-sensitive
 Compiler based
 Platform-independent
 Portability
 Dynamic memory allocation

Java
9
 An object-oriented, class-based, static, strong, robust, safe, and high-level programming
language. It was developed by James Gosling in 1995.
 It is bot compiled and interpreted.
 It is used to develop enterprise, mobile, and web-based applications.

Features of Java
o Object-oriented
o Architecture-neutral
o Platform independent
o Dynamic and Distributed
o Robust
o Secure
o Multithreaded

S.N. Basis C C++ Java

1 Origin The C language is The C++ language is The Java programming


based on BCPL. based on the C language is based on
language. both C and C++.

2 Programming It is a procedural It is an object-oriented It is a pure object-


Pattern language. programming oriented programming
language. language.

3 Approach It uses the top-down It uses the bottom-up It also uses the bottom-
approach. approach. up approach.

4 Dynamic or It is a static It is also a static It is a dynamic


Static programming programming programming language.

10
language. language.

5 Code The code is executed The code is executed The code is executed by
Execution directly. directly. the JVM.

6 Platform It is platform It is platform It is platform-


Dependency dependent. dependent. independent because of
byte code.

7 Translator It uses a compiler only It also uses a compiler Java uses both compiler
to translate the code only to translate the and interpreter and it is
into machine language. code into machine also known as an
language. interpreted language.

8 File It generates the .exe, It generates .exe file. It generates .class file.
Generation and .bak, files.

9 Number of There are 32 keywords There There are 52 keywords


Keyword in the C language. are 60 keywords in in the Java language.
the C++ language.

10 Source File The source file has a .c The source file has The source file has
Extension extension. a .cpp extension. a .java extension.

11 Pointer It supports pointer. It also supports Java does not support


Concept pointer. the pointer concept
because of security.

12 Union and It supports union and It also supports union It does not support
Structure structure data types. and structure data union and structure
Datatype types. data types.

13 Pre-processor It uses pre-processor It uses pre-processor It does not use


Directives directives such as directives such as directives but uses
#include, #define, etc. #include, #define, packages.
#header, etc.

14 Constructor/ It does not support It supports both It supports constructors


Destructor constructor and constructor and only.
destructor. destructor.

15 Exception It does not support It supports exception It also supports


Handling exception handling. handling. exception handling.

16 Memory It uses the calloc(), It uses new and delete It uses a garbage
Management malloc(), free(), and operator to manage collector to manage the
realloc() methods to the memory. memory.
manage the memory.

17 Overloading It does not support the Method and operator Only method
overloading concept. overloading can be overloading can be
achieved. achieved.

18 goto It supports the goto It also supports the It does not support the
Statement statement. goto statement. goto statements.

11
19 Used for It is widely used to It is widely used for It is used to develop
develop drivers and system programming. web applications,
operating systems. mobile applications, and
windows applications.

20 Array Size An array should be An array should be An array can be


declared with size. For declared with size. declared without
example, int num[10]. declaring the size. For
example, int num[].

java and www


World Wide Web is an open-ended information retrieval system designed to be used in the Internet’s
distributed environment.

1. The user sends a request for an HTML document to the remote computer’s Web server. The Web
server is a program that accepts a request, processes the request, and sends the request document.
2. The HTML document is returned to the user’s browser. The document contains the APPLET tag,
which identifies the applet.
3. The corresponding applet byte code is transferred to the user’s computer. This bytecode had been
previously created by the Java compiler using Java source code file for that applet.
4. The Java enabled browser on the user’s computer interprets the byte codes and provides output.
5. The user may have further interaction with the applet but with no further downloading from the
provider’s Web server. This is because the bytecode contains all the information necessary to
interpret the applet.

JAVA ENVIRONMENT:
 Java environment includes a large number of development tools, and hundreds of classes and
methods.

12
 The development tools are part of the system known as Java Development Kit (JDK) and the
classes and methods are part of the Java Standard Library (JSL) also known as API (Application
Programming Interface).
JAVA DEVELOPMENT KIT (JDK):
 The JDK comes with a collection of tools that are used for developing and running java
programs.
They include:
 AppletViewer (for viewing java applets).
 javac (java compiler)
 java (java Interpreter, which runs applets and application by reading and
interpreting byte code files)
 javah (produces header files for use with native methods)

Text Editor

Java Source HTML


Code javadoc Files

javac

Header
Java Class File javah Files

java jdb

Java Program Output

 javap (java dissembler, which enables us to convert byte code files into a program
description)
 jdb (java debugger, which helps us to find errors in our programs)

13
 To create source file using Text Editor.
 Source code is converted into HTML using java doc.
 Source code is compiled using javac & interpreted using java.
 Java debugger jdb is used to find errors. Compiled java code converted into source code using
javap (java disassembler).
Application Programming Interface:
 Java standard library (JSL) or API includes hundreds of classes & methods are grouped into several
functional Packages.
Commonly used Packages:
Language Support Packages:
 Collection of classes & methods required for implementing basic features of Java.
Utilities Package:
 Collection of classes to provide utility functions such as date & time.
I/O Packages:
 I/O manipulations.
Network Packages:
 Collection of classes for communicating with other computers via Internet.
AWT Packages:
 Abstract window Toolkit package contains classes that implements platform independent graphical
user.
Applet Packages:
 Set of a classes that allows us to create Java applets.
*****
SIMPLE JAVA PROGRAM:

class sample
{
public static void main(String args[])
{
[Link](“Welcome to java”);
}
}

14
THE FIRST LINE:
 Class sample declares a class, which is an object oriented construct. Class is a keyword and declares
that a new class definition follows.
THE THIRD LINE
 Public static void main(String args[]) defines a method named main.
 Every java application program must include the main() method.
 This is the starting point for the interpreter to begin the execution of the program.
 A java application can have any number of classes but only one of them must include a main method
to initiate the execution.
 The keyword public is an access specifier that declares the main method as unprotected and therefore
making it accessible to all other classes. This is similar to the C++ public modifier.
Static:
 The keyword static, which declares this method as one that belongs to the entire class and not part of
any objects of the class.
 It (main) must always be declared as static since the interpreter uses this method before any objects
are created.
Void:
 The type modifier void states that the main method does not return any value.
The Output Line
 The only executable statement in the program is
[Link](“Welcome to java”);
 This is similar to the printf() statement of C or cout << construct of C++.
 Since Java is a true object oriented language, every method must be part of an object.
 The println method is a member of the out object.
 The out object is a static member of System class.
 The method println always appears a newline character to the end of the string.

Java Program structure:


Java program involves the following sections:
 Documentation Section

15
 Package Statement
 Import Statements
 Interface Statement
 Class Definition
 Main Method Class
 Main Method Definition

Section Description

Documentation We can write a comment in this section. Comments are beneficial for the programmer because they
Section help them understand the code. These are optional, but it is useful to understand the operation of the
program, so we must write comments within the program.

Package statement We can create a package with any name. A package is a group of classes that are defined by a name.
That is, if we want to declare many classes within one element, then we can declare it within a
package. It is an optional part of the program.
package package_name;

Import statements This line indicates that if we want to use a class of another package, then we can do this by importing
it directly into our program.
Example:

import [Link];

Interface Interfaces are like a class that includes a group of method declarations. It's an optional section and can

16
statement be used when programmers want to implement multiple inheritances within a program.

Class Definition A Java program may contain several class definitions. Classes are the main and essential elements of
any Java program.

Main Method Every Java stand-alone program requires the main method as the starting point of the program. This is
Class an essential part of a Java program. There may be many classes in a Java program, and only one class
defines the main method. Methods contain data type declaration and executable statements.

JAVA TOKENS:
 A java program is a collection of classes.
 A class is defined by a set of declaration statement and methods containing executable statements.
 Smallest Individual Units in a program are called as Tokens.
 A java program is a collection of Tokens, Comments & White Spaces.
 It includes 5 Types of Tokens.
 Reserved Keywords.
 Identifiers.
 Literals.
 Operators.
 Separators.
Java Character Set:
 Smallest Units of Java language are characters which used to write Java Tokens.
 Characters are defined by Unicode char.
 Unicode is a 16 bit char coding system.
 It support more than 34000 char derived from 24 language.
 Mostly use ACII char, includes letters, Digits, Punctuation, Marks.
RESERVED KEYWORDS
 Java language has 60 reserved words as keywords.
 Keywords have specific meaning in java; we cannot use them as names for variables, methods and so
on.
 Java is case sensitive. All keywords are to be written in lowercase letters.

E.g. native case


Eg: auto int
void long

17
goto float
do char
IDENTIFIERS:
Identifiers are programmer-defined tokens.
They are used for naming classes, methods, variables, objects, labels, packages and interfaces in a program.
Rules:
1. They can have alphabets, digits, and the underscore and dollar sign characters.
2. They must not begin with a digit.
3. Uppercase and lowercase letters are distinct.
4. They can be of any length.
LITERALS:
 Literals in java are a sequence of characters (digits, letters and other characters) that represent
constant values to be stored in variables.
 Java language specifies five major types of literals. They are:
a) Integer literals
b) Floating-point literals
c) Character literals
d) String literals
e) Boolean literals.
JAVA STATEMENTS:
 A statement is an executable combination of tokens ending with semicolon.
 Statement is usually executed in sequence in order.
 It is used to control the flow of execution.
 Several types of statements.
1. Expression Statement.
2. Labelled Statement
3. Synchronized Statement.
4. Guarding Statement.
5. Control Statement.
5.1 Selection Statement.if,if_else,Switch
5.2 Iteration Statement.while,do,for
5.3 Jump Statement.break,continue,return.

18
Empty Statement:
 These do nothing.
 It is used during program development as a place holder.
Labeled Statement:
 Any statement may begin with a label.
 Labels must not be keywords.
 Labels are used as arguments of Jump statements.
Expression statement:
 Most statements are expression statements.
 Java has seven types of expression statements.
 They are ,
Assignment, Pre-Increment, Post-Increment, Pre-Decrement, Post-Decrement, Method call and
allocation.
Selection Statement:
 Selects one of several control flows.
 3 types of selection statements. if,if_else,Switch.
Iteration Statement:
 Specify how & when looping will take place.
 3 types of iteration statements. while,do,for.
Jump Statements:
 It passes control to beginning or end of the current block or labeled statement.
 4 types of jump statements are break, continue, return and throw.
19
Synchronization Statements:
 It is used for handling issues with Multithreading.
Guarding Statements:
 It is used for safe handling of code that may cause exceptions.
 These statements use the keywords Try, Catch, Finally.
*****

Diagram of JVM

********************* UNIT I COMPLETED *************************

20

You might also like