Session-4-1(Classes n Objects (1)

16
WELCOME TO JAVA TECHNOLOGIES TECHNOLOGIES Srinivas M

description

sessions for life

Transcript of Session-4-1(Classes n Objects (1)

  • WELCOME TO JAVA TECHNOLOGIESSrinivas M

  • Objective After This session u will be able to do Functions .INTRODUCTION TO OOPSObject Oriented Programming Features.Classes n ObjectsData-Encapsulation.

  • Methods In JAVAA 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 Javamethods break down large problems into smaller onesyour program may call the same method many timessaves writing and maintaining same codemethods take parameters information needed to do their jobmethods can return a valuemust specify type of value returned

  • Example method

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

    signaturebody

  • *Method signaturevisibility [static] returnType methodName(parameterList)visibility:public accessible to other objects and classesprotectedaccessible to classes which inherit from this oneprivatestatic keyword:use when method belongs to class as wholenot object of the class

  • *Calling a methodmethods will not run unless called from elsewhere a statement in main() method could call another methodthis method could call a third method .....class methods are called with the form:

    ClassName.methodName(parameters);omit ClassName if called in same classmethod name and parameters must match the method signatureif the method returns a value, it can be stored in a variable or passed to another method

  • Object-OrientedJava supports OODClasses and ObjectsData-Hiding Data-Encapsulation.PolymorphismInheritanceEncapsulationJava programs contain nothing but definitions and instantiations of classesEverything is encapsulated in a class!

  • Classes ARE Object DefinitionsOOP - object oriented programmingcode built from objects Java these are called classesEach class definition is coded in a separate .java fileName of the object must match the class/object name

  • The three principles of OOPEncapsulationObjects hide their functions (methods) and data (instance variables)InheritanceEach subclass inherits all variables of its superclassPolymorphismInterface same despite different data types

    carauto-maticmanualSuper classSubclasses

    draw()draw()

    *

  • Introduction to ClassesA class is a way of developing a new Data-Type by encapsulating both data and functions.Syntax:

    Class {data-type v1;data-type-v2;..function-1();function-2();}

  • Classes contdVariables 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 MethodClass Fruit{int grams;int cals_per_gram;

    int total_calories() {return(grams*cals_per_gram);}}

  • ObjectAn 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 objectsHere, 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 classDATA-ENCAPSULATION is wrapping up of data(IV) and operators(methods) together into a single class-type variable.

  • ?

    *