Vous êtes sur la page 1sur 26

JAVA Using NOTEPAD

Submitted to:
Mr Amit Monga

Submitted by:
Lavish Bansal

HISTORY OF C,C++ AND JAVA

1969-1973
Denis Ritche
A T & T Bell Lab
Windows

C++

1975-1984
Bjarne
StrausStrup
Windows

JAVA
1991-1995
James Gosling
Sun Microsystems
Oracle

FEATURES OF OOPS
It has 4 main features:>Inheritance
>Encapsulation
>Polymorphism
>Abstraction

What is JAVA ?
A general purpose object-oriented Programing
language.
Write Once Run Anywhere (WORA).
Designed for easy web/internet applications
Widespread acceptance
It is case sensitive language.
It is developed and deployed.

Why JAVA important ?


Two reasons :
Trouble with C/C++ language is that they are
not portable and are not platform
independent languages.
Emergence of World Wide Web, which
demanded portable programs
Portability and security necessitated the
invention of Java.

New features added in Java


Multithreading, that allows two or more pieces of
the same program to execute concurrently.
C++ has a set of library functions that use a
common header file. But java replaces it with its
own set of API classes.
It adds packages and interfaces.
Java supports automatic garbage collection.
Break and continue statements have been
enhanced in java to accept labels as targets.
The use of unicode characters ensures portability.

Features that differ:


Though C++ and java supports Boolean data type,
C++ takes any nonzero value as true and zero as
false. True and false in java are predefined literals
that are values for a boolean expression.
Java has replaced the destructor function with a
finalize() function.
C++ supports exception handling that is similar to
java's. However, in C++ there is no requirement
that a thrown exception be caught.

DATATYPE
Basically Data type is which type of value store
in variable.
SYNTAX <datatype> <variablename>;
Int age;
It is of 2 types:
Primitive(Pre defined) and Reference(User
defined).

ARRAYS
Array is a collection of variables of same
datatype.
SYNTAX <datatype> [] <array name> = new
<datatype> [];
Example:- int [] arr=new int [5];

PACKAGES
Packages are collection of classes or it act as a
container for different classes.
How to create a package?
PACKAGE <package name>;
class abc
{
};

CONSTRUCTOR
Class name and constructor name must be same.
It do not have any return type.
It is called when we create object of class.
It is used to initialise and store the variable.
Class hh
{
int a;
hh()
{a=10;
}

INHERITANCE
It is basically reusability ,like parent->child ,
super class->sub class.
SYNTAX :- extends <class name>
Type of inheritance:>single
>multilevel
>multiple

Overriding & overloading


Overriding is function name and function
signature should be same.
Overlaoding is function name is same and
function signature is different.

SUPER KEYWORD AND SUPER


METHOD
A subclass method can invoke a superclass
method using the super keyword.
Super method is used for invoking parent
parameter constuctor.
Example:-

class person
{
String firstname;
String lastname;
person(String fname,String lname)
{firstname=fname;
lastname=lname;
}
void display()
{System.out.println("person:");
System.out.println("firstname:"+firstname);
System.out.println("lastname:"+lastname);
}
}
class student extends person
{
int id;
String standard;
String instructor;
student(String fname,String lname,int nld,String stnd,String instr)
{
super(fname,lname);
id=nld;
standard=stnd;

instructor=instr;
}
void display()
{
System.out.println("student:");
super.display();
System.out.println("id:"+id);
System.out.println("standard:"+standard);
System.out.println("instructor:"+instructor);
}
}
class teacher extends person
{
String mainsubject;
String type;
int salary;

teacher(String fname,String lname,String sub,int slry,String stype)


{
super(fname,lname);
mainsubject=sub;
salary=slry;

type=stype;
}
void display()
{
System.out.println("teacher:");
super.display();
System.out.println("mainsubject:"+mainsubject);
System.out.println("salary:"+salary);
System.out.println("type:"+type);
}
}
class inheritancedemo
{
public static void main(String arr[])
{
person pobj=new person("rayan","miller");
student sobj=new student("jacob","smith",1,"1-b","roma");
teacher tobj=new teacher("daniel","mart","english",6000,"primary teacher");
pobj.display();
sobj.display();
tobj.display();
}}

ABSTRACT CLASS
Abstract is a keyword used along with classes
and methods.
In this we only declare the methods , we
define these methods in there child class.
Abstract class use only abstract methods.
Example:-

Abstract class dd
{
public abstract void a();
public abstract void b();
}
class s extends dd
{
public void a()
{
int a=10;
System.out.println(a);
}
public void b()
{int bb=10;
System.out.println(bb);
public static void main(String arr[])
{
s.b=new s();
b.a();
b.bb();
}}

INTERFACE
It is used for multiple inheritance.
In interface we only declare the methods , we define
these methods in there child class.
Example:Interface kk
{
}
Interface gg
{
}
Class s implements kk,gg

EXCEPTION HANDLING
Basically we have 3 types of errors:>Syntax error- occurs at compilation time
>Logical error- can occur at compile time or run
time
>Runtime error- basically unexpected
So to resolve this we use exception handling.
It has 3 blocks:> Try used where exception can occur.
> Catch used to handle the exception.
>Finally always run.

EXAMPLE OF EXCEPTION HANDLING

class ex
{
public static void main(String[]arr)
{
try
{
String aa=arr[0];
int a=Integer.parseInt(arr[1]);
int b=Integer.parseInt(arr[2]);
int c=a/b;
System.out.println(c);
System.out.println(aa);
System.out.println(b);
}
catch(Exception e)
{
System.out.println(e);
}

USER DEFINED EXCEPTION


It is when the user defined his/her defines
there own exception.
It uses 2 keywords throw and throws.
Throws clause is used to declare an exception
and throw keyword is used to throw an
exception explicitly.
The keyword throw is used inside method
bodyand throws clause is used in method
declaration.

GUI
Defined as graphical user interface.
Till now we had used it on black command
window but from now we will se how it will
work and we will visualise it .
Example:- import java. Awt.*;
Awt=abstract window toolkit
The child class of awt is swingx
Example:-import javax.swing.*;

CONSTRUCTION OF FRAME
Import javax.swing.*;
Class cont
{
Jframe f;
Jbutton b;
cont()
{
f=new Jframe (welcome);
b=new Jbutton(submit);
}
Public void aa()
{
f.add(b);
f.setSize(100,100);
f.setvisible(true);
Public static void main(String arrr[])
{cont v=new cont();
v.aa();
}}

Thank You

Vous aimerez peut-être aussi