Monday, January 15, 2007

Basics of Java, OOPS and Networking Protocols

Java, OOPS and Networking Protocols
Topic : Basics of Java, OOPS and Networking Protocols
Venue and audience : At a Professor’s house , his daughter who
finished engineering
Date: 24-09-2006
Resources used: IBM GREATMINDS CD and notes of my friend

Java Basics:à
With the Java(TM) programming language, every computer program must define one or more user-defined data types via the class construct. For example, to create a program that behaves like a dog, we can define a class that (minimally) represents a dog: class Dog { void bark() { System.out.println("Woof."); }}
This user-defined data type begins with the keyword class, followed by the name for the data type, in this case, Dog, followed by the specification of what it is to be a dog between opening and closing curly brackets. This simple example provides no data fields, only the single behavior of barking, as represented by the method bark().
A method is the object-oriented equivalent of a procedure in nonobject-oriented languages. That is, a method is a program construct that provides the mechanism (method) for performing some act, in this case, barking. Given an instance of some entity, we invoke behavior with a dot syntax that associates an instance with a method in the class definition

Creating a class instance
public class ADogsLife { public static void main(String[] args) { Dog dog = new Dog(); dog.bark(); System.exit(0); }}
Method overloading
class Dog { void bark() { System.out.println("Woof."); } void bark(String barkSound) { System.out.println(barkSound); }}


Method overloading

Methods have same name but perform different operations
public class DogChorus { public static void main(String[] args) { Dog fido = new Dog(); Dog spot = new Dog(); fido.bark(); spot.bark("Arf. Arf."); fido.bark("Arf. Arf."); System.exit(0); }}

int sum(int a, int b,int c)

{
c= a+b;
return c;

}

Instance variables(barksound is instance variable) class Dog { String barkSound = new String("Woof."); void bark() { System.out.println(barkSound); } void bark(String barkSound) { System.out.println(barkSound); }}


class

class is a template which specifies the structural information about the real world


class classname

{ type var;
}



returntype method( parameter)

{
…..
}

objects

to access members of a class an object must be defined. When object is created memory will be allocated


constructor

constructor is a method with the class name that will be executed when you create the object
the difference between constructor and method is that constructor can not return value but method can return value



class xyz
{

xyz()

{

sop(“object created”);
}
}




class abc

{

xyz obj;

obj = new xyz;

}

Abstraction

Access Methods
In order for the value of an instance variable to vary over time, we must supply a method to change its value; such a method is typically referred to as an access method. By convention, a method that's provided simply to affect a change to an instance variable's value begins with the word "set": void setBark(String barkSound) { this.barkSound = barkSound; }

public class DogChorus { public static void main(String[] args) { Dog fido = new Dog(); fido.setBark("Ruff."); fido.bark(); System.exit(0); }}

