0% found this document useful (0 votes)
6 views

Java Programming-Unit1

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Java Programming-Unit1

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 36

Programming in Java

IVth SEMESTER
CIC-212

Department of COMPUTER SCIENCE & ENGINEERING, BVCOE New Delhi


Features of Java

1. Simple
2. Object Oriented
3. Robust
4. Platform Independent
5. Secure
6. Multi-threading
7. Portable
8. High Performance

Department of COMPUTER SCIENCE & ENGINEERING, BVCOE New Delhi


Types of Variables

1. Primitive Variable – Can be used to represent primitive values, for eg: int x
= 10;
2. Reference Variable – Used to Refer Objects, for eg: Student s = new
Student();
Based on the purpose and position of declaration all variables are divided into
3 types:
1. Instance Variable
2. Static Variable
3. Local Variables

Department of COMPUTER SCIENCE & ENGINEERING, BVCOE New Delhi


Instance Variable

1. Value of the variable is varied from object to object and for every object
separate copy of instance variable is created.
2. Scope is exactly same as the scope of the objects.
3. Cannot be accessed from static area directly, we need to access by using
object reference.

Department of COMPUTER SCIENCE & ENGINEERING, BVCOE New Delhi


Instance Variable

1. It is not required to perform initialization explicitly, JVM will provide default


values.

Department of COMPUTER SCIENCE & ENGINEERING, BVCOE New Delhi


Static Variable

1. In this the value of variable is not varied from object to object.


2. In case of static variable single copy will be created at class level and the
copy will be shared by all objects of that class.
3. Created at the time of Class loading and destroyed at the time of class
unloading.
4. Can be accessed by either using Class Name(Recommended) or by using
Object Reference.
5. Are also known as “Class-Level Variables”.

Department of COMPUTER SCIENCE & ENGINEERING, BVCOE New Delhi


Local Variable

1. Created inside method or Block or Constructor


2. Scope is exactly same as the block in which local variables are declared.
3. JVM won’t provide any default values, it is compulsory to initialize the
variable before using it.

Department of COMPUTER SCIENCE & ENGINEERING, BVCOE New Delhi


First Hello World Program

Command
Line
Arguments
Starting
Point of
the Code
To call by JVM JVM calls this Main method can’t
from method without return anything to
anywhere any existing object JVM
Department of COMPUTER SCIENCE & ENGINEERING, BVCOE New Delhi
First Hello World Program

Method present to
output Values on
Class Name Static variable of type
Console
Present in PrintStream present in
java.lang system class
Department of COMPUTER SCIENCE & ENGINEERING, BVCOE New Delhi
Passing Command Line Argument

1. These arguments are passed from command prompt


2. Within the main method command line arguments are
available in String form.

Department of COMPUTER SCIENCE & ENGINEERING, BVCOE New Delhi


Java Assignment 1

1. Create Simple Calculator using Command Line


Argument.
2. Create a Java Program to take i/p from user and
find its factorial.

Department of COMPUTER SCIENCE & ENGINEERING, BVCOE New Delhi


Taking Input from User

Using Scanner Class


1. Import java.util.Scanner class
2. Create Object of Scanner Class
3. Use following in-built methods to take input
from user
1. nextInt()
2. next()
3. nextLong() and many more..

Department of COMPUTER SCIENCE & ENGINEERING, BVCOE New Delhi


Operators(Increment & Decrement)

Increment Operator
1. Pre Increment – int x=++y, Post Increment – int x =y++;
Decrement Operator
1. Pre Decrement – int x = --y, Post Decrement – int x = y--;
Example Showing Use of Increment and Decrement Operator

Department of COMPUTER SCIENCE & ENGINEERING, BVCOE New Delhi


Operators(Increment & Decrement)

1. Increment/Decrement can be apply only for variables, not for


constant values, int y=++4 -> Error
2. Nesting of inc/dec operator is not allowed, int y=++(++x) ->
Error
3. Can’t apply for final variables
4. Can apply for every primitive data type except Boolean

Department of COMPUTER SCIENCE & ENGINEERING, BVCOE New Delhi


Difference b/w x++ and x=x+1

1. In arithmetic operation between two variables x & y, the result


type is always -> Max(int, type of a, type of b)
2. In Increment and Decrement Operators the required type
casting(internal type casting) is automatically performed by
the compiler.
byte b++; => b = (byte)(b+1);
b++; => b=(type of b)(b+1);

Department of COMPUTER SCIENCE & ENGINEERING, BVCOE New Delhi


Break Statement

1. Can be used within Switch to stop fall through


2. Inside looks to break the loop execution based on
some condition
3. Inside labeled blocks to break that block execution
based on some condition.

Department of COMPUTER SCIENCE & ENGINEERING, BVCOE New Delhi


Continue Statement

1. Use continue statement to skip current iteration and continue for next
iteration inside loops.

L2p5.java

Labeled Continue Statement Example – L2P6.java

Department of COMPUTER SCIENCE & ENGINEERING, BVCOE New Delhi


Constructors in Java

Case1: When no Default Constructor is provided

Department of COMPUTER SCIENCE & ENGINEERING, BVCOE New Delhi


Constructors in Java

Department of COMPUTER SCIENCE & ENGINEERING, BVCOE New Delhi


Constructors in Java

Department of COMPUTER SCIENCE & ENGINEERING, BVCOE New


Constructors in Java

Example – App.java

Department of COMPUTER SCIENCE & ENGINEERING, BVCOE New Delhi


Constructors in Java

Ques: What will happen if only parameterized constructor is


declared in the program and you try to call default constructor.

Example

Ques: Can you define a method with same name as constructor.

Example
Department of COMPUTER SCIENCE & ENGINEERING, BVCOE New Delhi
Inheritance in Java

