UNIT-1 PART-1
FUNDAMENTALS OF OBJECT ORIENTED PROGRAMMING:
Basic Concepts of Object-Oriented Programming:
Java programming language was originally developed by Sun Microsystems, which was initiated by James Gosling and released in 1995 as core component of Sun Microsystems’s Java platform (Java 1.0 [J2SE]).
CLASS:
=> A class is a blueprint or prototype from which objects are created.
=> This section defines a class that models the state and behavior of a real-world object.
=> A class is a template, blueprint, or contract that defines what an object’s data fields and methods will be.
=> A Java class uses variables to define data fields and methods to define actions.
OBJECT:
=> An object is a software bundle of related state and behavior.
=> Software objects are often used to model the real-world objects that you find in everyday life
=> An object is an instance of a class. You can create many instances of a class.
ENCAPSULATION:
=> Encapsulation means putting together all the variables (instance variables) and the methods into a single unit called Class. It also means hiding data and methods within an Object. Encapsulation provides the security that keeps data and methods safe from inadvertent changes.
DATA ABSTRACTION:
=> Data abstraction is a compound reality by modeling classes of appropriate level of a legacy for a given phase of the problem. For example, Lassie the Dog may be cured as a Dog much of the time; it is also attained through Composition. For example, a car would be made up of an Engine, but one does not need to know how the diverse components work inside.
INHERITANCE:
=> Inheritance can be defined as the process where one object acquires the properties of another. With the use of inheritance the information is made manageable in a hierarchical order.
=> The extends keyword is used to achieve inheritance.
=> Java Does not Support Multiple Inheritance.
POLYMORPHISM:
=> Polymorphism is the ability of an object to take on many forms. The most common use of polymorphism in OOP occurs when a parent class reference is used to refer to a child class object.
=> Any java object that can pass more than one IS-A test is considered to be polymorphic. In Java, all java objects are polymorphic since any object will pass the IS-A test for their own type and for the class Object.
=> It is important to know that the only possible way to access an object is through a reference variable. A reference variable can be of only one type. Once declared the type of a reference variable cannot be changed.
=> The reference variable can be reassigned to other objects provided that it is not declared final. The type of the reference variable would determine the methods that it can invoke on the object.
=> A reference variable can refer to any object of its declared type or any subtype of its declared type. A reference variable can be declared as a class or interface type.
DYNAMIC BINDING:
=> Dynamic Binding is important tool for achieving polymorphic behavior.
=> If two or more methods with the same name but different method signatures are available in a class which method will be invoked at run time will be decided by parameters passed to method call.
=> Based on name of method in method call, first JAVA Compiler will find all methods with the same name. Once complier gets the list of available methods, it will find the method that best matches with type and number of parameters passed in method call.
=> If complier find the appropriate method, that method will be invoked. If complier does not find appropriate method, it will throw an exception.
APPLICATIONS AND BENEFITS OF OOP (Object Oriented Programming):
Main application areas of OOP are
=> User interface design such as windows, menu…
=> Real Time Systems
=> Simulation and Modeling
=> Object oriented databases
=> AI and Expert System
=> Neural Networks and parallel programming
=> Decision support and office automation system etc.
Benefits of OOP
The main advantages are
=> It is easy to model a real system as real objects are represented by programming objects in OOP. The objects are processed by their member data and functions. It is easy to analyze the user requirements.
=> With the help of inheritance, we can reuse the existing class to derive a new class such that the redundant code is eliminated and the use of existing class is extended. This saves time and cost of program.
=> In OOP, data can be made private to a class such that only member functions of the class can access the data. This principle of data hiding helps the programmer to build a secure program that can not be invaded by code in other part of the program.
=> With the help of polymorphism, the same function or same operator can be used for different purposes. This helps to manage software complexity easily.
=> Large problems can be reduced to smaller and more manageable problems. It is easy to partition the work in a project based on objects.
=> It is possible to have multiple instances of an object to co-exist without any interference i.e. each object has its own separate member data and function.
=> Java was created by a team of programmers at sun Microsystems of U.S.A in 1991.
=> This language was initially called “Oak” by James Gosling but renamed “Java” in 1995.
=> Java was designed for the development of software for consumer electronics devices like TVs, VCRs, and such electronics machines.
=> When the World Wide Web became popular in 1994, sun realized that Java was the perfect programming language for the Web.
=> Late 1995 and early 1996 they released Java & it was instant success.
FEATURES OF JAVA
1. Simple
2. Secure
3. Portable
4. Object-oriented
5. Robust
6. Multithreaded
7. Architecture-natural
8. Interpreted
9. High performance
10. Distributed
11. Dynamic
1. Simple
• Java was designed to be easy for the professional programmer to learn and use effectively.
• Assuming that you have some programming experience, you will not find java hard to master.
• If you already understand the basic concepts of object oriented programming, learning java will be even easier.
• If you are an experienced C++ programmer, moving to java will require very little effort. Because java inherits the C/C++ syntax and many of the object oriented features of C++, most programmers have little trouble learning Java.
2. Security
Security is the benefit of java. Java system not only verifies all memory access but also ensure that no viruses are communicated with an applet.
3. Portable
The most significant contribution of java over other language is its portability. Java programs can be easily moved from one computer system to another.
Java ensures portability in two ways:
=> Java compiler generates byte code instruction that can be implemented on any machine.
=> The size of the primitive data types is machine-independent.
4. Object-Oriented
• Java is a true object oriented language. All program code and data reside within object and classes.
• The object model in java is simple and easy to extend.
5. Robust(healthy, strong)
• The multiplatform environment of the Web places extraordinary demands on a program, because the program must execute reliably in a variety of systems. Thus, the ability to create robust programs was given a high priority in the design of java.
• To gain reliability, java has strict compile time and run time checking for codes.
• To better understand how java is robust, consider two main reasons for program failure: memory management mistakes and mishandled exceptional conditions.
1. Memory management can be a difficult, tedious task in traditional programming environments. For example, in C/C++, the programmer must manually allocate and free all dynamic memory. This sometimes leads to problems, because programmers will either forget to free memory that has been previously allocated or try to free some memory that another part of their code is still using. Java virtually eliminates these problems by managing memory allocation and deallocation for You. (In fact, deallocation is completely automatic, because java provides garbage collection for unused objects.)
2. Exceptional conditions in traditional environments often arise in situations such as division by zero or “file not found” and they must be managed with awkward and hard to read constructs. Java helps in this area by providing object oriented exception handling. In a well-written java program, all run-time errors can and should be managed by your program.
6. Multithreaded
• Java was designed to meet the real-world requirement of creating interactive, networked programs.
• To accomplish this, java supports multithreaded programming, which allows you to write programs that do many things simultaneously.
• The java run-time system comes with an elegant yet sophisticated solution for multiprocess synchronization that enables you to construct smoothly running interactive systems.
• Java’s easy to use approach to multithreading allows you to think about the specific behavior of your program, not the multitasking subsystem.
7. Architecture-Neutral
• A central issue of java programmers was that code longevity and portability. One of the main problems facing programmers is that no guarantee exists that if you write a program today, it will run tomorrow-even on the same machine.
• Operating system upgrades, and changes in core system resources can al combine to make a program malfunction.
• The java designer made several hard decisions in the java language and the java virtual machine in an attempt to alter this situation. Their goal was “write once; run anywhere, any time, forever.”
8. Interpreted
• Usually a computer language is either compiled or interpreted. Java combines these approaches thus making java a two-stage system.
• Java compiler translates source code into byte code instructions. Byte codes are not machine instructions and so java interpreter generates machine code that can be directly executed by the machine that is running the java program.
• We can thus say that java is both a compiled and an interpreted language.
9. High Performance
Java performance is impressive for an interpreted language, mainly due to the use of intermediate byte code.
10. Distributed
• Java is designed for the distributed environment of the Internet, because it handles TCP/IP protocols.
• Java also supports Remote Method Invocation (RMI). This feature enables a program to invoke methods across a network.
11. Dynamic
• Java is capable of dynamically linking in new class libraries, methods and object.
• Java can also determine the type of class through a query, making it possible to either dynamically link or abort the program.
JDK & its COMPONENTS
• Java Standard Edition (J2SE)
– J2SE can be used to develop client-side standalone applications or applets.
• Java Enterprise Edition (J2EE)
– J2EE can be used to develop server-side applications such as Java servlets and Java ServerPages.
• Java Micro Edition (J2ME).
– J2ME can be used to develop applications for mobile devices such as cell phones.
The java Developer’s Kit(JDK)
Before you can start writing java program, you need to acquire and setup
some kind of java programming software . Although several different products
are available for development of java programe, the starting place for many new
java programmers is the JDK.
JDK Tools:
1. A Compiler:
Javac, which takes a java program file and translates it into a form which the computer know how to run.
2. An Interpreter
Java, run program that are created by the compiler.
3. A Debugger
Java debugger can help you to find error in your program.
4. A Disassembler
If you run in to a compiled java program that you would like to see In java source code form, the java disassembler will do the translation for you
5. A Profiler:
Profiler will provide the handy information about your program.
You will need a Pentium 200-MHz computer with a minimum of 64 MB of RAM (128
MB of RAM recommended).
You also will need the following softwares:
=> Linux 7.1 or Windows 95/98/2000/XP operating system.
=> Java JDK 5
=> Microsoft Notepad or any other text editor like (Edit Plus)
Java Editors:
To write your java programs you will need a text editor.
=> Notepad: On Windows machine you can use any simple text editor like Notepad (Recommended for this tutorial), Text Pad.
=> Netbeans: is a Java IDE that is open source and free which can be downloaded from http://www.netbeans.org/index.html.
=> Eclipse: is also a java IDE developed by the eclipse open source community and can be downloaded from http://www.eclipse.org/.
BYTE CODE and JVM
Example
class Example{
public static void main(String args[]){
System.out.println(“First Example”);
}
}
example1
=> class Example
Here name of the class is Example.
=> public static void main(String args[])
public: The public keyword is an access specifier, which means that the content of the following block accessible from all other classes.
static: The keyword static allows main() to be called without having to instantiate a particular instance of a class.
void: The keyword void tells the compiler that main() does not return a value. The methods can return value.
main(): main is a method called when a java application begins,
• Java is case-sensitive
• Main is different from main
• Java compiler will compile classes that don’t contain a main () method but java has no ways to run these classes.
=> String args []
– Declares a parameter named args, which is an array of instance of the class string.
– Args[] receives any command-line argument present when the program is executed.
=> System.out.println()
– System is predefined class that provides access to the system.
– Out is the output stream that is connected to the console.
– Output is accomplished by the built-in println() method. Println() displays the string which is passed to it.
Compilation
=> Javac Example.java
This command will compile the source file and if the compilation is successful it will generate a file named example.class containing bytecode. Java compilers translate java program to bytecode form.
=> Java Example
The command called ‘java’ takes the bytecode and runs the bytecode on JVM environment in interpreted mode.
=> Output
First Example
=> When java source code is compiled, each individual class is put into its own output file named after the class and using the .class extension. This is why it is a good idea to give your java source files the same name as the class they contain – the name of the source file will match the name of the .class file.
Java Virtual Machine
=> All language compilers translate source code into machine code for a specific computer. Java compiler also does the same thing.
=> Java compiler produces an intermediate code known as byte code for a machine that does not exist.
=> This machine is called the Java Virtual Machine and it exists only inside the computer memory.
=> The virtual machine code (Byte Code) is not machine specific.
=> The machine specific code is generated by the Java Interpreter by acting as an intermediary between the virtual machine and the real machine as shown in fig. Interpreter is different for different
machine.
JAVA PROGRAM STRUCTURE
BASIC OF JAVA
HISTORY OF JAVA=> Java was created by a team of programmers at sun Microsystems of U.S.A in 1991.
=> This language was initially called “Oak” by James Gosling but renamed “Java” in 1995.
=> Java was designed for the development of software for consumer electronics devices like TVs, VCRs, and such electronics machines.
=> When the World Wide Web became popular in 1994, sun realized that Java was the perfect programming language for the Web.
=> Late 1995 and early 1996 they released Java & it was instant success.
FEATURES OF JAVA
1. Simple
2. Secure
3. Portable
4. Object-oriented
5. Robust
6. Multithreaded
7. Architecture-natural
8. Interpreted
9. High performance
10. Distributed
11. Dynamic
1. Simple
• Java was designed to be easy for the professional programmer to learn and use effectively.
• Assuming that you have some programming experience, you will not find java hard to master.
• If you already understand the basic concepts of object oriented programming, learning java will be even easier.
• If you are an experienced C++ programmer, moving to java will require very little effort. Because java inherits the C/C++ syntax and many of the object oriented features of C++, most programmers have little trouble learning Java.
2. Security
Security is the benefit of java. Java system not only verifies all memory access but also ensure that no viruses are communicated with an applet.
3. Portable
The most significant contribution of java over other language is its portability. Java programs can be easily moved from one computer system to another.
Java ensures portability in two ways:
=> Java compiler generates byte code instruction that can be implemented on any machine.
=> The size of the primitive data types is machine-independent.
4. Object-Oriented
• Java is a true object oriented language. All program code and data reside within object and classes.
• The object model in java is simple and easy to extend.
5. Robust(healthy, strong)
• The multiplatform environment of the Web places extraordinary demands on a program, because the program must execute reliably in a variety of systems. Thus, the ability to create robust programs was given a high priority in the design of java.
• To gain reliability, java has strict compile time and run time checking for codes.
• To better understand how java is robust, consider two main reasons for program failure: memory management mistakes and mishandled exceptional conditions.
1. Memory management can be a difficult, tedious task in traditional programming environments. For example, in C/C++, the programmer must manually allocate and free all dynamic memory. This sometimes leads to problems, because programmers will either forget to free memory that has been previously allocated or try to free some memory that another part of their code is still using. Java virtually eliminates these problems by managing memory allocation and deallocation for You. (In fact, deallocation is completely automatic, because java provides garbage collection for unused objects.)
2. Exceptional conditions in traditional environments often arise in situations such as division by zero or “file not found” and they must be managed with awkward and hard to read constructs. Java helps in this area by providing object oriented exception handling. In a well-written java program, all run-time errors can and should be managed by your program.
6. Multithreaded
• Java was designed to meet the real-world requirement of creating interactive, networked programs.
• To accomplish this, java supports multithreaded programming, which allows you to write programs that do many things simultaneously.
• The java run-time system comes with an elegant yet sophisticated solution for multiprocess synchronization that enables you to construct smoothly running interactive systems.
• Java’s easy to use approach to multithreading allows you to think about the specific behavior of your program, not the multitasking subsystem.
7. Architecture-Neutral
• A central issue of java programmers was that code longevity and portability. One of the main problems facing programmers is that no guarantee exists that if you write a program today, it will run tomorrow-even on the same machine.
• Operating system upgrades, and changes in core system resources can al combine to make a program malfunction.
• The java designer made several hard decisions in the java language and the java virtual machine in an attempt to alter this situation. Their goal was “write once; run anywhere, any time, forever.”
8. Interpreted
• Usually a computer language is either compiled or interpreted. Java combines these approaches thus making java a two-stage system.
• Java compiler translates source code into byte code instructions. Byte codes are not machine instructions and so java interpreter generates machine code that can be directly executed by the machine that is running the java program.
• We can thus say that java is both a compiled and an interpreted language.
9. High Performance
Java performance is impressive for an interpreted language, mainly due to the use of intermediate byte code.
10. Distributed
• Java is designed for the distributed environment of the Internet, because it handles TCP/IP protocols.
• Java also supports Remote Method Invocation (RMI). This feature enables a program to invoke methods across a network.
11. Dynamic
• Java is capable of dynamically linking in new class libraries, methods and object.
• Java can also determine the type of class through a query, making it possible to either dynamically link or abort the program.
JDK & its COMPONENTS
• Java Standard Edition (J2SE)
– J2SE can be used to develop client-side standalone applications or applets.
• Java Enterprise Edition (J2EE)
– J2EE can be used to develop server-side applications such as Java servlets and Java ServerPages.
• Java Micro Edition (J2ME).
– J2ME can be used to develop applications for mobile devices such as cell phones.
The java Developer’s Kit(JDK)
Before you can start writing java program, you need to acquire and setup
some kind of java programming software . Although several different products
are available for development of java programe, the starting place for many new
java programmers is the JDK.
JDK Tools:
1. A Compiler:
Javac, which takes a java program file and translates it into a form which the computer know how to run.
2. An Interpreter
Java, run program that are created by the compiler.
3. A Debugger
Java debugger can help you to find error in your program.
4. A Disassembler
If you run in to a compiled java program that you would like to see In java source code form, the java disassembler will do the translation for you
5. A Profiler:
Profiler will provide the handy information about your program.
You will need a Pentium 200-MHz computer with a minimum of 64 MB of RAM (128
MB of RAM recommended).
You also will need the following softwares:
=> Linux 7.1 or Windows 95/98/2000/XP operating system.
=> Java JDK 5
=> Microsoft Notepad or any other text editor like (Edit Plus)
Java Editors:
To write your java programs you will need a text editor.
=> Notepad: On Windows machine you can use any simple text editor like Notepad (Recommended for this tutorial), Text Pad.
=> Netbeans: is a Java IDE that is open source and free which can be downloaded from http://www.netbeans.org/index.html.
=> Eclipse: is also a java IDE developed by the eclipse open source community and can be downloaded from http://www.eclipse.org/.
BYTE CODE and JVM
Example
class Example{
public static void main(String args[]){
System.out.println(“First Example”);
}
}
example1
=> class Example
Here name of the class is Example.
=> public static void main(String args[])
public: The public keyword is an access specifier, which means that the content of the following block accessible from all other classes.
static: The keyword static allows main() to be called without having to instantiate a particular instance of a class.
void: The keyword void tells the compiler that main() does not return a value. The methods can return value.
main(): main is a method called when a java application begins,
• Java is case-sensitive
• Main is different from main
• Java compiler will compile classes that don’t contain a main () method but java has no ways to run these classes.
=> String args []
– Declares a parameter named args, which is an array of instance of the class string.
– Args[] receives any command-line argument present when the program is executed.
=> System.out.println()
– System is predefined class that provides access to the system.
– Out is the output stream that is connected to the console.
– Output is accomplished by the built-in println() method. Println() displays the string which is passed to it.
Compilation
=> Javac Example.java
This command will compile the source file and if the compilation is successful it will generate a file named example.class containing bytecode. Java compilers translate java program to bytecode form.
=> Java Example
The command called ‘java’ takes the bytecode and runs the bytecode on JVM environment in interpreted mode.
=> Output
First Example
=> When java source code is compiled, each individual class is put into its own output file named after the class and using the .class extension. This is why it is a good idea to give your java source files the same name as the class they contain – the name of the source file will match the name of the .class file.
Java Virtual Machine
=> All language compilers translate source code into machine code for a specific computer. Java compiler also does the same thing.
=> Java compiler produces an intermediate code known as byte code for a machine that does not exist.
=> This machine is called the Java Virtual Machine and it exists only inside the computer memory.
=> The virtual machine code (Byte Code) is not machine specific.
=> The machine specific code is generated by the Java Interpreter by acting as an intermediary between the virtual machine and the real machine as shown in fig. Interpreter is different for different
machine.
JAVA PROGRAM STRUCTURE



