Object Oriented Programming Through Java (20A05302T) KLMCEW
Introduction to Object Oriented Programming
Object-oriented programming is a method used for designing a program using classes and objects. Object-
oriented programming is also called the core of java. Object-oriented programming organizes a program
around objects and well-defined interfaces. This can also be characterized as data controlling for
accessing the code. In this type of approach, programmers define the data type of a data structure and
the operations that are applied to the data structure. This implies software development and
maintenance by using some of the concepts:
Object
Class
Abstraction
Inheritance
Polymorphism
Encapsulation
OOPs, concepts in java is to improve code
readability and reusability by defining a Java
program efficiently. The main principles of object-
oriented programming are abstraction,
encapsulation, inheritance, and polymorphism.
These concepts aim to implement real-world
entities in programs.
Sudhakar Bathala Asst. Professor CSE Dept 1
Object Oriented Programming Through Java (20A05302T) KLMCEW
The History and Evolution of Java
Java is related to C++, which is a direct descendant of C. Much of the character of Java is inherited
from these two languages. From C, Java derives its syntax. Many of Java’s object-oriented features were
influenced by C++.
The 1960s gave birth to structured programming
C++ was invented by Bjarne Stroustrup in 1979. Stroustrup initially called the new language “C
with Classes.” C++ extends C by adding object-oriented features. Because C++ is built on the foundation
of C, it includes all of C’s features, attributes, and benefits.
By the end of the 1980s and the early 1990s, object-oriented programming using C++ took hold.
Java was conceived by James Gosling, Patrick Naughton, Chris Wrath, Ed Frank, and Mike Sheridan at Sun
Microsystems, Inc. in 1991. It took 18 months to develop the first working version. This language was
initially called “Oak,” but was renamed “Java” in 1995.
An applet is a special kind of Java program that is designed to be transmitted over the Internet and
automatically executed by a Java-compatible web browser
Java achieved this protection by confining an applet to the Java execution environment and not allowing
it access to other parts of the computer.
Portability is a major aspect of the Internet because there are many different types of computers and
operating systems connected to it. If a Java program were to be run on virtually any computer connected
to the Internet, there needed to be some way to enable that program to execute on different systems.
The key that allows Java to solve both the security and the portability problems just described is that the
output of a Java compiler is not executable code. Rather, it is bytecode. Bytecode is a highly optimized
set of instructions designed to be executed by the Java run-time system, which is called the Java Virtual
Machine (JVM). In essence, the original JVM was designed as an interpreter for bytecode. This may come
as a bit of a surprise since many modern languages
are designed to be compiled into executable code
because of performance concerns.
Translating a Java program into bytecode makes it
much easier to run a program in a wide variety of
environments because only the JVM needs to be
implemented for each platform. Once the run-time
package exists for a given system, any Java program
can run on it.
The fact that a Java program is executed by the JVM
also helps to make it secure.
Sudhakar Bathala Asst. Professor CSE Dept 2
Object Oriented Programming Through Java (20A05302T) KLMCEW
Although Java was designed as an interpreted language, there is nothing about
Java that prevents on-the-fly compilation of bytecode into native code in order to boost performance.
For this reason, the HotSpot technology was introduced not long after Java’s initial release.
HotSpot provides a Just-In-Time (JIT) compiler for bytecode. When a JIT compiler is part of the JVM,
selected portions of bytecode are compiled into executable code in real time, on a piece-by-piece,
demand basis.
A servlet is a small program that executes on the server. Just as applets dynamically extend the
functionality of a web browser, servlets dynamically extend the functionality of a web server. Thus, with
the advent of the servlet, Java spanned both sides of the client/server connection.
Servlets are used to create dynamically generated content that is then served to the client
To know more details update visit the link
https://docs.oracle.com/javase/tutorial/information/history.html
Key features of Java
Simple Multithreaded Dynamic
Secure Architecture-neutral Secure and
Portable Interpreted Portable
Object-oriented High performance
Robust Distributed
The Evolution of Java
The initial release of Java1.0 was nothing short of revolutionary, but it did not mark the end of
Java’s era of rapid innovation.
Java 1.1 added many new library elements, redefined the way events are handled, and reconfigured many
features of the 1.0 library.
Java 2, where the “2” indicates “second generation.” With Java 2, Sun repackaged the Java product as
J2SE (Java 2 Platform Standard Edition), and the version numbers began to be applied to that product.
Java 2 added support for a number of new features, such as Swing and the Collections Framework, and it
enhanced the Java Virtual Machine and various programming tools. Java 2 also contained a few
deprecations. The most important affected the Thread class in which the methods suspend( ), resume(
), and stop( ) were deprecated.
The next release of Java was J2SE consider the following list of its major new features:
Generics Enhanced, for-each
Annotations style for loop
Autoboxing and auto- Variable-length Formatted I/O
unboxing arguments (varargs) Concurrency utilities
Enumerations Static import
Sudhakar Bathala Asst. Professor CSE Dept 3
Object Oriented Programming Through Java (20A05302T)
KLMCEW
The next release of Java was called Java SE 6. The Java Development Kit was called
JDK 6. As with J2SE 5, the 6 in Java SE 6 is the product version number. The internal, developer version
number is 1.6.
Java SE 7 made several additions to the Java API library. Two of the most important were the
enhancements to the NIO Framework and the addition of the Fork/Join Framework.
The next release of Java was Java SE 8, with the developer’s kit being called JDK 8. It has an internal
version number of 1.8 with feature: the lambda expression.
The next release of Java was Java SE 9. The developer’s kit was called JDK 9. The primary new JDK 9 feature
was modules. A tool called jlink was added to the JDK
The next release of Java was Java SE 10 (JDK 10).
Java JVM, JDK and JRE
Java virtual Machine (JVM) is a
virtual Machine that provides
runtime environment to execute
java byte code. The JVM doesn't
understand Java typo, that's why you
compile your *.java files to obtain
*.class files that contain the
bytecodes understandable by the
JVM.
JVM control execution of every Java
program. It enables features such as
automated exception handling,
Garbage-collected heap.
Class Loader: Class loader loads the
Class for execution.
Method area: Stores pre-class
structure as constant pool.
Heap: Heap is a memory area in which objects are allocated.
Stack: Local variables and partial results are store here. Each thread has a private JVM stack created
when the thread is created.
Program register: Program register holds the address of JVM instruction currently being executed.
Native method stack: It contains all native used in application.
Executive Engine: Execution engine controls the execute of instructions contained in the methods of the
classes.
Sudhakar Bathala Asst. Professor CSE Dept 4
Object Oriented Programming Through Java (20A05302T)
KLMCEW
Native Method Interface: Native method interface gives an interface between
java code and native code during execution.
Native Method Libraries: Native Libraries consist of files required for the execution of native code.
Java Language Keywords
Here is a list of keywords in the Java programming language. You cannot use any of the following as
identifiers in your programs. The keywords const and goto are reserved, even though they are not currently
used. true, false, and null might seem like keywords, but they are actually literals; you cannot use them as
identifiers in your programs.
S.No Keyword Usage
1. abstract Specifies that a class or method will be implemented later, in a subclass
Assert describes a predicate placed in a java program to indicate that the developer
2. assert
thinks that the predicate is always true at that place.
3. boolean A data type that can hold True and False values only
4. break A control statement for breaking out of loops.
5. byte A data type that can hold 8-bit data values
6. case Used in switch statements to mark blocks of text
7. catch Catches exceptions generated by try statements
8. char A data type that can hold unsigned 16-bit Unicode characters
9. class Declares a new class
10. continue Sends control back outside a loop
11. default Specifies the default block of code in a switch statement
12. do Starts a do-while loop
13. double A data type that can hold 64-bit floating-point numbers
Sudhakar Bathala Asst. Professor CSE Dept 5
Object Oriented Programming Through Java (20A05302T)
KLMCEW
S.No Keyword Usage
14. else Indicates alternative branches in an if statement
A Java keyword is used to declare an enumerated type. Enumerations extend the
15. enum
base class.
16. extends Indicates that a class is derived from another class or interface
Indicates that a variable holds a constant value or that a method will not be
17. final
overridden
18. finally Indicates a block of code in a try-catch structure that will always be executed
19. float A data type that holds a 32-bit floating-point number
20. for Used to start a for loop
21. if Tests a true/false expression and branches accordingly
22. implements Specifies that a class implements an interface
23. import References other classes
Indicates whether an object is an instance of a specific class or implements an
24. instanceof
interface
25. int A data type that can hold a 32-bit signed integer
26. interface Declares an interface
27. long A data type that holds a 64-bit integer
28. native Specifies that a method is implemented with native (platform-specific) code
29. new Creates new objects
30. null Indicates that a reference does not refer to anything
31. package Declares a Java package
An access specifier indicating that a method or variable may be accessed only in
32. private
the class it’s declared in
An access specifier indicating that a method or variable may only be accessed in
33. protected the class it’s declared in (or a subclass of the class it’s declared in or other classes
in the same package)
An access specifier used for classes, interfaces, methods, and variables indicating
34. public that an item is accessible throughout the application (or where the class that defines
it is accessible)
35. return Sends control and possibly a return value back from a called method
36. short A data type that can hold a 16-bit integer
Indicates that a variable or method is a class method (rather than being limited to
37 static
one particular object)
A Java keyword is used to restrict the precision and rounding of floating-point
38. strictfp
calculations to ensure portability.
39. super Refers to a class’s base class (used in a method or class constructor)
40. switch A statement that executes code based on a test value
41. synchronized Specifies critical sections or methods in multithreaded code
42. this Refers to the current object in a method or constructor
43. throw Creates an exception
44. throws Indicates what exceptions may be thrown by a method
45. transient Specifies that a variable is not part of an object’s persistent state
Sudhakar Bathala Asst. Professor CSE Dept 6
Object Oriented Programming Through Java (20A05302T)
KLMCEW
S.No Keyword Usage
46. try Starts a block of code that will be tested for exceptions
47. void Specifies that a method does not have a return value
48. volatile This indicates that a variable may change asynchronously
49. while Starts a while loop
Introduction to Classes
In Java everything is encapsulated under classes. Class is the core of Java language. Class can be defined
as a template/ blueprint that describe the behaviors /states of a particular entity.
A class defines new data type. Once defined this new type can be used to create object of that type.
Object is an instance of class. You may also call it as physical existence of a logical template class.
A class is declared using class keyword. A class contain both data and code that operate on that data. The
data or variables defined within a class are called instance variables and the code that operates on this
data is known as methods.
Rules for Java Class
* A class can have only public or default (no modifier) access specifier.
* It can be either abstract, final or concrete (normal class).
* It must have the class keyword, and class must be followed by a legal identifier.
* It may optionally extend one parent class. By default, it will extend Object class i.e java.lang.Object.
* It may optionally implement any number of comma-separated interfaces.
* The class's variables and methods are declared within a set of curly braces {}.
* Each .java source file may contain only one public class. A source file may contain any number of
default visible classes.
* Finally, the source file name must match the public class name and it must have a .java suffix.
A Java class can contains fields, methods, constructors, and blocks. A general structure of a class.
Java class Syntax
class class_name {
field;
method;
}
Every class defines the properties and behaviors of an object. All the objects of a class have the same
properties and behaviors that were defined in the class.
Sudhakar Bathala Asst. Professor CSE Dept 7
Object Oriented Programming Through Java (20A05302T)
KLMCEW
Every class of java programming language has the following characteristics.
Identity It is the name given to the class.
State Represents data values that are associated with an object.
Behavior Represents actions can be performed by an object.
A simple class example
Suppose, Student is a class and student's name, roll number, age are its fields and info() is a method.
Then class will look like below.
class Student{
String name;
int rollno;
int age;
void info(){
// some code
}
}
It is a blueprint for an object. We can call its fields and methods by using the object.
The fields declared inside the class are known as instance variables. It gets memory when an object is
created at runtime.
Methods in the class are similar to the functions that are used to perform operations and represent
behavior of an object.
Java Object
Object is an instance of a class while class is a blueprint of an object. An object represents the class and
consists of properties and behavior.
Properties refer to the fields declared with in class and behavior represents to the methods available in
the class.
In real world, we can understand object as a cell phone that has its properties like: name, cost, color etc
and behavior like calling, chatting etc.
So we can say that object is a real world entity. Some real world objects are: ball, fan, car etc.
There is a syntax to create an object in the Java.
Java Object Syntax
className variable_name = new className();
Here, className is the name of class that can be anything like: Student that we declared in the above
example.
Sudhakar Bathala Asst. Professor CSE Dept 8
Object Oriented Programming Through Java (20A05302T)
KLMCEW
variable_name is name of reference variable that is used to hold the reference
of created object.
The new is a keyword which is used to allocate memory for the object.
Example: Object creation
Student std = new Student();
Here, std is an object that represents the class Student
during runtime.
The new keyword creates an actual physical copy of the
object and assign it to the std variable. It will have physical
existence and get memory in heap area. The new operator
dynamically allocates memory for an object.
Look at the following picture to understand the class and object concept.
Sudhakar Bathala Asst. Professor CSE Dept 9
Object Oriented Programming Through Java (20A05302T)
KLMCEW
Sudhakar Bathala Asst. Professor CSE Dept 10
Object Oriented Programming Through Java (20A05302T)
KLMCEW
https://www.mygreatlearning.com/blog/oops-concepts-in-java/#WhatIsOOPs
http://www.btechsmartclass.com/java/java-classes.html
https://www.brainkart.com/article/Java-Class-Fundamentals_10419/
https://www.studytonight.com/java/
MCQ
https://www.knowledgeboat.com/learn/class-9-logix-icse-computer-applications-java-
bluej/solutions/bAE8g/oop-concepts
Sudhakar Bathala Asst. Professor CSE Dept 11