Stop thinking about WHAT WILL HAPPEN and start thinking about WHAT YOU CAN DO.
--Your friends at LectureNotes

Object Oriented Programming Using JAVA

by Sarmi Sasirekha
Institute: Biju Patnaik University of Technology Specialization: Computer Science EngineeringDownloads: 1931Views: 14118Uploaded: 6 months agoAdd to Favourite

Share it with your friends

Suggested Materials

Leave your Comments


Page-1 Java notes Genesis of JAVA 1. JAVA Syntax form C –language OOP’s concepts features are influences from C++ language. 2. C, Pascal , Basic, Fortran are the based on either compiler or interpreter. But Java programs are based on compiler and interpreter both. Source code Java Compiler Byte Code Java Interpreter Machine Code 3. Java program is platform independent to achieve the independency the Byte codes area used. 4. Java Compiler is generally known as JVM(Java Virtual Machine). Importance of java in Internet One of the java advantages for use over the Internet is that platform independent, meaning that programs created with the java can run on any machine (computers).Java achieves its independence by creating programs designed to run on the JVM rather than any specific computer system. These java programs once compiled and stored in a compact machine independent format known as Byte codes. Byte code files area easily transferred across the Internet. The preloaded interpreter run the byte code in the local machine then plays them. Java Features  Simple - Java's developers deliberately left out many of the unnecessary features of other high-level programming languages. For example, Java does not support pointer math, implicit type casting, structures or unions, operator overloading, templates, header files, or multiple inheritance.                                                                                                                                      Compiled by Umesh Sharma 
Page-2  Object-oriented. Just like C++, Java uses classes to organize code into logical modules. At runtime, a program creates objects from the classes. Java classes can inherit from other classes, but multiple inheritance, wherein a class inherits methods and fields from more than one class, is not allowed.  Statically typed - All objects used in a program must be declared before they are used. This enables the Java compiler to locate and report type conflicts.  Compiled - Before you can run a program written in the Java language, the program must be compiled by the Java compiler. The compilation results in a "byte-code" file that, while similar to a machine-code file, can be executed under any operating system that has a Java interpreter. This interpreter reads in the byte-code file and translates the byte-code commands into machine-language commands that can be directly executed by the machine that's running the Java program. You could say, then, that Java is both a compiled and interpreted language.  Multi-threaded - Java programs can contain multiple threads of execution, which enables programs to handle several tasks concurrently. For example, a multi-threaded program can render an image on the screen in one thread while continuing to accept keyboard input from the user in the main thread. All applications have at least one thread, which represents the program's main path of execution.  Garbage collector - Java programs do their own garbage collection, which means that programs are not required to delete objects that they allocate in memory. This relieves programmers of virtually all memory-management problems.  Robust - Because the Java interpreter checks all system access performed within a program, Java programs cannot crash the system. Instead, when a serious error is discovered, Java programs create an exception. This exception can be captured and managed by the program without any risk of bringing down the system.  Secure - The Java system not only verifies all memory access but also ensures that no viruses are hitching a ride with a running applet. Because pointers are not supported by the Java language, programs cannot gain access to areas of the system for which they have no authorization.  Extensible - Java programs support native methods, which are functions written in another language, usually C++. Support for native methods enables programmers to write functions that may execute faster than the equivalent functions written in Java. Native methods are dynamically linked to the Java program; that is, they are associated with the program at runtime. As the Java language is further refined for speed, native methods will probably be unnecessary.                                                                                                                                      Compiled by Umesh Sharma 
Page-3  Well-understood - The Java language is based upon technology that's been developed over many years. For this reason, Java can be quickly and easily understood by anyone with experience with modern programming languages such as C++.  Java Applet – Another advantage of the java for the internet is its ability to create applets. Applets are the small programs that area designed to be embedded into a Web Page. Any java enabled browser can load and run java applets directly form web pages. Applets provide World Wide Web pages with a level of interactivity never before possible on the internet. Object Oriented Programming Concepts What is the encapsulation? Definition :Encapsulation is the process of compartmentalizing the elements of an abstraction that constitute its structure and behavior; encapsulation serves to separate the contractual interface of an abstraction and its implementation. Encapsulation is the combining of data and the code that manipulates that data into a single component-that is, an object. Encapsulation also refers to the control of access to the details of an object’s implementation. //Example of Encapsulation class data { int idno, mark; String name; data() { name="raja"; idno=12; mark=900; } void show() { System.out.println("Name of student is="+name); System.out.println("id no. of student is="+idno); System.out.println("mark of student is="+mark); } } class firstdemo { public static void main(String args[]) { data obj=new data(); obj.show(); } }                                                                                                                                      Compiled by Umesh Sharma 
Page-4 Save this program named as firstdemo.java and complied as c:\>javac firstdemo.java and run above program as c:\>java firstdemo {where object call the data with the help of objects} What is the Polymorphism? Polymorphism build in two words =poly+morphism here poly means many and morphism means shapes. And merely means using the same one name to refer to different methods. “Name Reuse” would be better terms to use for polymorphism. There are two types of polymorphism in java. 1. Overloading:(Same name but different parameter) In java, it is possible to create methods that have same name but different definition. That is called method overloading. Method overloading is used where objects area required performing similar task but using different input parameters. When we call a method in an object, JVM matches up the method name first and then the number and type of parameters to decide which one of the definition to execute. //Example of overloading. class student { int idno, mark; String name; void setData() { idno=90;mark=89;name="raja"; } void setData(int id,int m,String n) { idno=id;mark=m;name=n; } void show() { System.out.println("\n\nStudent RollNo.="+idno); System.out.println("Student Name.="+name); System.out.println("Student RollNo.="+mark); } }//end of student class class demoOverload { public static void main(String args[]){ student obj=new student(); obj.setData(); obj.show(); student obj1=new student(); obj1.setData(1,67,"vinu"); obj1.show(); } }                                                                                                                                      Compiled by Umesh Sharma 

Lecture Notes