this.barkSound = barkSound;
replaces the current value of the instance variable (this.barkSound with the new value passed as an argument (barkSound) to setBark().

Garbage collection finalize method free() new delete

Whatever is the purpose of malloc(), calloc(), free(), realloc() in C i.e. dynamic
memory allocation or during runtime same is the purpose of new and delete in C++ and garbage collector and new in Java while finalize is a method which is invoked before garbage collection.


Applets

The Java(TM) programming language is powerful and elegant. Ironically, however, many people think of it only in terms of its use for developing applets. In reality, the Java programming language is becoming the language of choice for a broad range of other development areas. Nevertheless, applets play an role important in many intranet environments because they provide an (elegant) way of implementing Web-based user interfaces to enterprise-wide computing services.
An applet is an instance of a user-defined class that specializes (inherits from) Applet (java.applet.Applet). Class inheritance is beyond the scope of this tutorial, but, for now, to specialize a class is to extend its capabilities. Applet is a placeholder class with an empty paint() method. Thus, to develop a minimal applet that displays in a portion of a Web browser window, you implement a paint() method that renders graphical output.
Applets employ the Java Abstract Windowing Toolkit (AWT) for the Graphics class, which provides drawing primitives, as well as for GUI components such as Button and TextField. With these components it's straightforward to design graphical forms-entry utilities that corporate-wide users access from a Web browser.
Although applet programmers often develop task-specific implementations of several methods such as init(), start(), stop() that control the applet lifecycle in the browser window, a minimal example with init() and paint() is sufficient here. DogApplet.java implements a simple applet that renders a graphical barking message: import java.awt.*;import java.applet.Applet;public class DogApplet extends Applet { public void init() { setBackground(Color.pink); } public void paint(Graphics g) { g.drawString("Woof!", 10, 20); }}
init() set the background to an uncommon color to ensure that its allocated browser window area is visible. Java-enabled Web browsers execute init() only once, and prior to other methods. paint() uses the Graphics instance, passed as an argument by the browser environment, to draw a string at coordinates (10, 20) relative to the applet's window area.
To specify an applet in a Web page, you must provide an HTML applet tag that specifies the class file (code="class-file") and its relative location (codebase="location"), as well as a width and height request for the applet's window area relative to other components in the Web page. For example, this document includes the following applet tag:
In processing this tag the Web browser:
Loads the DogApplet class file
Allocates its area in the window
Instantiates DogApplet
Executes prescribed methods such as init()
DogApplet appears as follows:
.


public static void main()


main() -à starting point of the program

static-àobject need not be created

public --à can access anywhere


encapsulation

it means wrapping of data into a single unit to hide or to safeguard from the outside world.

It means combining elements to create a new entity

A procedure, a class, a record are an example of encapsulation

encapsulation means that the attributes (data) and the behaviors (code) are encapsulated in to a single object.

class CheckingAccount {

private double balance = 0;
public void setBalance(double bal) {
balance = bal;
};

public double getBalance(){

return balance;
};
}



class Encapsulation {

public static void main(String args[]) {
System.out.println("Starting myEncapsulation...");
CheckingAccount myAccount = new CheckingAccount();
myAccount.setBalance(40.00);
System.out.println("Balance = " + myAccount.getBalance());
}
}



data hiding one of the features of encapsulation

class Encapsulation {

public static void main(String args[]) {
System.out.println("Starting myEncapsulation...");
CheckingAccount myAccount = new CheckingAccount();
myAccount.balance = 40.00;
System.out.println("Balance = " + myAccount.getBalance());
}
}


public private protected are the access specifiers

Inheritance where in specific classes derive attributes from general classes

From Base class/super class attributes are derived by sub class, extends keyword is used to achieve inheritance in Java

Inheritance is used for reusability of code.





Class A

{

…….
}

class B extends A

{
……..

}


class A
{
int x;
int y;

int Add(int I, int j)
{
x=I;
y=j;
return(x+y)

}}


class B extends A

{

int z;

add(int I, int j, int k)

{
x=I;
y=j;
z=k;
s.o.p)x+y+z);
}
}


pvsm()
{

A objA = new A();

B objB = new B();
s.o.p(obj.Add(2,3));
}









method
a function that is part of a class
constructor
a special method that is called when an object is created
superclass
the class that is inherited from (the parent class)
subclass
the class the does the inheriting (the child class)
extends
in Java the keyword extends means that a class will inherit from another class
overload
a method is overloaded if there are two or more methods with the same name in a class. Each overloaded method has a different set of parameters. That's how you can tell which one will get called.
override
a method is overridden if there is a method in the subclass that has the same name and the same set of parameters. The superclass method is then NOT inherited


Multiple inheritance is achieved thru interfaces.

This continued and ended with a brief overview of protocols like TCP/IP, ARP/RARP, DHCP, ICMP/IGMP, RIP/OSPF/BGP, DNS, WEB APPLICATIONS, MVC ARCHITECTURE, LAMP/MAMP/WIMP, LUCID, RAD, SOA etc.

No comments: