Classes define objects and their properties and behaviors. An object is an instance of a class and has both state, stored in fields, and behavior, exposed through methods. In Java, classes define custom data types that are used to create objects through the new operator. Objects can then access class members like fields and methods using dot notation. Constructors initialize new objects and polymorphism allows classes to share method names while implementing them differently based on object type.
Download as PPTX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
20 views
Chapter 3
Classes define objects and their properties and behaviors. An object is an instance of a class and has both state, stored in fields, and behavior, exposed through methods. In Java, classes define custom data types that are used to create objects through the new operator. Objects can then access class members like fields and methods using dot notation. Constructors initialize new objects and polymorphism allows classes to share method names while implementing them differently based on object type.
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 9
Object and Classes
• Object − Objects have states and behaviors. Example: A dog has
states - color, name, breed as well as behaviors – wagging the tail, barking, eating. An object is an instance of a class. • Class − A class can be defined as a template/blueprint that describes the behavior/state that the object of its type support. Objects in Java • Let us now look deep into what are objects. If we consider the real- world, we can find many objects around us, cars, dogs, humans, etc. All these objects have a state and a behavior. • If we consider a dog, then its state is - name, breed, color, and the behavior is - barking, wagging the tail, running. • If you compare the software object with a real-world object, they have very similar characteristics. • Software objects also have a state and a behavior. A software object's state is stored in fields and behavior is shown via methods. • So in software development, methods operate on the internal state of an object and the object-to-object communication is done via methods. Class • A class is a user – defined data type with a template that serves to define its properties. Once the class type has been defined we can create “Variables” of that type using declarations that are similar to the basic type declarations. In Java, these variables are termed as instances of classes, which are the actual objects. The basic form of a class definition as : class classname [ extends superclassname ] { [ variable declaration;] [methods declaration;] } Adding Variables • Data is encapsulation in a class by placing data fields inside the body of the class definition. These variables are called instance variables because they are created whenever an object of the class is instantiated. We can declare the instance variables exactly the same way as we declare local variables. e.g. class rectangle { int length; int width; } Adding Methods • A class with only data fields has no life. The objects created by such a class cannot respond to any message. Methods are declared inside the class but immediately after the declaration of instance. The form of method declaration is:- type methodname ( parameter_list) { method_body; } Method declarations have four basic parts :- - The name of the method ( methodname ) - The type of the value the method returns (type) - A list of parameters ( parameter list ) - The body of the method. Creating Objects • Objects in Java are created using the new operator. The new operator creates an objects an objects of the specified class and returns a reference to that object. eg. Rectangle rect1; //declare rect1= new Rectangle (); // instantiate Both statements can be combined into one as shown below : Rectangle rect1 =new Rectangle (); The method Rectangle() is the default constructor Accessing the Class Members • Now that we have created objects, each containing its own set of variables, we should assign values to these variables in the order to use them in our program. Remember, all variables must be assigned values before they are used. Since, we are outside the class, we cannot access the instance variables and the methods directly, to do this, and we must use the concerned object and the dot operator as shown below: objectname.variablename objectname.methodname(parameter list); Constructor • To initialize a object when it is first created. Java supports a special type of method, called a constructor that enables an object to initialize itself they do not specify a return type, not even void. This is because they return the instance of the class itself. Polymorphism • Polymorphism is important feature in OOP concept Polymorphism means the ability to take more than one form. E.g. an operation may exhibit different behavior in different instances. The behavior depends upon the types of data used in the operation. • E.g. Consider, The operation of addition for two numbers, the operation will generate a sum. If the operands are strings, then the operation would produce a third string by concatenation. • In the following figure, a single function name can be used to handle different number and different types of arguments.