SlideShare uma empresa Scribd logo
1 de 52
Unit-1
7/14/2017 1Ms SURBHI SAROHA(Asst.Professor)
Contents
 Introduction
 Data types
 Control structured
 Arrays
 Strings
 Vector
 Classes( Inheritance , package ,
exception handling)
 Multithreaded programming
7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 2
Java – Overview
 Java programming language was
originally developed by Sun
Microsystems which was initiated by
James Gosling and released in 1995.
 The latest release of the Java Standard
Edition is Java SE 8.
 Java is guaranteed to be Write Once,
Run Anywhere .
7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 3
Java is:
 Object Oriented: In Java, everything is an Object. Java can
be easily extended since it is based on the Object model.
 Platform Independent: Unlike many other programming
languages including C and C++, when Java is compiled, it
is not compiled into platform specific machine, rather into
platform independent byte code. This byte code is
distributed over the web and interpreted by the Virtual
Machine (JVM) on whichever platform it is being run on.
 Simple: Java is designed to be easy to learn. If you
understand the basic concept of OOP Java, it would be
easy to master.
 Secure: With Java's secure feature it enables to develop
virus-free, tamper-free systems. Authentication techniques
are based on public-key encryption.
7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 4
Cont…..
 Architecture-neutral: Java compiler generates an architecture-
neutral object file format, which makes the compiled code
executable on many processors, with the presence of Java
runtime system.
 Portable: Being architecture-neutral and having no
implementation dependent aspects of the specification makes
Java portable.
 Robust: Java makes an effort to eliminate error prone situations
by emphasizing mainly on compile time error checking and
runtime checking.
 Multithreaded: With Java's multithreaded feature it is possible to
write programs that can perform many tasks simultaneously. This
design feature allows the developers to construct interactive
applications that can run smoothly.
7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 5
Cont…
 Interpreted: Java byte code is translated on the fly to native
machine instructions and is not stored anywhere. The
development process is more rapid and analytical since the
linking is an incremental and light-weight process.
 High Performance: With the use of Just-In-Time compilers,
Java enables high performance.
 Distributed: Java is designed for the distributed
environment of the internet.
 Dynamic: Java is considered to be more dynamic than C or
C++ since it is designed to adapt to an evolving
environment. Java programs can carry extensive amount of
run-time information that can be used to verify and resolve
accesses to objects on run-time.
7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 6
First Java Program
 public class MyFirstJavaProgram {
 /* This is my first java program.
 * This will print 'Hello World' as the output
 */
 public static void main(String []args) {
 System.out.println("Hello World"); // prints
Hello World
 }
 }
7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 7
Setting Up the Path for
Windows
 Assuming you have installed Java in
c:Program Filesjavajdk directory:
 Right-click on 'My Computer' and select
'Properties'.
 Click the 'Environment variables' button under
the 'Advanced' tab.
 Now, alter the 'Path' variable so that it also
contains the path to the Java executable.
Example, if the path is currently set to
'C:WINDOWSSYSTEM32', then change your
path to read
'C:WINDOWSSYSTEM32;c:Program
Filesjavajdkbin
7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 8
Save the file, compile and run
the program
 Open notepad and add the code as above.
 Save the file as: MyFirstJavaProgram.java.
 Open a command prompt window and
. Java – Basic Syntax
 go to the directory where you saved the
class. Assume it's C:.
7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 9
Cont….
 Type 'javac MyFirstJavaProgram.java' and
press enter to compile your code. If there are
no errors in your code, the command prompt
will take you to the next line (Assumption : The
path variable is set).
 Now, type ' java MyFirstJavaProgram ' to run
your program.
 You will be able to see ' Hello World ' printed
on the window.
7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 10
Cont….
 C:> javac MyFirstJavaProgram.java
 C:> java MyFirstJavaProgram
 Hello World
7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 11
Basic Syntax
 Case Sensitivity - Java is case sensitive, which means identifier Helloand hello
would have different meaning in Java.
 Class Names - For all class names the first letter should be in Upper Case. If
several words are used to form a name of the class, each inner word's first letter
should be in Upper Case.
 Example: class MyFirstJavaClass
 Method Names - All method names should start with a Lower Case letter. If
several words are used to form the name of the method, then each inner word's
first letter should be in Upper Case.
 Example: public void myMethodName()
 Program File Name - Name of the program file should exactly match the class
name. When saving the file, you should save it using the class name (Remember
Java is case sensitive) and append '.java' to the end of the name (if the file name
and the class name do not match, your program will not compile). Example:
Assume 'MyFirstJavaProgram' is the class name. Then the file should be saved
as 'MyFirstJavaProgram.java'
7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 12
Java is an Object-Oriented
Language
 Java supports the following fundamental
concepts:
 Polymorphism
 Inheritance
 Encapsulation
 Abstraction
 Classes
 Objects
 Instance
 Method
 Message Parsing
7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 13
Classes in Java
 A class is a blueprint from which individual objects
