itft-classes and object in java
DESCRIPTION
introduction, class definition, the class hierarchy,Creating Objects,Syntax of object, Constructor,ExampleTRANSCRIPT
![Page 1: ITFT-Classes and object in java](https://reader034.vdocuments.mx/reader034/viewer/2022052410/555a5f25d8b42ae7218b45d9/html5/thumbnails/1.jpg)
Classes and object
![Page 2: ITFT-Classes and object in java](https://reader034.vdocuments.mx/reader034/viewer/2022052410/555a5f25d8b42ae7218b45d9/html5/thumbnails/2.jpg)
Introduction
• The class is at the core of Java.
• It is the logical construct upon which the entire Java language is built because it defines the shape and nature of an object.
• As such, the class forms the basis for object-oriented programming in Java.
![Page 3: ITFT-Classes and object in java](https://reader034.vdocuments.mx/reader034/viewer/2022052410/555a5f25d8b42ae7218b45d9/html5/thumbnails/3.jpg)
What is a class?
• A class consists of• a collection of fields, or variables, very much like the
named fields of a struct
• all the operations (called methods) that can be performed on those fields
• can be instantiated
• A class describes objects and operations defined on those objects
![Page 4: ITFT-Classes and object in java](https://reader034.vdocuments.mx/reader034/viewer/2022052410/555a5f25d8b42ae7218b45d9/html5/thumbnails/4.jpg)
Defining Classes• A class is declared by using the keyword
class. The general syntax for a class declaration is
<modifier> class <className> { }
• Where <className> specifies the name of the class
• class is the keyword, and
• The <modifier> specification is optional, and specifies some characteristics of the class.
![Page 5: ITFT-Classes and object in java](https://reader034.vdocuments.mx/reader034/viewer/2022052410/555a5f25d8b42ae7218b45d9/html5/thumbnails/5.jpg)
The class hierarchy
• Classes are arranged in a hierarchy
• The root, or topmost, class is Object
• A class may have subclasses
• Each class inherits all the fields and methods of its (possibly numerous) superclasses
![Page 6: ITFT-Classes and object in java](https://reader034.vdocuments.mx/reader034/viewer/2022052410/555a5f25d8b42ae7218b45d9/html5/thumbnails/6.jpg)
Methods
• 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.
![Page 7: ITFT-Classes and object in java](https://reader034.vdocuments.mx/reader034/viewer/2022052410/555a5f25d8b42ae7218b45d9/html5/thumbnails/7.jpg)
Creating Objects
• When you write a class, you must keep in mind the objects that will be created from it, which correspond to the objects in the problem that is being solved by the program. You may also look upon the classes as data types.
• for example, of a primitive data type and assign it a value, as follows:
int i=0;
![Page 8: ITFT-Classes and object in java](https://reader034.vdocuments.mx/reader034/viewer/2022052410/555a5f25d8b42ae7218b45d9/html5/thumbnails/8.jpg)
Syntax of object• Similarly, you can declare a variable (a reference
variable) of a class and assign it a value with the following syntax:
<className> <variableName> = new <classConstructor>
• Eg. Room1 obj = new Room1();
• Where <variableName> is the name of the object and you choose this name. <className> is the name of an existing class.
![Page 9: ITFT-Classes and object in java](https://reader034.vdocuments.mx/reader034/viewer/2022052410/555a5f25d8b42ae7218b45d9/html5/thumbnails/9.jpg)
Using Object
• New operator dynamically allocates memory for an object and returns reference to it.
Fruit plum=new Fruit();
int cals;
cals = plum.total_calories();
• Dot operator allows you to access (public) data/methods inside Fruit class
![Page 10: ITFT-Classes and object in java](https://reader034.vdocuments.mx/reader034/viewer/2022052410/555a5f25d8b42ae7218b45d9/html5/thumbnails/10.jpg)
Constructor
• Constructor initializes an object immediately on creation.
• Constructor is automatically called immediately after the object is created.
• Constructor name is same as class name and have no return type not even void.
![Page 11: ITFT-Classes and object in java](https://reader034.vdocuments.mx/reader034/viewer/2022052410/555a5f25d8b42ae7218b45d9/html5/thumbnails/11.jpg)
Example
Class Box
{
int w,h,d;
Box() { //contructor
w=10;
h=10;
d=10; }
public static void main(String str[])
{
Box b= new Box() }
}
![Page 12: ITFT-Classes and object in java](https://reader034.vdocuments.mx/reader034/viewer/2022052410/555a5f25d8b42ae7218b45d9/html5/thumbnails/12.jpg)