1 / 17

Unit-2 Objects and Classes

Unit-2 Objects and Classes. Basics of objects and classes in java. Introduction. Languages like Pascal, C, FORTRAN, and COBOL are called procedure oriented programming languages. Since in these languages, a programmer uses procedures or functions to perform a task.

ide
Télécharger la présentation

Unit-2 Objects and Classes

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Unit-2 Objects and Classes Basics of objects and classes in java

  2. Introduction • Languages like Pascal, C, FORTRAN, and COBOL are called procedure oriented programming languages. Since in these languages, a programmer uses procedures or functions to perform a task. • When the programmer wants to write a program, he will first divide the task into separate sub tasks, each of which is expressed as functions/ procedures. This approach is called procedure oriented approach. • The languages like C++ and Java use classes and object in their programs and are called Object Oriented Programming languages. The main task is divided into several modules and these are represented as classes. • Each class can perform some tasks for which several methods are written in a class. This approach is called Object Oriented approach.

  3. Difference between Procedure Oriented Programming and OOP

  4. Class • In object-oriented programming, a class is a programming language construct that is used as a blueprint to create objects. • A class is a blueprint or prototype that defines the variables and methods common to all objects of a certain kind. • This blueprint includes attributes and methods that the created objects all share. • Class can be thought of as a user defined data type and an object as a variable of that data type, which can contain data and methods i.e. functions, working on that data. • Usually, a class represents a person, place, or thing.

  5. Class(Cont.)

  6. Parts of a class • The class contains two different sections: • variable declarations and method declaration. • The variables of a class describe its state • and methods describe its behavior. classDeclaration { memberVariableDeclarations methodDeclarations }

  7. object • Object-oriented programming (OOP) involves programming using objects. • The real-world objects have state and behavior. • For example, • horses have state (name, color, breed, hungry) and horses have behavior (barking, fetching, and slobbering on your newly cleaned slacks). • Bikes have state (gear, accelerator, two wheels, number of gears, brakes) and behavior (braking, accelerating, slowing down and changing gears).

  8. Object(Cont.) • Objects of the same type are defined using a common class. • An object is an instance of a class. Instance means physically happening • You can create many instances of a class. • “To give a real world analogy, a house is constructed according to a specification. Here, the specification is a blueprint that represents a class, and the constructed house represents the object”. • EXAMPLE: a class named Circle and its three objects.

  9. Object(Cont.)

  10. Object(Cont.) • By initializing an object we mean that the instances variables are assigned some values. This task is accomplished using a constructor. • The final object creation can be said as complete when the objects are initialized, either with an implicit constructor or an explicit constructor. This object creation can be used in programming code in two ways: • SalesTaxCalculator obj1 = new SalesTaxCalculator ( ); • Here all the three operations, object declaration, object instantiation and object initialization are done by one statement only.

  11. Example:Object And Class

  12. Difference between variables and objects • A variable holds a single type of literal, while an • object is a instance of a class with a set of instance variables and methods which performs certain task depending what methods have been defined for .

  13. Instance Variable • a class can have many instances, each instance having its own set of variables. E.g. class SalesTaxCalculator { float amount=100.0f; // instance variable float taxRate=10.2f; //instance variable void calculateTax() { float taxAmt = amount*taxRate/100; System.out.println(taxAmt); } public static void main (String args[ ]) { SalesTaxCalculator obj1 = new SalesTaxCalculator(); SalesTaxCalculator obj2 = new SalesTaxCalculator(); }}

  14. Instance variable (contd.) • Each variable declared inside a class and outside the methods is termed as instance variable .these variables are initialized by the constructors. • In the above example the two objects obj1 and obj2 will have their own set of instance variables. i.e. obj1 will have its own amount and taxRate whereas obj2 will have its own set of amount and taxRate.

  15. Initializing Instance Variables

  16. Initializing Instance Variables(Cont.)

  17. Initializing Instance Variables(Cont.) • Type 3: We can initialize instance variables using a constructor. See more in Constructor Section. • For accessing value of an object • Objectname.variablename • to assign values to the variables of an object • Objectname.variablename = value

More Related