Sei sulla pagina 1di 16

WELCOME

TO
JAVA TECHNOLOGIES

Srinivas M

Objective

After This session u will be able to do

Functions .
INTRODUCTION TO OOPS
Object

Oriented Programming Features.


Classes n Objects
Data-Encapsulation.

Methods In JAVA

A method is a named sequence of


code that can be invoked by other
Java code.
A method takes some parameters,
performs some computations and
then optionally returns a value (or
object).
Methods can be used as part of an
expression statement.

public float convertCelsius(float tempC) {


return( ((tempC * 9.0f) / 5.0f) + 32.0 );
}

Methods in Java
methods break down large problems into
smaller ones
your program may call the same method many
times

saves

writing and maintaining same code

methods take parameters


information

needed to do their job

methods can return a value


must

specify type of value returned

Example method
signature
public static int addNums(int num1, int num2)
{
int answer = num1 + num2;
return answer;
}

body

Method signature
visibility [static] returnType methodName(parameterList)

visibility:
public

accessible to other objects and classes


protected
accessible to classes which inherit from this one
private

static keyword:
use

when method belongs to class as whole

not object of the class


6

Calling a method

methods will not run unless called from


elsewhere
a

statement in main() method could call another


method
this method could call a third method .....

class methods are called with the form:


ClassName.methodName(parameters);
omit ClassName if called in same class

method name and parameters must match


the method signature
if the method returns a value, it can be
stored in a variable or passed to another
method

Object-Oriented

Java supports OOD

Classes and Objects


Data-Hiding
Data-Encapsulation.
Polymorphism
Inheritance
Encapsulation

Java programs contain nothing but


definitions and instantiations of classes

Everything is encapsulated in a class!

Classes ARE Object


Definitions

OOP - object oriented programming


code built from objects
Java these are called classes
Each class definition is coded in a
separate .java file
Name of the object must match the
class/object name

The three principles of OOP

Encapsulation

Inheritance

Objects hide their


functions (methods)
and data (instance
variables)
Each subclass inherits
manual
all variables of its
superclass

Polymorphism

Interface same despitedraw()


different data types

car

Superclass
auto
matic

Subclasses

draw()

Introduction to Classes

A class is a way of developing a new


Data-Type by encapsulating both data
and functions.
Syntax:
Class <class-name>
{
data-type v1;
data-type-v2;
.
.
function-1();
function-2();
}

Classes contd

Variables declared inside a class are called


Instance Variables.
Functions' declared inside a class are called
Methods.
Usage: Classes are used for creating objects like
creating variables using primitive data-type.
Classes are called Reference types in JAVA

Example of Simple Class and


Method
Class Fruit{
int grams;
int cals_per_gram;
int total_calories() {
return(grams*cals_per_gram);
}
}

Object

An object is a real world entity in


general.
In JAVA an object is instance of a class
created in memory with structure of a
class.
Objects consists memory for Instance
variables and methods.

Using objects

Here, code in one class creates an instance


of another class and does something with it

Fruit plum=new Fruit();


int cals;
cals = plum.total_calories();

Dot operator allows you to access (public)


data/methods inside Fruit class
DATA-ENCAPSULATION is wrapping up of
data(IV) and operators(methods) together
into a single class-type variable.

Potrebbero piacerti anche