are created.
 public class Dog{
 String breed;
 int ageC
 String color;
 void barking(){
 }
 void hungry(){
 }
 void sleeping(){
 }
 }
7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 14
Constructors
 The main rule of constructors is that they should
have the same name as the class. A class can have
more than one constructor.
 Following is an example of a constructor:
 public class Puppy{
 public Puppy(){
 }
 public Puppy(String name){
 // This constructor has one parameter, name.
 }
 }
7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 15
Basic Datatypes
 There are two data types available in
Java:
 Primitive Datatypes
 Reference/Object Datatypes
7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 16
Primitive Datatypes
 Byte
 short
 int
 long
 float
 double
 boolean
 char
7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 17
Reference Datatypes
 A reference variable can be used to
refer any object of the declared type or
any compatible type.
 Example: Animal animal = new
Animal("giraffe");
7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 18
Java Access Modifiers
 The four access levels are:
 Visible to the package, the default. No
modifiers are needed.
 Visible to the class only (private).
 Visible to the world (public).
 Visible to the package and all
subclasses (protected).
7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 19
Abstract Class
 An abstract class can never be
instantiated. If a class is declared as
abstract then the sole purpose is for the
class to be extended.
 A class cannot be both abstract and final
(since a final class cannot be extended). If
a class contains abstract methods then the
class should be declared abstract.
Otherwise, a compile error will be thrown.
 An abstract class may contain both
abstract methods as well normal methods.
7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 20
Example
 abstract class Caravan{
 private double price;
 private String model;
 private String year;
 public abstract void goFast(); //an
abstract method
 public abstract void changeColor();
 }
7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 21
Loop Control
7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 22
Decision Making
7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 23
Cont….
7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 24
Strings Class
 Strings, which are widely used in Java
programming, are a sequence of
characters.
 Creating Strings
 The most direct way to create a string is
to write:
 String greeting = "Hello world!";
7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 25
Example
 public class StringDemo{
 public static void main(String args[]){
 char[] helloArray = { 'h', 'e', 'l', 'l', 'o', '.'};
 String helloString = new
String(helloArray);
 System.out.println( helloString );
 }
 }
7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 26
Output
 This will produce the following result:
 hello.
7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 27
String Length
 public class StringDemo {
 public static void main(String args[]) {
 String palindrome = "Dot saw I was Tod";
 int len = palindrome.length();
 System.out.println( "String Length is : " +
len );
 }
 }
 This will produce the following result:
 String Length is : 17
7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 28
Concatenating Strings
String compareTo(String anotherString)
Method
 The String class includes a method for
concatenating two strings:
 string1.concat(string2);
7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 29
Arrays
 Java provides a data structure, the
array, which stores a fixed-size
sequential collection of elements of the
same type.
 An array is used to store a collection of
data, but it is often more useful to think
of an array as a collection of variables of
the same type.
7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 30
Java – Exceptions
 An exception (or exceptional event) is a
problem that arises during the execution
of a program. When an Exception
occurs the normal flow of the program is
disrupted and the program/Application
terminates abnormally, which is not
recommended, therefore, these
exceptions are to be handled.
7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 31
Exception Hierarchy
7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 32
Catching Exceptions
 A method catches an exception using a
combination of the try and catch
keywords.
 try
 {
 //Protected code
 }catch(ExceptionName e1)
 {
 //Catch block
 }
7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 33
Java – Inner Classes
 Nested Classes
 In Java, just like methods, variables of a
class too can have another class as its
member. Writing a class within another
is allowed in Java. The class written
within is called the nested class, and
the class that holds the inner class is
called the outer class.
7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 34
Syntax to write a nested
class
 Here, the class Outer_Demo is the
outer class and the class Inner_Demo
is the nested class.
 class Outer_Demo{
 class Nested_Demo{
 }
 }
7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 35
Java – Inheritance
 The class which inherits the properties
of other is known as subclass (derived
class, child class) and the class whose
properties are inherited is known as
superclass (base class, parent class).
 extends Keyword
 extends is the keyword used to inherit
the properties of a class. Following is
the syntax of extends keyword.
7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 36
Syntax of extends keyword
 class Super{
 .....
 .....
 }
 class Sub extends Super{
 .....
 .....
 }
7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 37
Types of Inheritance
7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 38
Cont….
7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 39
Note:-
 A very important fact to remember is that
Java does not support multiple
inheritance. This means that a class
cannot extend more than one class.
7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 40
Java – Packages
 A Package can be defined as a grouping
of related types (classes, interfaces,
enumerations and annotations )
providing access protection and
namespace management.
 Some of the existing packages in Java are:
 java.lang - bundles the fundamental
classes
 java.io - classes for input, output
functions are bundled in this package
7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 41
Creating a Package
 While creating a package, you should
choose a name for the package and
include a package statement along with
that name at the top of every source file
that contains the classes, interfaces,
enumerations, and annotation types
that you want to include in the package.
 The package statement should be the first
line in the source file. There can be only
one package statement in each source file,
and it applies to all types in the file.
7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 42
Package example
 Following package example contains
