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 .

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

Every variable and method is declared within a class and forms part of that class • Java doesn’t use pointers • There are no header files in Java .Java Vs C++ • Java doesn’t support operator overloading • Java doesn’t 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.

Java Environment Java Environment= JDK(javac.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) .javah.

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

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

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

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 .

class containing the bytecodes of the program. the javac comopiler creates a file called Test. At the command prompt type java Test Now.java If everything is ok.java extension Compile the program You must run the java compiler javac with the name of the source file you just created javac Test. Running the program Use java interpreter to run a stand-alone 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 . 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.

BedRoom( int x. int breadth. breadth=y.Application of single inheritance class Room { int length.y). int z) { super(x. Room( int x. int y) { length=x. } } . height=z. } } class BedRoom extends Room { int height. } int area( ) { return (length * breadth). } int volume() { return ( length * breadth * height). int y.

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 .

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

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 .

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

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 . Public.Private.

Sign up to vote on this title
UsefulNot useful