|
OBJECT ORIENTED PROGRAMMING
THROUGH JAVA
UNIT 1
IMPORTANT POINTS:
INTRODUCTION TO OOPS CONCEPTS:
All oops languages, including C++, share three common defining trails.
ENCAPSULATION
POLYMORPHISM
INHERITANCE
ENCAPSULATION:
It binds together code and the data it manipulates and keeps them safe from outside interference and misuse.
When data and code are linked together in this fashion, an object is created. In other words an object is the device that
supports encapsulation.
POLYMORPHISM:
The name itself suggests that “POLY” stands for many and “MORPHISM” stands for forms.
Polymorphism is the quality that allows one name to be used, for two or more related but technically different purposes.
In general giving multiple interfaces to a function or an operator is called “POLYMORPHISM”.
INHERITANCE:
Inheritance is the process by which one object can acquire the properties of another.
The best example for this is C , C++, and JAVA languages.
Java is being derived from C++ which in return is being derived from C .
So it inherits all the capabilities of C,C++ and also add the features of its own.
OBJECT-ORIENTED DEVELOPMENT IN JAVA:
Java is unusual for an oop’s language in that it is “objects all the way down”. Unlike C,C++, which is a
confusing combination of objects and functions, everything in java is an object.
Strings are objects, numbers are objects, threads are objects, even applets are objects. Because of this, java has all the
helpful features of object-oriented systems just described.
It’s core constructs of classes, objects, methods, and instance variables are, by there very nature, managed in a modular
fashion. Java’s support for inheritance allows you to build new classes from other classes. Each class you construct
becomes a tool that can be used to create yet more complex classes.
It has runtime garbage collector that removes objects from memory that you no longer need. No longer will your program run
out of memory because you forget to explicitly delete an object. The garbage collector, which your program doesn’t even
have to be aware of, does this for you.
Furthermore, java’s total orientation toward objects removes another construct that has been a blight of programmers-the
pointer.
Because of this combination of garbage collection and removal of the pointer construct, java has generally taken the problem
of memory management away from you.
USING METHODS:
A method has two parts: a definition and a body.
The definition must have at least two components: a return value, and a parameter list. These components define the signature
of a method.
The return value of a method can be a primitive data type (such as “int”), a class name (such as String), or ‘void’
if there is no return value.
A method has zero or more parameters. If there are no parameters, the method consists of empty parentheses; otherwise, the
parameters can be primitive data types or classes, separated by commas.
OVERLOADING METHODS:
As mentioned earlier, a method has a signature. This is important because java provides a mechanism for repeatedly using the
same method name; this mechanism is called overloading. To overload a method it has to follow these rules.
They should at least differ in any one of the following
The number of arguments
Type of arguments
Sequence of arguments
METHOD OVERRIDING:
Method overriding is used when we need a subclass to replace a method of its super class.
Recall that each method has a signature. When you override a method, you are defining a new method that replaces the method
in the super class and that has the same signature.
Whenever you call a method, java looks for a method of that signature in the class definition of that object If java doesn’t
find the method, it looks for a matching method until it reaches the topmost super class.
APPLETS:
An applet is a java program that runs in the applet viewer (a test utility for applets that is included with the j2sdk) or
a “world wide web” browser such as “netscape communicator” or “Microsoft internet explorer”.
The appletviewer (browser) executes an applet when a hypertext markup language(HTML) document containing the applet is opened
in the applet viewer (browser).
IMPORTANT BITS
● Java originated at sun Microsystems as a project for intelligent consumer electronic devices.
● Java program files end with the “.java” extension.
● Java program consists of pieces called “Classes”. Classes consists of pieces called “Methods”
that perform task and return information when they complete their tasks.
● Java program goes through five phases to be executed –edit, compile, load, verify and execute.
● The company that popularized personal computing was “Apple”.
● The appletviewer command from the Java 2 Software Development Kit executes a Java Applet.
● The java command from the Java 2 Software Development Kit executes a java application.
● An HTML file is required to invoke a Java Applet.
● JDBC provides access to relational database in java.
● List and tables of values are called “Arrays”.
● The file produced by the java compiler contains “Byte-code” that are interpreted to execute a Java Applet
or application.
● The programs that translate high-level language programs into machine language are called “Compilers”.
UNIT 2
IMPORTANT POINTS:
OBJECT ORIENTED PROGRAMMING:
OOP is a new way to approach the task of programming. Since its early beginnings, it has been governed by several methodologies.
Actually the very concept of object oriented programming has started long back before C has been invented. To allow more complex
programs to be written OOP has been invented. The approach to this OOP is some times called as “Modular Programming”.
In this we have to identify the module and position the program such that the data is been hidden inside that module.
CONSTRUCTORS:
As stated earlier, constructors are a special type of methods, which are invoked automatically when an object is being created;
their syntax is similar to that of methods, except they don’t return values.
The name of a constructor is the same as its class. A constructor is automatically called when an object is instantiated.
There are three types of constructors. They are Default, Parameterized, and Copy constructors.
PACKAGE:
Packages are containers for classes that are used to keep the class name space compartmentalized. For example, a package allows
you to create a class named List, which you can store in your own package without concern that it will collide with some other
class named List elsewhere.
The general form of package statement:
Package Pkg;
Here, Pkg is the name of the package.
INTERFACE:
Using the keyword “interface”, you can fully abstract a class interface from its implementation. Using interface,
you can specify what a class must do, but not how it does it.
Interfaces are syntactically similar to classes, but they lack instance variables, and their methods are declared with out
any body.
Interfaces are designed to support “dynamic method resolution” at run time.
By providing the “interface” keyword, Java allows you to fully utilize the “one interface, multiple methods”
aspect of polymorphism.
Once an interface is defined, one or more classes can implement that interface.
DYNAMIC METHOD DISPATCH:
Method overriding forms the basis for one of java’s most powerful concepts: “dynamic method dispatch”.
Dynamic Method Dispatch is the mechanism by which a call to an overridden function is resolved at run time, rather than compile
time. Dynamic method dispatch is important because this is how java implements “run-time polymorphism”.
WRAPPER CLASSES:
Java provides classes that correspond to each of the simple types. In essence, these classes encapsulate, or wrap, the simple
types with in a class. Thus, they are economically referred to as “Type Wrappers”.
STRING BUFFER:
String represents fixed length, immutable character sequence.
String Buffer represents growable and writeable character sequences. String buffer may have characters and sub strings inserted
in the middle or appended to the end.
String buffer will automatically grow to make room for such additions and often has more characters preallocated than are
actually needed , to allow room for growth.
String Buffer Constructors:
StringBuffer( )
StringBuffer(int size)
StringBuffer(String str)
ABSTRACT CLASSES:
When we think of a class as a type, we assume that objects of that type will be initiated. However, there are cases in which
it is useful to define classes for which the programmer never intends to instantiate any objects. Such classes are called
as Abstract Classes.
Classes from which objects can be instantiated are called as Concrete Classes. A class is made abstract by declaring it with
keyword “abstract”.
IMPORTANT POINTS :
An inner class can be declared static.
A class is made abstract by declaring the keyword abstract.
Inner class definitions are used mainly in event handling.
Compiling a class that contains inner classes results in a separate “.class file” for every class.
An object of a sub class can be treated as an object of its corresponding super class.
A method call resolved at run-time is referred to as Dynamic Binding.
If a class contains one or more abstract methods, it is an Abstract Class.
An object of a sub class can be treated as an object of its corresponding super class. But vice-versa is not true.
A reference to a sub class object may be implicitly converted to a reference for a super class object.
Classes from which objects can be instantiated are called “Concrete Classes”.
An Anonymous Inner Class can implement an Interface or “extend” a class.
To access outer classess “this” reference is used . It is written as “ OuterClassName.this”.
The outer class is responsible for creating objects of its non-static inner class.
UNIT 3
IMPORTANT POINTS:
EVENTS:
An “event” is an object that describes a state change in a source.
Some of the activities that cause events to be generated are -- “pressing a button”, “entering a character
via the keyboard”, “selecting an item in a list”, or “clicking the mouse”.
EVENT SOURCES:
A “Source” is an object, which generates an event. This occurs when the internal state of that object changes
in some way.
Sources may generate more than one type of event.
EVENT LISTENERS:
A “Listener” is an object that is notified when an event occurs.
It has two major requirements. First, it must have been registered with one or more sources to receive notifications about
specific type of events. Second, it must implement methods to receive and process these notifications.
EVENT CLASSES:
Action Event Generated when a button is pressed, a list item is
double -clicked, or a menu item is selected.
Adjustment Event Generated when a scroll bar is manipulated.
Component Event Generated when a component is hidden, moved,
resized, or becomes visible.
Container Event Generated when a component is added to or
removed from a container.
Focus Event Generated when a component gains or losses
keyboard focus.
Input Event Abstract super class for all component input
event class.
Item Event Generated when a check box or list item is
clicked; also occurs when a choice selection
is made or a checkable menu item is selected
or deselected.
Key Event Generated when input is received from the
keyboard.
Mouse Event Generated when the mouse is dragged, moved,
clicked, pressed, or released; also generated
when the mouse enters or exits a component.
Text Event Generated when the value of a text area or text
field is changed.
SOURCE OF EVENTS:
The event sources are:
Buttons
Checkbox
Choice
List
Menu item
Scroll bar
Text components
Windows
EVENT LISTENER INTERFACE:
The listener interfaces are:
Action listener
Adjustment listener
Component listener
Container listener
Focus listener
Item listener
Key listener
Mouse listener
Mouse Motion listener
Text listener
Window listener
CONTROL FUNDAMENTALS:
The AWT supports the following types of controls:
Labels
Push buttons
Check boxes
Choice buttons
Lists
Scroll bar
Text editing
LAYOUT MANAGER:
A layout manager automatically arranges your controls with in a window by using some type of algorithm.
We have four types of layout managers. They are:
FLOW LAYOUT:
Flow layout is the default layout manager. Flow layout implements a simple layout style, which is similar to how words flow
in a text editor. Components are laid out from the upper-left corner, left to right and top to bottom.
The constructors for flow layout:
FlowLayout( )
FlowLayout( int how)
FlowLayout ( int how, int horz, int vert)
BORDER LAYOUT:
GRID LAYOUT:
Grid layout lays out components in a two dimensional grid. When you instantiate a Grid layout, you define the number of rows
and columns.
The constructors supported by Grid layout are shown here:
GridLayout( )
GridLayout(int numrows, int numcoloumns)
GridLayout(int numrows, int numcoloumns, int horz,int vert)
CARD LAYOUT:
The card layout is unique among the other layout managers in that it stores several different layouts.
Each layout can be thought of as being on a separate index card in a deck that can be shuffled so that any card is on top
at a given time.
Card layout provides two constructors:
CardLayout( )
CardLayout(int horz,int vert)
|