interface named animals:
 /* File name : Animal.java */
 package animals;
 interface Animal {
 public void eat();
 public void travel();
 }
7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 43
Exception Handling
 A Java exception is an object that
describes an exceptional (that is, error)
condition that has occurred in a piece of
code.
 Java exception handling is managed via
five keywords: try, catch, throw,
throws, and finally.
7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 44
General form of an exception-
handling block:
try {
 // block of code to monitor for errors
 }
 catch (ExceptionType1 exOb) {
 // exception handler for ExceptionType1
 }
 catch (ExceptionType2 exOb) {
 // exception handler for ExceptionType2
 }
 // ...
 finally {
 // block of code to be executed after try block ends
 }
 Here, ExceptionType is the type of exception that has occurred
7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 45
Multithreaded Programming
 Unlike many other computer languages,
Java provides built-in support for
multithreaded programming. A
multithreaded program contains two or
more parts that can run concurrently.
 Each part of such a program is called a
thread, and each thread defines
a separate path of execution. Thus,
multithreading is a specialized form of
multitasking.
7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 46
Creating Multiple Threads
 // Create multiple threads.
 class NewThread implements Runnable {
 String name; // name of thread
 Thread t;
 NewThread(String threadname) {
 name = threadname;
 t = new Thread(this, name);
 System.out.println("New thread: " + t);
 t.start(); // Start the thread
 }
7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 47
Cont….
 // This is the entry point for thread.
 public void run() {
 try {
 for(int i = 5; i > 0; i--) {
 System.out.println(name + ": " + i);
 Thread.sleep(1000);
 }
 } catch (InterruptedException e) {
 System.out.println(name + "Interrupted");
 }
 System.out.println(name + " exiting.");
 }
 }
