What Is Class and Objects? A Class Is A Template For An Object and Object Is A Instance of A Class
A class is a template that defines the characteristics of an object. An object is an instance of a class. A class contains variables to store data and methods to perform actions. When an object is created using the new operator, memory is allocated and a constructor initializes the object. Classes can define public or private members and static methods.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
75 views13 pages
What Is Class and Objects? A Class Is A Template For An Object and Object Is A Instance of A Class
A class is a template that defines the characteristics of an object. An object is an instance of a class. A class contains variables to store data and methods to perform actions. When an object is created using the new operator, memory is allocated and a constructor initializes the object. Classes can define public or private members and static methods.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 13
what is class and objects?
A class is a template for an object
and object is a instance of a class The General Form of a Class A class is declared by use of the class keyword • "A home is a house that has a family and a pet.“ 1. public class Home extends House { 2. Family inhabitants; 3. Pet thePet; 4. } The General Form of a Class class classname { type instance-variable1; type instance-variable2; // ... type instance-variableN ; type methodname1 (parameter-list) { // body of method } • The data, or variables, defined within a class are called instance variables. • The code is contained within methods. • Collectively, the methods and variables defined within a class are called members of the class • A Simple Class class Box { double width; double height; double depth; } Object creation • The new operator dynamically allocates (that is, allocates at run time) memory for an object • Box mybox = new Box(); • // create a Box object called mybox • That is instance of object Introducing Methods • classes usually consist of two things: • instance variables and methods. Adding a Method to the Box Class class Box { double width; double height; double depth; // display volume of a box void volume() { System.out.print("Volume is "); System.out.println(width * height * depth); } } class BoxDemo3 { public static void main(String args[]) { Box mybox1 = new Box(); Box mybox2 = new Box(); // assign values to mybox1's instance variables mybox1.width = 10; mybox1.height = 20; mybox1.depth = 15; /* assign different values to mybox2's instance variables */ mybox2.width = 3; mybox2.height = 6; mybox2.depth = 9; // display volume of first box mybox1.volume(); // display volume of second box mybox2.volume(); } } Return keyword return value; • Here,value is the value returned. • double volume() { return width * height * depth; } Adding a Method That Takes Parameters int square(int i) { return i * i; } Now,square( ) will return the square of whatever value it is called with. That is, square( ) is now a general-purpose method that can compute the square of any integer value, rather than just 10. Here is an example: int x, y; x = square(5); // x equals 25 x = square(9); // x equals 81 y = 2; x = square(y); // x equals 4 Constructors • It can be tedious to initialize all of the variables in a class each time an instance is created. • A constructor initializes an object immediately upon creation. It has the same name as the class in which it resides and is syntactically similar to a method. • Once defined, the constructor is automatically called immediately after the object is created, before the new operator completes. Introducing Access Control • Java’s access specifiers are public , private , and protected . Java also defines a default access level. • Protected applies only when inheritance is involved. • When a member of a class is modified by the public specifier, then that member can be accessed by any other cod e. • When a member of a class is specified as private , then that member can only be accessed by other members of its class. • Now you can understand why main( ) has always been • preceded by the public specifier. Understanding static • You can declare both methods and variables to be static . • The most common example of a static member is main( ) . main( ) is declared as static because it must be called before any objects exist. • Methods declared as static have several restrictions: They can only call other static methods. They must only access static data. They cannot refer to this or super in any way