Sunteți pe pagina 1din 30

Drawbacks in procedure oriented programs (C)

Data restriction is not possible Extendibility wont possible User data types can not behave like primitive data types No default mechanism for handling errors at run time

A new approach object oriented programming


It provides some principles: Object Class Encapsulation Hiding Abstraction Polymorphism Inheritance Dynamic binding Message passing

Object: An instance of the class Class: Template or model of an object. Ex: student o1=new student(); (new uses for Dynamic Memory allocation) Encapsulation: Mechanism that binds together code and the data it manipulates and keeps both safe from outside influence and misuse Abstraction: Process of representing the essential by hiding the unwanted from the user

Object
If the class (student) consists of m1, m2, m3 (marks) variables and functions Total(), Avg(). Object comprises of functions and data it manipulates during its dynamic memory allocation
m1 m2 m3

Total( )
Avg( )

Polymorphism: Using same thing for different purposes


Inheritance: The process by which one object acquires the properties of another object Dynamic binding: Linking the function call to its definition at the time of execution Message passing: The process of communicating one object with the other by means of their functions and parameters

Features of Java
Simple OOPL Portable Robust Compilation and Interpretation Platform Independent Distributed Multi threaded Secured Improved Enhancement

Compilation and Interpretation

JAVA PROGRAM

Compiler

CLASS FILE

Interpreter

.EXE FILE

General Program Structure


Documentation (comments) Packaging statements Import statements class1, class2, class3, class4 , classn

- Out of n classes (one class should contain main function)

Constructors
Special method of the class Its name same as the class name Initializes an object (initializing member data of the object) Types of Constructors Default constructors Parameter constructors

Inheritance
Inheritance: The process by which one object acquires the properties of another object Types of Inheritance Single level Inheritance Multi level Inheritance Multiple Inheritance Hierarchal Inheritance Hybrid Inheritance

super passes values from derived class constructor to base class constructor final prefixed to class, method, data in order to prevent further class inheritance, method overloading, data variability.

Polymorphism achieved
@
Compilation time
Method Overloading

Operator Overloading
Run time

Method Overloading

Difference b/w function over loading and function over riding


Overloading deals with multiple methods in the same class with the same name but different method signatures
Overriding deals with two methods. One in the parent class and other in the child class and has the same name and signatures

Interface
It contains static final variable and abstract methods An interface can extend only one interface A class can extend only one class but it can implement more than one interface An interface can not implement other interface An interface can not extend a class Implementation class should provide definitions for all the methods of an interface

Types of classes
Simple class contains member data and member methods Inner class (or) nested class is defined with in another class Local class is defined with in a method Ananomus class is referred without an instance

Packages
Container which contains classes or interfaces provides the following features: 1)Reusability beyond the scope of the program 2)Avoids naming conflicts with respect to classes 3)Seperates coding and implementation

Types of Packages
- Predefined packages - User defined packages Predefined packages are a part of standard library which comes along with the software referred as APIs
Lang String util Date io Input net Socket awt Frame applet Applet sql Connection

By default Compiler import java.lang.*;

Visibility Mode (or) Access Specifier


A visibility mode or Access Specifier has key words which provides additional meaning. These are also used to impose some restrictions on variables, classes, methods

Visibility/ Classes Same Class


Sub Class Other Class Sub Class of Other program Other Class of Other program

public protected private default


YES YES YES YES YES YES NO YES YES NO NO NO YES YES YES NO

YES

NO

NO

NO

Exception Handling
Errors
Compile Time error Logic error Run Time error
Condition due to which run time error occurs is called Exception

Sudden termination of the program execution is called as abnormal termination


The mechanism which gives response to handle Exception is referred as Exception Handling

Exception Handling
Exception Handling
JVM Explicitly in the program (Key words) try catch finally throw throws

I/O Stream
Stream: Interface b/w program and I/O device Stream
Byte Streams InputStream, OutputStream Character Stream Reader, Writer I/P Device Input Stream/ Source Program O/P Device

Output Stream/ Destination

Stream Benefits
Provides a clean abstraction for a complex and often cumbersome task. Java programs written to adhere to the abstract highlevel stream classes (InputStream, OutputStream, Reader and Writer) will function properly even when new and concrete stream classes are invented. Works well, when we switched from a file based set of streams to the network and socket streams

Applets
Applet is Java class which is a part of web page and it is used to perform calculations or displaying animations dynamically on the web page Applet
Local Applet Developed and Used in a local system Remote Applet Developed in remote system and used in local system Requires Internet -URL (address of the applet)

Difference between Normal Application and Applet


Console Application -It contains main( ) Applet -Not contains main( ) - Contains init( ) -Executed using Java -Executed using Applet Viewer -Title management is -Title management is tough easy -Database programming - Database programming is tough is easy

Abstract Windowing Toolkit


Java provides a set of UI components as (follows), it enables a developer to develop (GUI) UI components in AWT - Containers & its components - Event system for handling events - Layout managers for positioning and aligning

Layout manager classes in the AWT


Flow Layout Grid Layout Border Layout Card Layout GridBag Layout (define more precise placement of UI components in Java applets)

Multi threaded Programming


Multi-Threading - The process of creating environment in order to execute its (programs) different sections (sub program) simultaneously Multi-Tasking - Modern OS (W-95) achieve this by switching its attention so rapidly between the different applications appear to be running simultaneously

Advantages of using threads


Can be created faster. Requires fewer overheads. Inter process communication is faster Context Switching is faster Maximum use of C.P.U time

Main difference b/w process and thread


Process is an execution of a program. Thread is a single execution sequence with in the process.

S-ar putea să vă placă și