7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 48
Cont….
 class MultiThreadDemo {
 public static void main(String args[]) {
 new NewThread("One"); // start threads
 new NewThread("Two");
 new NewThread("Three");
 try {
 // wait for other threads to end
 Thread.sleep(10000);
 } catch (InterruptedException e) {
 System.out.println("Main thread Interrupted");
 }
7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 49
Cont…
 System.out.println("Main thread
exiting.");
 }
 }
7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 50
Output
 New thread: Thread[One,5,main]
 New thread: Thread[Two,5,main]
 New thread: Thread[Three,5,main]
 One: 5
 Two: 5
 Three: 5
 One: 4
 Two: 4
 Three: 4
 One: 3
 Three: 3
 Two: 3
 One: 2
 Three: 2
 Two: 2
 One: 1
 Three: 1
 Two: 1
 One exiting.
 Two exiting.
 Three exiting.
 Main thread exiting.
7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 51
 THANK YOU 
7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 52

Mais conteúdo relacionado

Mais procurados

Final keyword in java
Final keyword in javaFinal keyword in java
Final keyword in javaHitesh Kumar
 
Java Inheritance
Java InheritanceJava Inheritance
Java InheritanceVINOTH R
 
Collections In Java
Collections In JavaCollections In Java
Collections In JavaBinoj T E
 
Reflection in java
Reflection in javaReflection in java
Reflection in javaupen.rockin
 
Arrays in java language
Arrays in java languageArrays in java language
Arrays in java languageHareem Naz
 
Java Strings Tutorial | String Manipulation in Java | Java Tutorial For Begin...
Java Strings Tutorial | String Manipulation in Java | Java Tutorial For Begin...Java Strings Tutorial | String Manipulation in Java | Java Tutorial For Begin...
Java Strings Tutorial | String Manipulation in Java | Java Tutorial For Begin...Edureka!
 
Introduction to Spring Framework
Introduction to Spring FrameworkIntroduction to Spring Framework
Introduction to Spring Framework Serhat Can
 
Java interfaces
Java interfacesJava interfaces
Java interfacesjehan1987
 
Exception Handling in JAVA
Exception Handling in JAVAException Handling in JAVA
Exception Handling in JAVASURIT DATTA
 
Applet Architecture - Introducing Java Applets
Applet Architecture - Introducing Java AppletsApplet Architecture - Introducing Java Applets
Applet Architecture - Introducing Java Appletsamitksaha
 
Interface in java By Dheeraj Kumar Singh
Interface in java By Dheeraj Kumar SinghInterface in java By Dheeraj Kumar Singh
Interface in java By Dheeraj Kumar Singhdheeraj_cse
 

Mais procurados (20)

Final keyword in java
Final keyword in javaFinal keyword in java
Final keyword in java
 
Java Inheritance
Java InheritanceJava Inheritance
Java Inheritance
 
Collections In Java
Collections In JavaCollections In Java
Collections In Java
 
Collections framework in java
Collections framework in javaCollections framework in java
Collections framework in java
 
Reflection in java
Reflection in javaReflection in java
Reflection in java
 
Arrays in java language
Arrays in java languageArrays in java language
Arrays in java language
 
Polymorphism in java
Polymorphism in javaPolymorphism in java
Polymorphism in java
 
Java Strings Tutorial | String Manipulation in Java | Java Tutorial For Begin...
Java Strings Tutorial | String Manipulation in Java | Java Tutorial For Begin...Java Strings Tutorial | String Manipulation in Java | Java Tutorial For Begin...
Java Strings Tutorial | String Manipulation in Java | Java Tutorial For Begin...
 
Introduction to Spring Framework
Introduction to Spring FrameworkIntroduction to Spring Framework
Introduction to Spring Framework
 
Java interfaces
Java interfacesJava interfaces
Java interfaces
 
Java access modifiers
Java access modifiersJava access modifiers
Java access modifiers
 
Exception Handling in JAVA
Exception Handling in JAVAException Handling in JAVA
Exception Handling in JAVA
 
Core java
Core javaCore java
Core java
 
Java Basics
Java BasicsJava Basics
Java Basics
 
Java collections
Java collectionsJava collections
Java collections
 
Applet Architecture - Introducing Java Applets
Applet Architecture - Introducing Java AppletsApplet Architecture - Introducing Java Applets
Applet Architecture - Introducing Java Applets
 
Interface in java By Dheeraj Kumar Singh
Interface in java By Dheeraj Kumar SinghInterface in java By Dheeraj Kumar Singh
Interface in java By Dheeraj Kumar Singh
 
Java collections notes
Java collections notesJava collections notes
Java collections notes
 
Strings in java
Strings in javaStrings in java
Strings in java
 
Java- Nested Classes
Java- Nested ClassesJava- Nested Classes
Java- Nested Classes
 

Semelhante a Java programming(unit 1)

Java 9 features
Java 9 featuresJava 9 features
Java 9 featuresshrinath97
 
Vikeshp
VikeshpVikeshp
VikeshpMdAsu1
 
Intro Java Rev010
Intro Java Rev010Intro Java Rev010
Intro Java Rev010Rich Helton
 
Classes and Objects
Classes and ObjectsClasses and Objects
Classes and Objectsvmadan89
 
java-06inheritance
java-06inheritancejava-06inheritance
java-06inheritanceArjun Shanka
 
INDUMATHY- UNIT 1 cs3391 oops introduction to oop and java.pptx
INDUMATHY- UNIT 1 cs3391 oops introduction to oop and java.pptxINDUMATHY- UNIT 1 cs3391 oops introduction to oop and java.pptx
INDUMATHY- UNIT 1 cs3391 oops introduction to oop and java.pptxIndu65
 
Object+oriented+programming+in+java
Object+oriented+programming+in+javaObject+oriented+programming+in+java
Object+oriented+programming+in+javaYe Win
 
Introduction to Java Programming.pdf
Introduction to Java Programming.pdfIntroduction to Java Programming.pdf
Introduction to Java Programming.pdfAdiseshaK
 
Intro to programing with java-lecture 1
Intro to programing with java-lecture 1Intro to programing with java-lecture 1
Intro to programing with java-lecture 1Mohamed Essam
 
Core java &collections
Core java &collectionsCore java &collections
Core java &collectionsRavi varma
 
java traning report_Summer.docx
java traning report_Summer.docxjava traning report_Summer.docx
java traning report_Summer.docxGauravSharma164138
 
oops with java modules i & ii.ppt
oops with java modules i & ii.pptoops with java modules i & ii.ppt
oops with java modules i & ii.pptrani marri
 

Semelhante a Java programming(unit 1) (20)

JAVA PPT -3 BY ADI.pdf
JAVA PPT -3 BY ADI.pdfJAVA PPT -3 BY ADI.pdf
JAVA PPT -3 BY ADI.pdf
 
Introduction To Java.
Introduction To Java.Introduction To Java.
Introduction To Java.
 
Basics of java 1
Basics of java 1Basics of java 1
Basics of java 1
 
Java 9 features
Java 9 featuresJava 9 features
Java 9 features
 
Vikeshp
VikeshpVikeshp
Vikeshp
 
Intro Java Rev010
Intro Java Rev010Intro Java Rev010
Intro Java Rev010
 
Java_presesntation.ppt
Java_presesntation.pptJava_presesntation.ppt
Java_presesntation.ppt
 
Java basics
Java basicsJava basics
Java basics
 
Classes and Objects
Classes and ObjectsClasses and Objects
Classes and Objects
 
java-06inheritance
java-06inheritancejava-06inheritance
java-06inheritance
 
INDUMATHY- UNIT 1 cs3391 oops introduction to oop and java.pptx
INDUMATHY- UNIT 1 cs3391 oops introduction to oop and java.pptxINDUMATHY- UNIT 1 cs3391 oops introduction to oop and java.pptx
INDUMATHY- UNIT 1 cs3391 oops introduction to oop and java.pptx
 
Object+oriented+programming+in+java
Object+oriented+programming+in+javaObject+oriented+programming+in+java
Object+oriented+programming+in+java
 
Introduction to Java Programming.pdf
Introduction to Java Programming.pdfIntroduction to Java Programming.pdf
Introduction to Java Programming.pdf
 
Intro to programing with java-lecture 1
Intro to programing with java-lecture 1Intro to programing with java-lecture 1
Intro to programing with java-lecture 1
 
Core java
Core java Core java
Core java
 
Core java &collections
Core java &collectionsCore java &collections
Core java &collections
 
Core java1
Core java1Core java1
Core java1
 
java traning report_Summer.docx
java traning report_Summer.docxjava traning report_Summer.docx
java traning report_Summer.docx
 
oops with java modules i & ii.ppt
oops with java modules i & ii.pptoops with java modules i & ii.ppt
oops with java modules i & ii.ppt
 
Java notes
Java notesJava notes
Java notes
 

Mais de SURBHI SAROHA

Cloud Computing (Infrastructure as a Service)UNIT 2
Cloud Computing (Infrastructure as a Service)UNIT 2Cloud Computing (Infrastructure as a Service)UNIT 2
Cloud Computing (Infrastructure as a Service)UNIT 2SURBHI SAROHA
 
Management Information System(Unit 2).pptx
Management Information System(Unit 2).pptxManagement Information System(Unit 2).pptx
Management Information System(Unit 2).pptxSURBHI SAROHA
 
Searching in Data Structure(Linear search and Binary search)
Searching in Data Structure(Linear search and Binary search)Searching in Data Structure(Linear search and Binary search)
Searching in Data Structure(Linear search and Binary search)SURBHI SAROHA
 
Management Information System(UNIT 1).pptx
Management Information System(UNIT 1).pptxManagement Information System(UNIT 1).pptx
Management Information System(UNIT 1).pptxSURBHI SAROHA
 
Introduction to Cloud Computing(UNIT 1).pptx
Introduction to Cloud Computing(UNIT 1).pptxIntroduction to Cloud Computing(UNIT 1).pptx
Introduction to Cloud Computing(UNIT 1).pptxSURBHI SAROHA
 
Keys in dbms(UNIT 2)
Keys in dbms(UNIT 2)Keys in dbms(UNIT 2)
Keys in dbms(UNIT 2)SURBHI SAROHA
 
Database Management System(UNIT 1)
Database Management System(UNIT 1)Database Management System(UNIT 1)
Database Management System(UNIT 1)SURBHI SAROHA
 
Object-Oriented Programming with Java UNIT 1
Object-Oriented Programming with Java UNIT 1Object-Oriented Programming with Java UNIT 1
Object-Oriented Programming with Java UNIT 1SURBHI SAROHA
 
Database Management System(UNIT 1)
Database Management System(UNIT 1)Database Management System(UNIT 1)
Database Management System(UNIT 1)SURBHI SAROHA
 

Mais de SURBHI SAROHA (20)

Cloud Computing (Infrastructure as a Service)UNIT 2
Cloud Computing (Infrastructure as a Service)UNIT 2Cloud Computing (Infrastructure as a Service)UNIT 2
Cloud Computing (Infrastructure as a Service)UNIT 2
 
Management Information System(Unit 2).pptx
Management Information System(Unit 2).pptxManagement Information System(Unit 2).pptx
Management Information System(Unit 2).pptx
 
Searching in Data Structure(Linear search and Binary search)
Searching in Data Structure(Linear search and Binary search)Searching in Data Structure(Linear search and Binary search)
Searching in Data Structure(Linear search and Binary search)
 
Management Information System(UNIT 1).pptx
Management Information System(UNIT 1).pptxManagement Information System(UNIT 1).pptx
Management Information System(UNIT 1).pptx
 
Introduction to Cloud Computing(UNIT 1).pptx
Introduction to Cloud Computing(UNIT 1).pptxIntroduction to Cloud Computing(UNIT 1).pptx
Introduction to Cloud Computing(UNIT 1).pptx
 
JAVA (UNIT 5)
JAVA (UNIT 5)JAVA (UNIT 5)
JAVA (UNIT 5)
 
DBMS (UNIT 5)
DBMS (UNIT 5)DBMS (UNIT 5)
DBMS (UNIT 5)
 
DBMS UNIT 4
DBMS UNIT 4DBMS UNIT 4
DBMS UNIT 4
 
JAVA(UNIT 4)
JAVA(UNIT 4)JAVA(UNIT 4)
JAVA(UNIT 4)
 
OOPs & C++(UNIT 5)
OOPs & C++(UNIT 5)OOPs & C++(UNIT 5)
OOPs & C++(UNIT 5)
 
OOPS & C++(UNIT 4)
OOPS & C++(UNIT 4)OOPS & C++(UNIT 4)
OOPS & C++(UNIT 4)
 
DBMS UNIT 3
DBMS UNIT 3DBMS UNIT 3
DBMS UNIT 3
 
JAVA (UNIT 3)
JAVA (UNIT 3)JAVA (UNIT 3)
JAVA (UNIT 3)
 
Keys in dbms(UNIT 2)
Keys in dbms(UNIT 2)Keys in dbms(UNIT 2)
Keys in dbms(UNIT 2)
 
DBMS (UNIT 2)
DBMS (UNIT 2)DBMS (UNIT 2)
DBMS (UNIT 2)
 
JAVA UNIT 2
JAVA UNIT 2JAVA UNIT 2
JAVA UNIT 2
 
Database Management System(UNIT 1)
Database Management System(UNIT 1)Database Management System(UNIT 1)
Database Management System(UNIT 1)
 
Object-Oriented Programming with Java UNIT 1
Object-Oriented Programming with Java UNIT 1Object-Oriented Programming with Java UNIT 1
Object-Oriented Programming with Java UNIT 1
 
Database Management System(UNIT 1)
Database Management System(UNIT 1)Database Management System(UNIT 1)
Database Management System(UNIT 1)
 
OOPs & C++ UNIT 3
OOPs & C++ UNIT 3OOPs & C++ UNIT 3
OOPs & C++ UNIT 3
 

Último

University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdfKamal Acharya
 
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756dollysharma2066
 
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Bookingdharasingh5698
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performancesivaprakash250
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXssuser89054b
 
Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01KreezheaRecto
 
Unit 2- Effective stress & Permeability.pdf
Unit 2- Effective stress & Permeability.pdfUnit 2- Effective stress & Permeability.pdf
Unit 2- Effective stress & Permeability.pdfRagavanV2
 
Block diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.pptBlock diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.pptNANDHAKUMARA10
 
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Bookingroncy bisnoi
 
data_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfdata_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfJiananWang21
 
Bhosari ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready For ...
Bhosari ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready For ...Bhosari ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready For ...
Bhosari ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready For ...tanu pandey
 
notes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.pptnotes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.pptMsecMca
 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...roncy bisnoi
 

Último (20)

University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdf
 
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort ServiceCall Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
 
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak HamilCara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
 
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
 
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
 
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
 
NFPA 5000 2024 standard .
NFPA 5000 2024 standard                                  .NFPA 5000 2024 standard                                  .
NFPA 5000 2024 standard .
 
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
 
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performance
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
 
Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01
 
Unit 2- Effective stress & Permeability.pdf
Unit 2- Effective stress & Permeability.pdfUnit 2- Effective stress & Permeability.pdf
Unit 2- Effective stress & Permeability.pdf
 
Block diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.pptBlock diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.ppt
 
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
 
data_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfdata_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdf
 
Bhosari ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready For ...
Bhosari ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready For ...Bhosari ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready For ...
Bhosari ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready For ...
 
notes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.pptnotes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.ppt
 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
 
Call Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort ServiceCall Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
 

Java programming(unit 1)

  • 1. Unit-1 7/14/2017 1Ms SURBHI SAROHA(Asst.Professor)
  • 2. Contents  Introduction  Data types  Control structured  Arrays  Strings  Vector  Classes( Inheritance , package , exception handling)  Multithreaded programming 7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 2
  • 3. Java – Overview  Java programming language was originally developed by Sun Microsystems which was initiated by James Gosling and released in 1995.  The latest release of the Java Standard Edition is Java SE 8.  Java is guaranteed to be Write Once, Run Anywhere . 7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 3
  • 4. Java is:  Object Oriented: In Java, everything is an Object. Java can be easily extended since it is based on the Object model.  Platform Independent: Unlike many other programming languages including C and C++, when Java is compiled, it is not compiled into platform specific machine, rather into platform independent byte code. This byte code is distributed over the web and interpreted by the Virtual Machine (JVM) on whichever platform it is being run on.  Simple: Java is designed to be easy to learn. If you understand the basic concept of OOP Java, it would be easy to master.  Secure: With Java's secure feature it enables to develop virus-free, tamper-free systems. Authentication techniques are based on public-key encryption. 7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 4
  • 5. Cont…..  Architecture-neutral: Java compiler generates an architecture- neutral object file format, which makes the compiled code executable on many processors, with the presence of Java runtime system.  Portable: Being architecture-neutral and having no implementation dependent aspects of the specification makes Java portable.  Robust: Java makes an effort to eliminate error prone situations by emphasizing mainly on compile time error checking and runtime checking.  Multithreaded: With Java's multithreaded feature it is possible to write programs that can perform many tasks simultaneously. This design feature allows the developers to construct interactive applications that can run smoothly. 7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 5
  • 6. Cont…  Interpreted: Java byte code is translated on the fly to native machine instructions and is not stored anywhere. The development process is more rapid and analytical since the linking is an incremental and light-weight process.  High Performance: With the use of Just-In-Time compilers, Java enables high performance.  Distributed: Java is designed for the distributed environment of the internet.  Dynamic: Java is considered to be more dynamic than C or C++ since it is designed to adapt to an evolving environment. Java programs can carry extensive amount of run-time information that can be used to verify and resolve accesses to objects on run-time. 7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 6
  • 7. First Java Program  public class MyFirstJavaProgram {  /* This is my first java program.  * This will print 'Hello World' as the output  */  public static void main(String []args) {  System.out.println("Hello World"); // prints Hello World  }  } 7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 7
  • 8. Setting Up the Path for Windows  Assuming you have installed Java in c:Program Filesjavajdk directory:  Right-click on 'My Computer' and select 'Properties'.  Click the 'Environment variables' button under the 'Advanced' tab.  Now, alter the 'Path' variable so that it also contains the path to the Java executable. Example, if the path is currently set to 'C:WINDOWSSYSTEM32', then change your path to read 'C:WINDOWSSYSTEM32;c:Program Filesjavajdkbin 7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 8
  • 9. Save the file, compile and run the program  Open notepad and add the code as above.  Save the file as: MyFirstJavaProgram.java.  Open a command prompt window and . Java – Basic Syntax  go to the directory where you saved the class. Assume it's C:. 7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 9
  • 10. Cont….  Type 'javac MyFirstJavaProgram.java' and press enter to compile your code. If there are no errors in your code, the command prompt will take you to the next line (Assumption : The path variable is set).  Now, type ' java MyFirstJavaProgram ' to run your program.  You will be able to see ' Hello World ' printed on the window. 7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 10
  • 11. Cont….  C:> javac MyFirstJavaProgram.java  C:> java MyFirstJavaProgram  Hello World 7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 11
  • 12. Basic Syntax  Case Sensitivity - Java is case sensitive, which means identifier Helloand hello would have different meaning in Java.  Class Names - For all class names the first letter should be in Upper Case. If several words are used to form a name of the class, each inner word's first letter should be in Upper Case.  Example: class MyFirstJavaClass  Method Names - All method names should start with a Lower Case letter. If several words are used to form the name of the method, then each inner word's first letter should be in Upper Case.  Example: public void myMethodName()  Program File Name - Name of the program file should exactly match the class name. When saving the file, you should save it using the class name (Remember Java is case sensitive) and append '.java' to the end of the name (if the file name and the class name do not match, your program will not compile). Example: Assume 'MyFirstJavaProgram' is the class name. Then the file should be saved as 'MyFirstJavaProgram.java' 7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 12
  • 13. Java is an Object-Oriented Language  Java supports the following fundamental concepts:  Polymorphism  Inheritance  Encapsulation  Abstraction  Classes  Objects  Instance  Method  Message Parsing 7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 13
  • 14. Classes in Java  A class is a blueprint from which individual objects are created.  public class Dog{  String breed;  int ageC  String color;  void barking(){  }  void hungry(){  }  void sleeping(){  }  } 7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 14
  • 15. Constructors  The main rule of constructors is that they should have the same name as the class. A class can have more than one constructor.  Following is an example of a constructor:  public class Puppy{  public Puppy(){  }  public Puppy(String name){  // This constructor has one parameter, name.  }  } 7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 15
  • 16. Basic Datatypes  There are two data types available in Java:  Primitive Datatypes  Reference/Object Datatypes 7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 16
  • 17. Primitive Datatypes  Byte  short  int  long  float  double  boolean  char 7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 17
  • 18. Reference Datatypes  A reference variable can be used to refer any object of the declared type or any compatible type.  Example: Animal animal = new Animal("giraffe"); 7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 18
  • 19. Java Access Modifiers  The four access levels are:  Visible to the package, the default. No modifiers are needed.  Visible to the class only (private).  Visible to the world (public).  Visible to the package and all subclasses (protected). 7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 19
  • 20. Abstract Class  An abstract class can never be instantiated. If a class is declared as abstract then the sole purpose is for the class to be extended.  A class cannot be both abstract and final (since a final class cannot be extended). If a class contains abstract methods then the class should be declared abstract. Otherwise, a compile error will be thrown.  An abstract class may contain both abstract methods as well normal methods. 7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 20
  • 21. Example  abstract class Caravan{  private double price;  private String model;  private String year;  public abstract void goFast(); //an abstract method  public abstract void changeColor();  } 7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 21
  • 22. Loop Control 7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 22
  • 23. Decision Making 7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 23
  • 24. Cont…. 7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 24
  • 25. Strings Class  Strings, which are widely used in Java programming, are a sequence of characters.  Creating Strings  The most direct way to create a string is to write:  String greeting = "Hello world!"; 7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 25
  • 26. Example  public class StringDemo{  public static void main(String args[]){  char[] helloArray = { 'h', 'e', 'l', 'l', 'o', '.'};  String helloString = new String(helloArray);  System.out.println( helloString );  }  } 7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 26
  • 27. Output  This will produce the following result:  hello. 7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 27
  • 28. String Length  public class StringDemo {  public static void main(String args[]) {  String palindrome = "Dot saw I was Tod";  int len = palindrome.length();  System.out.println( "String Length is : " + len );  }  }  This will produce the following result:  String Length is : 17 7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 28
  • 29. Concatenating Strings String compareTo(String anotherString) Method  The String class includes a method for concatenating two strings:  string1.concat(string2); 7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 29
  • 30. Arrays  Java provides a data structure, the array, which stores a fixed-size sequential collection of elements of the same type.  An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type. 7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 30
  • 31. Java – Exceptions  An exception (or exceptional event) is a problem that arises during the execution of a program. When an Exception occurs the normal flow of the program is disrupted and the program/Application terminates abnormally, which is not recommended, therefore, these exceptions are to be handled. 7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 31
  • 32. Exception Hierarchy 7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 32
  • 33. Catching Exceptions  A method catches an exception using a combination of the try and catch keywords.  try  {  //Protected code  }catch(ExceptionName e1)  {  //Catch block  } 7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 33
  • 34. Java – Inner Classes  Nested Classes  In Java, just like methods, variables of a class too can have another class as its member. Writing a class within another is allowed in Java. The class written within is called the nested class, and the class that holds the inner class is called the outer class. 7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 34
  • 35. Syntax to write a nested class  Here, the class Outer_Demo is the outer class and the class Inner_Demo is the nested class.  class Outer_Demo{  class Nested_Demo{  }  } 7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 35
  • 36. Java – Inheritance  The class which inherits the properties of other is known as subclass (derived class, child class) and the class whose properties are inherited is known as superclass (base class, parent class).  extends Keyword  extends is the keyword used to inherit the properties of a class. Following is the syntax of extends keyword. 7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 36
  • 37. Syntax of extends keyword  class Super{  .....  .....  }  class Sub extends Super{  .....  .....  } 7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 37
  • 38. Types of Inheritance 7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 38
  • 39. Cont…. 7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 39
  • 40. Note:-  A very important fact to remember is that Java does not support multiple inheritance. This means that a class cannot extend more than one class. 7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 40
  • 41. Java – Packages  A Package can be defined as a grouping of related types (classes, interfaces, enumerations and annotations ) providing access protection and namespace management.  Some of the existing packages in Java are:  java.lang - bundles the fundamental classes  java.io - classes for input, output functions are bundled in this package 7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 41
  • 42. Creating a Package  While creating a package, you should choose a name for the package and include a package statement along with that name at the top of every source file that contains the classes, interfaces, enumerations, and annotation types that you want to include in the package.  The package statement should be the first line in the source file. There can be only one package statement in each source file, and it applies to all types in the file. 7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 42
  • 43. Package example  Following package example contains interface named animals:  /* File name : Animal.java */  package animals;  interface Animal {  public void eat();  public void travel();  } 7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 43
  • 44. Exception Handling  A Java exception is an object that describes an exceptional (that is, error) condition that has occurred in a piece of code.  Java exception handling is managed via five keywords: try, catch, throw, throws, and finally. 7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 44
  • 45. General form of an exception- handling block: try {  // block of code to monitor for errors  }  catch (ExceptionType1 exOb) {  // exception handler for ExceptionType1  }  catch (ExceptionType2 exOb) {  // exception handler for ExceptionType2  }  // ...  finally {  // block of code to be executed after try block ends  }  Here, ExceptionType is the type of exception that has occurred 7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 45
  • 46. Multithreaded Programming  Unlike many other computer languages, Java provides built-in support for multithreaded programming. A multithreaded program contains two or more parts that can run concurrently.  Each part of such a program is called a thread, and each thread defines a separate path of execution. Thus, multithreading is a specialized form of multitasking. 7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 46
  • 47. Creating Multiple Threads  // Create multiple threads.  class NewThread implements Runnable {  String name; // name of thread  Thread t;  NewThread(String threadname) {  name = threadname;  t = new Thread(this, name);  System.out.println("New thread: " + t);  t.start(); // Start the thread  } 7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 47
  • 48. Cont….  // This is the entry point for thread.  public void run() {  try {  for(int i = 5; i > 0; i--) {  System.out.println(name + ": " + i);  Thread.sleep(1000);  }  } catch (InterruptedException e) {  System.out.println(name + "Interrupted");  }  System.out.println(name + " exiting.");  }  } 7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 48
  • 49. Cont….  class MultiThreadDemo {  public static void main(String args[]) {  new NewThread("One"); // start threads  new NewThread("Two");  new NewThread("Three");  try {  // wait for other threads to end  Thread.sleep(10000);  } catch (InterruptedException e) {  System.out.println("Main thread Interrupted");  } 7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 49
  • 50. Cont…  System.out.println("Main thread exiting.");  }  } 7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 50
  • 51. Output  New thread: Thread[One,5,main]  New thread: Thread[Two,5,main]  New thread: Thread[Three,5,main]  One: 5  Two: 5  Three: 5  One: 4  Two: 4  Three: 4  One: 3  Three: 3  Two: 3  One: 2  Three: 2  Two: 2  One: 1  Three: 1  Two: 1  One exiting.  Two exiting.  Three exiting.  Main thread exiting. 7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 51
  • 52.  THANK YOU  7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 52