1. Inheritance is the mechanism of deriving new class from


old one.
2. Old class is knows as superclass and new class is known
as subclass
3. The subclass inherits all of its instances variables and
methods defined by the superclass and it also adds its
own unique elements. Thus we can say that subclass are
specialized version of superclass.
4. Benefits
1. Reusability of code
2. Code Sharing
3. Consistency in using an interface
Department of COMPUTER SCIENCE & ENGINEERING, BVCOE New Delhi
Type of Inheritance in Java

1. Single Inheritance - one class extends one class


only

2. Multilevel Inheritance - It is a ladder or hierarchy


of single level inheritance. It means if Class A is
extended by Class B and then further Class C
extends Class B then the whole structure is
termed as Multilevel Inheritance.
Department of COMPUTER SCIENCE & ENGINEERING, BVCOE New Delhi
Type of Inheritance in Java

3. Hierarchical Inheritance - One class is extended by many


subclasses. It is one to many relationship.

Department of COMPUTER SCIENCE & ENGINEERING, BVCOE New Delhi


Examples of Member Access Modifier
Case 1: Member Variables have no access modifier (default access
modifier)

Department of COMPUTER SCIENCE & ENGINEERING, BVCOE New Delhi


Examples of Member Access Modifier
Case 2: Member Variables have public/protected access modifier. If a
member of class is declared as either public or protected than it can be
accessed from child or inherited class.

Department of COMPUTER SCIENCE & ENGINEERING, BVCOE New Delhi


Examples of Member Access Modifier
Case 3: Member Variables have private access modifier. If a member of
class is declared as private than it cannot be accessed outside the class not
even in the inherited class.

Department of COMPUTER SCIENCE & ENGINEERING, BVCOE New Delhi


Use of “Super” Keyword

super is a reference variable that is used to refer immediate parent


class object. Uses of super keyword are as follows:
1. super() is used to invoke immediate parent class constructors
2. super is used to invoke immediate parent class method
3. super is used to refer immediate parent class variable
Example to Access Parent Class Variable with Super
Example to Call Immediate Parent Class Constructor using Super
Example where super() is not written

Department of COMPUTER SCIENCE & ENGINEERING, BVCOE New Delhi


Method Overriding
1. If a class inherits a method from its super class, then there is a
chance to override the method provided that it is not marked
final.
2. The benefit of overriding is: ability to define a behavior that's
specific to the subclass type which means a subclass can
implement a parent class method based on its requirement.
Example 1
Example 2

Department of COMPUTER SCIENCE & ENGINEERING, BVCOE New Delhi


Final Keyword
Final keyword can be used in the following ways:
1. Final Variable : Once a variable is declared as final, its value
cannot be changed during the scope of the program, Example
2. Final Method : Method declared as final cannot be overridden,
Example
3. Final Class : A final class cannot be inherited, Example

Department of COMPUTER SCIENCE & ENGINEERING, BVCOE New Delhi


Abstract Class

When the keyword abstract appears in a class definition, it means that zero or
more of its methods are abstract.
1. An abstract method has no body
2. Some of the subclass has to override it and provide the implementation
3. Objects cannot be created out of abstract class.
4. Abstract classes basically provide a guideline for the properties and
methods of an object.
5. In order to use abstract classes, they have to be subclassed.
6. There are situations in which you want to define a superclass that declares
the structure of a given abstraction without providing a complete
implementation of every method.

Department of COMPUTER SCIENCE & ENGINEERING, BVCOE New Delhi


Interface

1. An interface is a collection of abstract methods. A class implements an interface, thereby


inheriting the abstract methods of the interface.
2. Unless the class that implements the interface is abstract, all the methods of the interface need
to be defined in the class.
3. An interface is similar to a class in the following ways:
1. An interface can contain any number of methods.
2. An interface is written in a file with a .java extension, with the name of the interface
matching the name of the file.
3. The bytecode of an interface appears in a .class file.
4. Interfaces appear in packages, and their corresponding bytecode file must be in a directory
structure that matches the package name.

Department of COMPUTER SCIENCE & ENGINEERING, BVCOE New Delhi


Interface

However, an interface is different from a class in several ways, including:


1. You cannot instantiate an interface.
2. An interface does not contain any constructors.
3. All of the methods in an interface are abstract.
4. An interface cannot contain instance fields. The only fields that can appear in an interface must
be declared public, static and final.
5. An interface is not extended by a class; it is implemented by a class.
6. An interface can extend multiple interfaces.
Example to Create Interface, Example of Class implementing interface

Department of COMPUTER SCIENCE & ENGINEERING, BVCOE New Delhi


Multiple inheritance using Interface

1. Case 1: If two interfaces contains a method with same signature


and same return type, then in the implementation class we can
provide implementation only for one method. Example
2. Case 2: In two interface contains a method with same name but
different arguments, then in the implementation class we have to
provide implementation for both methods. Example
3. Case 3: If two interfaces contains a method with same signature
but different return type then it is impossible to implement both
interfaces at a time. Example
4. Variable Naming Conflict: Example
Department of COMPUTER SCIENCE & ENGINEERING, BVCOE New Delhi
Difference b/w Interface and Abstract
ClassesInterface Abstract Class
Every method is by default public Every method need not be public
and abstract and abstract, non-abstract methods
can also be created.
Following modifiers are not allowed No Restrictions, Any modifier can be
for interface methods: strictfp, used
protected, static, private, final,
synchronized
Every Variable is public, static, final Need Not be public, static, final
by default whether we declare it or
not
VariableDepartment of COMPUTER SCIENCE & ENGINEERING, BVCOE New Delhi
initialization is compulsory No Restrictions

You might also like