Anda di halaman 1dari 20

Java (Introduction)

Introduction
Another computer language but with a difference language that is purely object-oriented Best features of many existing languages such as C and C++ and added a few new features to form a simple, easy to learn and object-oriented language Secure language, making it well suited for Internet programming

Introduction
Java has two lives
Stand-alone computer language for general-purpose programming Supporting language for internet programming General purpose programs are known as applications Programs written for Internet are known as applets

Java features
Compiled and Interpreted Platform independent & Portable Object-Oriented Robust & Secure Distributed Simple , Small & Familiar Multithreaded & Interactive High Performance Dynamic & Extensible

Java Vs C++
Java doesnt support operator overloading Java doesnt have template classes as in C++ Java does not support multiple inheritance of classes. This is accomplished using interface Java does not support global variables. Every variable and method is declared within a class and forms part of that class Java doesnt use pointers There are no header files in Java

Java Environment
Java Environment= JDK(javac,javah,java etc) + JSL or API
Java Environment includes a large number of development tools and hundreds of classes and methods The development tools are part of the system known as Java Development Kit(JDK) and the classes and methods are part of the Java Standard Library(JSL), also known as Application Programming Interface(API)

Simple Java Program


class SampleOne { public static void main(String args[]) { System.out.println(Java is better than C++); } }

More Example
import java.lang.Math; class SquareRoot { public static void main(String args[]) { double x=5; double y; y=Math.sqrt(x); System.out.println(y= +y); } }

An application with two classes


class Room { float length; float breadth; void getdata(float a, float b) { length=a; breadth=b; } } class RoomArea { public static void main(String args[]) { float area; Room room1=new Room(); room1.getdata(14,10); area=room1.length*room1.breadth; System.out.println(Area=+area); } }

Java Program Structure


Documentation Section Package Statement Import Statement Interface Statement Class Definitions Main Method Class { Main Method Definition }
Suggested

Optional
Optional Optional Optional

Essential

Implementing a Java Program


Creating the program Compiling the program Running the program

What to do?
Make sure JDK is properly installed in your system Make a program in a text editor(say notepad) Save your program with the filename same as the name of your class(class containing main method) and with .java extension Compile the program You must run the java compiler javac with the name of the source file you just created javac Test.java If everything is ok, the javac comopiler creates a file called Test.class containing the bytecodes of the program. Running the program Use java interpreter to run a stand-alone program. At the command prompt type java Test Now, the interpreter looks for the main method on the program and begins execution from there.

Inheritance: Extending a class


Single Inheritance ( only one super class) Multiple inheritance( several super classes) Hierarchical inheritance( one super class, many subclasses) Multilevel inheritance( Derived from a derived class)

Defining a subclass
class subclassname extends superclassname { variables declaration ; methods declaration; }

Application of single inheritance


class Room { int length; int breadth; Room( int x, int y) { length=x; breadth=y; } int area( ) { return (length * breadth); } }

class BedRoom extends Room { int height; BedRoom( int x, int y, int z) { super(x,y); height=z; }
int volume() { return ( length * breadth * height); } }

Subclass Constructor
A subclass constructor uses the keyword super to invoke the constructor method of the superclass. Super may only be used within a subclass constructor method The call to superclass constructor must appear as the first statement within the subclass constructor The parameter in the super call must match the order and type of the instance variable declared in the superclass

Final Variables and Methods


All methods and variables can be overridden by default in subclasses. If we wish to prevent the subclasses from overriding the members of the superclass, we will use keyword final with there declaration final int SIZE=100; final void showstatus() {}

Final Classes
We can prevent a class from being further subclassed using keyword final as follows: final class Employee{.} Any attempt to inherit this class will cause an error and the compiler wont allow it

Abstract Method and Classes


By using final---we prohibit overriding By using Abstract concept we make it mandatory Abstract class shape {. abstract void draw(); //method without implementation . } When a class contains one or more abstract methods, it should also be declared abstract. While using abstract classes: 1. we cannot use abstract classes to instantiate objects directly 2. The abstract methods of an abstract class must be defined in its subclass 3. We cannot declare abstract constructors or abstract static methods 4. Abstract class can contain methods with implementation

Private, Public, Protected


public protected Friendly
(default) Same class Subclass in same package

private protected

private
yes no

yes yes

yes yes

yes yes

yes yes

Other class in same package


Subclass in other packages Nonsubclasses in other packages

yes
yes yes

yes
yes no

yes
no no

no
yes no

no
no no

Anda mungkin juga menyukai