0% found this document useful (0 votes)
68 views29 pages

Nishita Koshta Branch:-Information Technology 3rd Year, VTH Semester

Nishita koshta studies Information Technology in the 3rd year, 5th semester. The document discusses key concepts of Object-Oriented Programming like objects, classes, inheritance, and encapsulation. It explains why Java is a popular language and describes how Java code is executed by the Java Virtual Machine independently of hardware platforms, making Java portable. It also provides examples of primitive data types in Java, and shows a simple "Hello World" program.

Uploaded by

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

Nishita Koshta Branch:-Information Technology 3rd Year, VTH Semester

Nishita koshta studies Information Technology in the 3rd year, 5th semester. The document discusses key concepts of Object-Oriented Programming like objects, classes, inheritance, and encapsulation. It explains why Java is a popular language and describes how Java code is executed by the Java Virtual Machine independently of hardware platforms, making Java portable. It also provides examples of primitive data types in Java, and shows a simple "Hello World" program.

Uploaded by

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

Nishita koshta

Branch:- Information technology


3rd year, Vth semester
Object-Oriented
Programming
Main Concepts

• Object
• Class
• Inheritance
• Encapsulation
Why Java ?

• Portable
• Easy to learn

• [ Designed to be used on the Internet ]


JVM

• JVM stands for


Java Virtual Machine

• Unlike other languages, Java “executables”


are executed on a CPU that does not exist.
Platform Independent

myprog.java myprog.class
javac bytecode
Java source code
JVM

OS/Hardware
Primitive types
• int 4 bytes
• short 2 bytes
• long 8 bytes Behaviors is
• byte 1 byte exactly as in
• float 4 bytes C++
• double 8 bytes
• char Unicode encoding (2 bytes) Note:
Primitive type
• boolean {true,false} always begin
with lower-case
Hello World
Hello.java

class Hello {
public static void main(String[] args) {
System.out.println(“Hello World
!!!”); }
}
Arrays
• Array is an object

• Array size is
fixed

Animal[] arr; // nothing yet …

arr = new Animal[4]; // only array of pointers

for(int i=0 ; i < arr.length ; i++) {


arr[i] = new Animal();

// now we have a complete array


Arrays - Multidimensional

• In Java
Animal[][] arr=
new
Animal[2][2]

What is the type of


the object here ?
• Member function
– Static member function can access only static members
– Static member function can be called without an
instance.

Class TeaPot {
private static int numOfTP = 0;
private Color myColor_;
public TeaPot(Color c) {
myColor_ = c;
numOfTP++;
}
public static int howManyTeaPots()
{ return numOfTP; }

// error :
public static Color getColor()
{ return myColor_; }
}
String is an Object
• Constant strings as in C, does not exist

• The function call foo(“Hello”) creates a String


object, containing “Hello”, and passes reference to it
to foo.

• There is no point in writing :

String s = new String(“Hello”);

• The String object is a constant. It can’t be changed


using a reference to it.
Flow control
Basically, it is exactly like c/c++.
do/while
int i=5; switch
if/else
do { char
If(x==4) { // act1 c=IN.getChar();
// act1 i--; switch(c) {
} else { } while(i!=0); case ‘a’:
// act2 case ‘b’:
} for // act1
int j; break;
for(int i=0;i<=9;i++) default:
{ // act2
j+=i; }
}
Packages
• Java code has hierarchical structure.
• The environment variable CLASSPATH contains
the directory names of the roots.
• Every Object belongs to a package ( ‘package’
keyword)
• Object full name contains the name full name of the
package containing it.
Access Control
• public member (function/data)
– Can be called/modified from outside.
• protected
– Can be called/modified from derived classes
• private
– Can be called/modified only from the current class
• default ( if no access modifier stated )
– Usually referred to as “Friendly”.
– Can be called/modified/instantiated from the same
package.
Inheritance class Base {
Base(){}
Base(int i) {}
Base protected void foo() {…}
}

class Derived extends Base {


Derived() {}
protected void foo() {…}
Derived(int i) {
super(i);
Derived …
super.foo();
}
}
As opposed to C++, it is possible to inherit only from ONE
class.
Pros avoids many potential problems and bugs.
Cons might cause code replication
Abstract

A class which is declared as abstract is known as an


abstract class. It can have abstract and non-abstract
methods. It needs to be extended and its method
implemented. It cannot be instantiated.
Abstraction in Java:
• Abstraction is a process of hiding the
implementation details and showing only
functionality to the user.
Interface

Declaring Interfaces
The interface keyword is used to declare an
interface. Here is a simple example to declare an
interface −
/* File name : NameOfInterface.java */
import java.lang.*;
// Any number of import statements

public interface NameOfInterface {


// Any number of final, static fields
// Any number of abstract method d
When to use an interface ?

Perfect tool for encapsulating the


classes inner structure. Only the
interface will be exposed
Collections
• Collection/container
– object that groups multiple elements
– used to store, retrieve, manipulate,
communicate aggregate data
• Iterator - object used for traversing a
collection and selectively remove elements

• Generics – implementation is parametric in


the type of elements
Collection Interfaces

Collection Map

Set List Queue Sorted


Map
SortedSet
final
final class Base {
• final member data final int i=5;
final void foo() {
Constant member i=10;
//what will the compiler
say about this?
• final member function }
}
The method can’t be
overridden. class Derived extends
Base { // Error
// another foo ...
• final class void foo() {
‘Base’ is final, thus it }
can’t be extended }
Exception - What is it and why do I care?
Definition: An exception is an event that
occurs during the execution of a program
that disrupts the normal flow of instructions.
Thank you

You might also like