Upload
sahil-ahuja
View
215
Download
0
Embed Size (px)
Citation preview
7/28/2019 Java First 20
1/20
Java (Introduction)
7/28/2019 Java First 20
2/20
Introduction
Another computer language but with a difference
language that is purely object-oriented
Best features of many existing languages such asC 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
7/28/2019 Java First 20
3/20
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
7/28/2019 Java First 20
4/20
Java features
Compiled and Interpreted
Platform independent & Portable
Object-Oriented Robust & Secure
Distributed
Simple , Small & Familiar
Multithreaded & Interactive High Performance
Dynamic & Extensible
7/28/2019 Java First 20
5/20
Java Vs C++
Java doesnt support operator overloading
Java doesnt have template classes as in C++
Java does not support multiple inheritance ofclasses. This is accomplished using interface
Java does not support global variables. Everyvariable and method is declared within a class and
forms part of that class Java doesnt use pointers
There are no header files in Java
7/28/2019 Java First 20
6/20
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)
7/28/2019 Java First 20
7/20
Simple Java Program
class SampleOne
{
public static void main(String args[])
{
System.out.println(Java is better than C++);
}}
7/28/2019 Java First 20
8/20
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);
}
}
7/28/2019 Java First 20
9/20
An application with two classesclass 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);
}
}
7/28/2019 Java First 20
10/20
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
7/28/2019 Java First 20
11/20
Implementing a Java Program
Creating the program
Compiling the program
Running the program
7/28/2019 Java First 20
12/20
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 .javaextension
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 typejava Test
Now, the interpreter looks for the main method on the program and begins
execution from there.
7/28/2019 Java First 20
13/20
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)
7/28/2019 Java First 20
14/20
Defining a subclass
class subclassname extends superclassname
{
variables declaration ;
methods declaration;
}
7/28/2019 Java First 20
15/20
Application of single inheritanceclass 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);
}
}
7/28/2019 Java First 20
16/20
Subclass Constructor
A subclass constructor uses the keyword super toinvoke the constructor method of the superclass.
Super may only be used within a subclassconstructor method
The call to superclass constructor must appear asthe first statement within the subclass constructor
The parameter in the super call must match theorder and type of the instance variable declared inthe superclass
7/28/2019 Java First 20
17/20
Final Variables and Methods
All methods and variables can beoverridden by default in subclasses. If we
wish to prevent the subclasses fromoverriding the members of the superclass,we will use keyword final with theredeclaration
final int SIZE=100;
final void showstatus() {}
7/28/2019 Java First 20
18/20
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
7/28/2019 Java First 20
19/20
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 directly2. 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
P i t P bli P t t d
7/28/2019 Java First 20
20/20
Private, Public, Protected
public protected Friendly
(default)
private
protectedprivate
Same class yes yes yes yes yes
Subclass in
same
package
yes yes yes yes no
Other class
in same
package
yes yes yes no no
Subclass in
otherpackages
yes yes no yes no
Non-
subclasses
in other
packages
yes no no no no