Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 17

Classes

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();

• Object references are used to store objects.


new operator

Dynamically allocates memory for an object

Creates the object on the heap

Returns a reference to it

The reference is then stored in the variable


Encapsulation
• Combine an object’s data and methods into a single unit is known as
data encapsulation
• A car’s dashboard hides the complexity and internal workings of its
engine.
• Implemented using the concept of access specifiers
• public, private etc.
Methods
• Constructor
• Accessors
• Mutators
Constructor
• Constructor is automatically invoked whenever an object of the class
is created
• Rules to define a constructor
• A constructor has the same name as the class name
• A constructor should not have a return type
• A constructor can be defined with any access specifier (like private,
public)
• A class can contain more than one constructor
Static in Java
• Static class members are the members of a class that do not belong to an instance of a
class
• We can access static members directly by prefixing the members with the class name
• ClassName.staticVariable
• ClassName.staticMethod(...)

• 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

You might also like