Concept of Objects and Classes
Concept of Objects and Classes
Object Oriented Programming: OOP focuses on data rather than procedure and structure. This is a
method where programming is adjusted and modeled on the basis of real life entities and processes.
E.g. C++, Smalltalk ,Java
Characteristics of Oriented Programming:
Programs are divided into what are known as objects.
Data is hidden and cannot be accessed by external methods/functions.
Addition of new data and functions can be done easily whenever necessary.
Class - A class is a prototype or blueprint or layout that defines how an instance of that class should be
i.e. a class is template according to which objects of that class are created.
A class is a group of objects that share common properties, relationship and behaviors.
Thus mobile is a class and Nokia N73 etc are objects of this class.
Abstraction - Abstraction is a way to manage the complexity of any system. Abstraction refers to the act
of representing the essential features without including the complexity of background details.
Ex. While using your computer system you need to know the basic operations of putting on, shutting down
and knowledge to use the peripherals. You need not know the internal functioning or the complex
architecture of the system
Encapsulation - The wrapping up of data and functions into a single unit called is known as
encapsulation.
Encapsulation is a way of implementing abstraction. Encapsulation is a mechanism that associates the code
and data it manipulates and keep them safe from outside interference and misuse.
Inheritance - Inheritance is a process by which a class called derived class/sub class/child class inherits
properties of another class called base class/super class/parent class.
Greatest advantage of inheritance is code reusability. Java used the extends keywords to set the relationship
between derived class and base class. Derived class uses keyword super to refer to the members of the base
class.
Ex:
Vehicle
Four Wheeler
Car
Swift
Software Objects - A software object is a collection of variables, which represent the attributes and
related methods which implement the behaviour.
The current values of instance variables define the state of an object. Software objects represent the
attributes through variables called instance variable.
Account
accountNumber Withdraw()
clientName
accountType accountNumber
E enquire()
clientName
balance accountType
Deposit( ) balance
Withdraw( ) deposit()
Enquire( )
Polymorphism - Polymorphism has been derived from Greek word polymorphous i.e. Polus (many)
morpha (form). Thus polymorphism means having many forms.
When same message is sent to different objects. The interpretation of the message and response generated
will be different. This is termed as polymorphism.
Ex. ‘+’ when used with numbers results in their addition: 3+7 gives 10. When use with strings results in their
concatenation: “good” + “morning” gives good morning
Q.1: Explain a class as an object factory?
Ans: A class represent a group of objects of the same kind. Ex. Class of cellphone represent Samsung j7, Nokia
N73 etc. Class of car represent – Zen, Ford, SUV etc. Objects with the same attributes (Structure, name, Size…)
and behaviours (function) are grouped into a class. Each class can produced number of objects, So it can be
called a factory of objects.
TYPE CONVERSION: The process of conversion of one data type to another data type is called type conversion.
There are two type of conversion in java.
1. Implicit type conversion:- This is a conversion which is done by compiler without programmer intervention.
Implicit type conversion is performed automatically. Whin an expression contains operand of different types,
lower types are automatically converted to higher types. This is also referred to as type promotion
Ex. char ch= ‘a’; 97 * Destination is larger than source
int x= 7 * The two types are compatible
Sopln( ch + x); ----->
2. Explicit type conversion: This type of conversion is exclusively done by the user. User can convert all the
operands to any specific type. This is also referred to as type casting.
ESCAPE SEQUENCES: These are non-printable characters, they cannot types directly from keyboards.
Ex. \n (next line), ‘\b’ (backspace), ‘\t’ (tab) etc.
HANDLING STRINGS IN JAVA: Strings can be stored in variables of the string type. String is not a primitive type
but a class. String objects can be constructed in number of ways:
(i) Using String Literals:- A String can be created from a String literal. E.g. String s= “HELLO”;
Above statement create a string object s with value HELLO.
(ii) Using new keyword:- String object can be create like other object using new keyword.
String s1=new String (“INDIA”);
(iv) Using character Array:- String object can be create using character array.
E.g. char ch[ ]= {‘H’ , ‘E’ , ‘L’ , ‘L’ , ‘O’};
String str = new String (ch);
Above statement create a string object str, from character array ch[ ] , thus str contains HELLO.
DECLARING A VARIABLE : In java all variables must be declared before they are used. The general form of
variable declaration in given below:
int x,y,z ; // declare 3 int type variable
int a=5, b, c=10; // declare 3 int type variable & initialize 2
double p=3.14159; // declare a double type variable & initialize.
DYNAMIC INITIALIZATION : Java allows variable to be initialize during the execution of program.
int a = 16; //simple initialization
int b = a*a ; // dynamic initialization
double c= Math.sqrt(b); //dynamic initialization
THE SCOPE AND LIFETIME OF VARIABLES: Java allows variable to be defined inside any block enclosed in
curly braces. A block define a scope. The scope determine, what the area over which variable is accessible.
Lifetime of a variable refers to the duration for which a variable exists in the computer memory “Scope
generally refers to the program region within which a variable is accessible”.
WRAPPER CLASSES: Java provide classes that correspond to each other of the simple type. There classes
encapsulate, (or wrap) the primitive type within a class. Thus they are referred to as type wrapper. Wrapper
classes are available for all primitive data type.
NON- PRIMITIVE OR REFERENCE DATA TYPE: Reference in java is a variable whose value is an address.
E.g: int a =5; Here variable a has been initialized to value 5. This value (5) is called rvalue (actual read value)
and the memory location of variable a is known as lvalue (location value).
Reference data type deal with lvalue, Array, Classes and interfaces are reference data type. This arrays, object
and interfaces are references i.e. contain address of location instead of value stored at that location.
1. INSTANCE VARIBALE: Objects store their individual states in variables called instance variable. Each
object of the class has its own set of values for these variables. Thus we can say that these are related
to objects(instance of the class). Hence these variables are known as instance variables.
These variables take default values if not initialized.
2. Class Variables (Static fields): These are related to a class and none of the individual objects. The
variables defined with static keywords are shared by all objects. Here objects do not store an individual
value but they are forced to share it among themselves. These variable are declared as “static fields”
using the static keywords. So these variable are like global variable which are same for all objects of the
same class.
3. Local Variables: The variables defined in a method or block of code only. These variables don’t take
default values if not initialized. These values are required to be initialized before using them.