Classes in Java
Classes in Java
Structured Programming
• In structured programming, focus is on the algorithm and importance
is given to the procedure (logic) and not to the data on which these
procedures operate
• The entire problem was divided into a number of smaller units
• Functions/Procedures
• All these units need to work on a data item to produce the result
• The data need to be global
• Global data made the code complex
• As the code size grows, maintaining code becomes difficult
Object Oriented Programming
• Object Oriented Programming
• The entire program is visualized as a number of objects interacting with each
other
• An object is a self-contained entity that contains attributes (data) and
behaviors (functions)
• Car, Telephone, Pen
State and Behavior
Example: Car object
• State
• Current Speed
• Current Gear
• Engine State (Running, Not Running)
• Behavior (Acts on the object and changes state)
• Slow down
• Accelerate
• Stop
• Switch Off Engine
• Start Engine
State and Behavior
Example: Dog Object
• State
• Color
• Breed
• Activity (Barking/Not barking)
• Tail Activity (Wagging/Not Wagging)
• Behavior
• Bark
• Wag Tail
• Eat
What is a Class
• A Class
• Is a blueprint used to create objects.
• It defines the structure and behavior of objects of that class.
• Examples: Animal, Human Being, Automobiles, Bank Account, Customer
What is a Class
• A class contains state and behavior
• State (Member Variables)
• Variables defined inside the class
• Not exposed to external world
• Behavior (Member Methods)
• Functions defined inside the class
• Behavior exhibited by the class to external world
• Exposed to external world
• An object is an instance of a class
Define a class in Java
Class Emp
{
int id;
String name;
void setId(int i){ id=I;}
Void print(){}
}
Member variables
• A variable declared within a class(outside any method) is known as an
instance variable.
• A variable declared within a method is known as local variable.
• Variables with method declarations are known as parameters or
arguments.
Objects and References
• Once a class is defined, you can declare a variable (object reference)
of type class
• Student stud1;
• Employee emp1;
The new operator is used to create an object of that reference type
• Employee emp = new Employee();
Returns a reference to it
• Static variables:
• Shared among all objects of the class
• Only one copy exists for the entire class to use
Method Overloading
• Product(int);
• Product(int, int);
• Product(double);
•Thank You