KEMBAR78
Object Oriented | PDF | Programming | Constructor (Object Oriented Programming)
0% found this document useful (0 votes)
51 views48 pages

Object Oriented

The document discusses object-oriented programming concepts like classes, objects, attributes, behaviors, and how they relate to real-world objects. It explains that classes are blueprints used to create multiple object instances, and that objects have identity, attributes describing their state, and behaviors defining what they can do.

Uploaded by

Raghu Gowda
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views48 pages

Object Oriented

The document discusses object-oriented programming concepts like classes, objects, attributes, behaviors, and how they relate to real-world objects. It explains that classes are blueprints used to create multiple object instances, and that objects have identity, attributes describing their state, and behaviors defining what they can do.

Uploaded by

Raghu Gowda
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 48

Object Oriented

Object
What is Object in Computer Programing

What is Object in Real World


Object in Real Life
• Object is a thing
• So is apple an object in real life?
• Is desk?
• This Laptop?
• A mug?
• They are all objects, these are all things.
• We understand that objects are separate from one another. They have
their own existence, their own identity(object name) that is
independent of other objects. This is a Laptop, and this is a Laptop, but
they are not the same Laptop, they are not the same object. They are
different objects, they have their own identity.
We know that objects have Attribute (Variable)
 A Laptop can be Open or Close.
An apple can be green or red,
A lamp can be off or on.
A mug can be full or empty.
• These are the attributes of any object, things like color, weight, and
size. They describe the current state of an object and the state of one
object is independent of another. We turn one lamp off, it does not turn
all the lamps in the world off.
• And most objects have multiple attributes. A mug can be full or empty
but at the same time it could be black or white or some other color. It
could be large or small.
• In real world objects have behavior (Methods)
a telephone can ring,
an airplane can fly,
• A behavior is specific to the type of object
an apple does not ring,
a telephone does not fly.
• But those three things, identity, attributes, and behavior are the same
three things that describe an object in an object-oriented
programming language.
Object in Computer Programing
Objects have identity separate from other objects. They also have
their own attributes. Information that describes their current state,
and they have their own behavior, things they can do.
Now while in the real world we tend to only use the word object for
things we can see and touch, but in computing we can take it further.
Sure, in a computer program, we often have objects that represent
real world items like car, house, apple, but also a date could be an
object, a time, abank account could be an object, and you can't touch
and hold a bank account in real life.
• But it is still a well-defined idea, and even in real life it meets
our definition of object.
• It has identity. One bank account is separate from another bank
account.
• It has attributes or data that describe its current state. An account
number, a balance, an account holder name.
• It has behavior. You can deposit to a bank account, you can withdraw
from it, you can open it or close it.
• And just as they don't have to be physical items, objects in a
computer program are not just the things with a visual appearance on
the computer screen
• And the entire point of Object-Oriented
Design is not about objects, it's about classes
because we use classes to create objects.
What is Class?
• A class is a blueprint.
• The blueprint example is. If you want to build a house, you make a
blueprint (Map) first. It describes everything about how that house
will be built, but it isn't the house. You then use that blueprint to build
the house
• We write the class then use the class to create the object. And just as
we could use the same blueprint(Map) to build one, two, or a
hundred houses, we can define the class once and then create a
thousand objects based on that one class, but the class comes first.
The class comes first, that's what we write

• A class has a name, attributes and behavior


Creating a Class
• Name(Type): What is it?
• Employee, BankAccount, Student, MarkAttendence

• Attribute(Properties/Data/Variable Name): What Describe it?


• Width, Height, Amount, Roll#, IsPresent

• Behaviour(Operation/Method): What can it do?


• Open, Close, WithDraw, Search, Delete, Insert
Class/Objects

• If we create a BankAccount class?


• What Attribute & Behaviour we create?
• The class says that each object has an
accountNumber, but it doesn't say what the
account number is.
• It says each object will have a balance, but it
doesn't say what that balance is.

• And behavior might say we have got open,


closed, and deposit and withdraw.
• After writing the class, we can then create
objects based on that class
Create Object
• Means we are creating instances of a class,
• each object is an instance of a particular class.
• Now each instance/object-- whether one, two, or a thousand--has its
own identity independent from other objects and its own data and its
own behavior. So the class says that each object has a balance, but
the individual objects say, well, my balance is 500 or -50 or 7500.
Define Fields in a class
• A class can contain any of the following variable types.
• Local variables. variables defined inside methods, constructors or blocks are called local variables.
The variable will be declared and initialized within the method and the variable will be destroyed
when the method has completed.
• Instance variables or Member variable or Fields or Attributes . Instance variables are variables
within a class but outside any method. These variables are instantiated when the class is loaded.
Instance variables can be accessed from inside any method, constructor or blocks of particular
class. Instance variables stores the state of the object. Each class would have its own copy of the
variable.
• private int abc; //fields variables
• public int abc //member variables
• Class variables or static variable. Class variables are variables declared with in a class, outside any
method, with the static keyword.
• public static int abc;
Access Modifier
• Default: has scope only inside the
package.
• Public: scope is visible every where
• Protected: has scope with in the package
and all subclasses.
• Private: has scope with in the class
Constructor
• Every class has a constructor. If we do not explicitly write a
constructor for a class the java compiler builds a default constructor
for that class.
• The main rule of constructors is that they should have the same name
as the class. A class can have more than one constructor.
• Constructor is a special type of method which have no return type
• constructors for initializing new objects or declarations for the fields
• class Box {
• The new Operator dynamically
• double width;
• double height; allocates memory for an Object
• double depth;

public static void main(String args[]) {


• Box mybox = new Box();
• double vol;
• mybox.width = 10;
• mybox.height = 20;
• mybox.depth = 15;
• vol = mybox.width * mybox.height *
mybox.depth;
• System.out.println("Volume is " +
vol);
• }
• }
• class Box { • class BoxDemo {
• public static void main(String args[]) {
• double width; • Box mybox1 = new Box();

• double height; • Box mybox2 = new Box();


• mybox1.width = 10;
• double depth; • mybox1.height = 20;

•} • mybox1.depth = 15;
• mybox2.width = 3;
• mybox2.height = 6;
• mybox2.depth = 9;
• vol = mybox1.width * mybox1.height *
mybox1.depth;
• System.out.println("Volume is " + vol);
• vol = mybox2.width * mybox2.height *
mybox2.depth;
• System.out.println("Volume is " + vol);
• }
• }
• class Box { • class BoxDemo6 {
• double width; • public static void main(String args[]) {
• double height; • Box mybox1 = new Box();
• double depth; • double vol;
• vol = mybox1.width * mybox1.height * mybox.1depth;
• Box() { • System.out.println("Volume is " + vol);

System.out.println("Constructing Box");
• }
• width = 10;
• }
• height = 10;
• depth = 10;
• }
• }
Assigning Object Reference Variables
• Box b1 = new Box();
• Box b2 = b1
• That is, you might think that b1 and b2 refer to separate and distinct objects. However, this would be wrong.
Instead, after this fragment executes,b1 and b2 will both refer to the same object. The assignment of b1 to
b2 did not allocate any memory or copy any part of the original object. It simply makes b2 refer to the same
object as does b1. Thus, any changes made to the object through b2 will affect the object to which b1 is
referring, since they are the same object
• Although b1 and b2 both refer to the same object, b1 from the original object without affecting the object
b2. For example:
• Box b1 = new Box();
• Box b2 = b1;
• // ...
• b1 = null;
• Here,b1 has been set to null , but b2 still points to the original object. When you assign one object reference
variable to another object reference variable, you are not creating a copy of the object, you are only making
a copy of the reference.
Introducing Methods
• Although it is perfectly fine to create a class that contains only data, it rarely happens. Most of the time you will
use methods to access the instance variables defined by the class
• classes usually consist of two things: instance variables and methods
• This is the general form of a method:
• modifier type name(parameter-list) Exception{
• // body of method
• }
1. Modifiers—such as public, private, protected
2. The return type—the data type of the value returned by the method, or void if the method does not return a
value.
3. The method name—any name .
4. The parameter list in parenthesis—a comma-delimited list of input parameters, preceded by their data types,
enclosed by parentheses, (). If there are no parameters, you must use empty parentheses
5. An exception—is an optional to be discussed later
6. The method body, enclosed between braces—the method's code, including the declaration of local variables,
goes here
• class Box { • class BoxDemo3 {
• double width; • public static void main(String args[]) {
• double height; • Box mybox1 = new Box();
• double depth; • Box mybox2 = new Box();
• mybox1.width =
• void volume() { 10;
• System.out.print("Volume is "); • mybox1.height =
20;
• System.out.println(width * height *
depth); • mybox1.depth =
• } 15;
• }
• mybox2.width = 3;
• mybox2.height = 6;
• mybox2.depth = 9;

• mybox1.volume();
• mybox2.volume();
• }
Returning a Value
1. completes all the statements in the method,
2. reaches a return statement
3. You declare a method's return type in its method declaration. Within the body of the method, you use the
return statement to return the value.
• Any method declared void doesn't return a value. It does not need to contain a return statement, but it may do
so. In such a case, a return statement can be used to branch out of a control flow block and exit the method and
is simply used like this:
• return;
• If you try to return a value from a method that is declared void, you will get a compiler error.
• Any method that is not declared void must contain a return statement with a corresponding return value, like
this:
• return returnValue;
• The data type of the return value must match the method's declared return type; you can't return an integer
value from a method declared to return a boolean.
• class Box { • class BoxDemo4 {
• double width; • public static void main(String args[]) {
• double height; • Box mybox1 = new Box();
• double depth; • Box mybox2 = new Box();
• • double vol;
• double volume() { • mybox1.width = 10;
• return width * height * • mybox1.height = 20;
depth;
• mybox1.depth = 15;
• }
• mybox2.width = 3;
• }
• mybox2.height = 6;
• mybox2.depth = 9;
• vol = mybox1.volume();
• System.out.println("Volume is " +
vol);
• vol = mybox2.volume();
• System.out.println("Volume is " +
vol);
• }
• }
Adding a Method That Takes Parameters
• Parameters allow a method to be generalized
• Here is a method that returns the square of the number 10
• int square()
• {
• return 10 * 10;
• }
• While this method does, indeed, return the value of 10 squared, its use is very limited. However, if you
modify the method so that it takes a parameter, as shown next, then you can make square( ) much more
useful
• int square(int i)
• {
• return i * i;
• }
Argument Passing
• there are two ways that a computer language can pass an argument.
call-by-value and call-by-reference
• call-by-value:- copy of an argument value is pass to a method.
Changes made to the argument value inside the method will have no
effect on the argument
• call-by-reference reference of an argument is pass to a method. Any
changes made inside the method will affect the argument value.
• In java you pass a data type to a method it is pass-by-value when you
pass an object to a method it is pass-by-reference
• class CallByValue {

CallByValue • public static void main(String args[]) {


• Test ob = new Test();
• int a = 15, b = 20;

• class Test {
• System.out.println("a and b before call: " +
• void meth(int i, int j) {
• a + " " + b);
• i *= 2;
• j /= 2; • ob.meth(a, b);
• }
•} • System.out.println("a and b after call: " +
• a + " " + b);

• The output from this program is shown


here: • }
• a and b before call: 15 20 • }

• a and b after call: 15 20


CallByReference • class CallByRef {
• public static void main(String args[]) {
• Test ob = new Test(15, 20);
• class Test {
• int a, b;
• System.out.println("ob.a and ob.b
• before call: " + ob.a + " " + ob.b);
• Test(int i, int j) {
• a = i; • ob.meth(ob);
• b = j;
• } • System.out.println("ob.a and ob.b
• void meth(Test o) { after call: " +ob.a + " " + ob.b);
• o.a *= 2; • }
• o.b /= 2; • }
• } • This program generates the following output:
• } • ob.a and ob.b before call: 15 20
• ob.a and ob.b after call: 30 10
Parameterized Constructor
• class Box { • class BoxDemo7 {
• double width; • public static void main(String args[]) {
• double height; • Box mybox1 = new Box(10, 20, 15);

• double depth; • Box mybox2 = new Box(3, 6, 9);


• double vol;
• Box(double w, double h, double d) {
• vol = mybox1.volume();
• width = w;
• System.out.println("Volume is " + vol);
• height = h;
• vol = mybox2.volume();
• depth = d;
• System.out.println("Volume is " + vol);
• }
• }
• double volume() { • }
• return width * height *
depth;
• }
• }
Returning Objects
• A method can return any type of data, including class types that you
create. For example, in the following program, the incrByTen( )
method returns an object in which the value of a is ten greater than it
is in the invoking object
• class Test { • class RetOb {
• int a; • public static void main(String args[]) {
• • Test ob1 = new Test(2);
• Test(int i) { • Test ob2;
• a = i; • ob2 = ob1.incrByTen();
• } • System.out.println("ob1.a: " +
ob1.a);

• Test incrByTen() {
• System.out.println("ob2.a: " +
• Test temp = new Test(a+10);
ob2.a);
• return temp;
• } • ob2 = ob2.incrByTen();
• }

• System.out.println("ob2.a after
second increase: "+ ob2.a);
• }
• }
Define Last Example
• The output generated by this program is shown here:
• ob1.a: 2
• ob2.a: 12
• ob2.a after second increase: 22

• As you can see, each timeincrByTen( ) is invoked, a new object is created, and a
reference to it is returned to the calling routine. The preceding program makes
another important point
“static” Keyword = Class Variables
• Variables can be declared with the “static” keyword. Example:
• static int y = 0;
• When a variable is declared with the keyword “static”, its called a “class variable”. All instances
share the same copy of the variable. A class variable can be accessed directly with the class,
without the need to create a instance.
• A static variable call any method or constructor
• class T2 { • // each b1 and b2 has separate copies of x
• int x = 0; // instance variable • System.out.println( b1.getX() );
• static int y = 0; // class variable • System.out.println( b2.getX() );

• void setX (int n) { x = n;} • // both have same value


• void setY (int n) { y = n;} • System.out.println( b1.getY() );
• int getX () { return x;} • System.out.println( b2.getY() );
• int getY () { return y;}
• } • // class variable can be used directly without a instance of it.
• //(if changed to T2.x, it won't compile)
• class T1 { • System.out.println( T2.y );
• public static void main(String[] arg) { • T2.y = 7;
• T2 b1 = new T2(); • System.out.println( T2.y );
• T2 b2 = new T2();
• // class variable can be manipulated thru methods as usual
• b1.setX(5); • b1.setY(T2.y+1);
• b2.setX(10); • System.out.println( b1.getY() );
• b1.setY(15); • }
• b2.setY(20); • }
Output Of Last Program
•5
• 10
• 20
• 20
• 20
•7
•8
Instance Metod vs Class Methods
• Methods can also be declared with the keyword “static”. When a
method is declared static, it can be used without having to create a
object first. For example, you can define a collection of math
functions in a class, all static, using them like functions.
• 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. (The keyword super
relates to inheritance and is described Later.)
• // A class with a static method • class T1 {
• public static void main(String[] arg) {
• class T2 {
• static int triple (int n) { • // calling static methods without creating a instance
• System.out.println( T2.triple(4) );
• return 3*n;
• } • // calling static methods thru a instance is also
allowed
•} • T2 x1 = new T2();
• System.out.println( x1.triple(5) );
• }
• }

• Methods declared with “static” keyword are called “class methods”.


Otherwise they are “instance methods”.
Static Methods Cannot Access Non-Static
Variables
Methods declared with “static” cannot access variables declared without “static”. The following gives a
compilation error, unless x is also static.
• class T2 { • class T1 {
• public static void main(String[] arg) {
• int x = 3; • System.out.println( T2.returnIt() );
• static int returnIt () { • }
• }
• return x;
• }
•}
static variables, methods, and blocks

• class UseStatic { • static {


• static int a = 3; • System.out.println("Static block initialized.");
• static int b; • b = a * 4;
• • }
• static void meth(int x) {
• System.out.println("x = " + x); • public static void main(String args[]) {
• System.out.println("a = " + a); • meth(42);
• System.out.println("b = " + b); • }
• } • }


Define last program
• Out put is
• Static block initlize
• X=42
• a=3
• b=12
• The UseStaticclass is loaded, all of the static statements are run. First, a is set to 3, then the static block
executes (printing a message), and finally, b is initialized to a * 4 or 12. Then main( )is called, which
callsmeth( ), passing 42 to x. The threeprintln( )statements refer to the two static variables a and b, as well as
to the local variable x.
Inner Class
• An instance of InnerClass can exist only within an instance of OuterClass and has direct access to the methods and fields of its enclosing
instance. The next figure illustrates this idea.
• class OuterClass {
• ...
• class InnerClass {
• ...
• }
• }
• class Outer { • class InnerClassDemo {
• int outer_x = 100; • public static void main(String args[]) {
• Outer outer = new Outer();
• void test() { • outer.test();
• Inner inner = new Inner(); • }
• inner.display(); • }
• } • OR
• class InnerClassDemo {
• // this is an inner class • public static void main(String args[]) {
• class Inner { • Outer outer = new Outer();
• void display() { • Outer.Inner innerObject =
• System.out.println("display: outer_x = " + outer.new Inner();
outer_x); • outer.test();
• } • innerObject.display();
• } • }
• } • }
• class Outer { • void showy() {
• int outer_x = 100; • System.out.println(y); // error, y not known here!
• void test() { • }
• Inner inner = new Inner(); • }
• inner.display();
• } • class InnerClassDemo {
• // this is an inner class • public static void main(String args[]) {
• Outer outer = new Outer();
• class Inner { • outer.test();
• int y = 10; // y is local to Inner • }
• void display() { • }
• System.out.println("display: outer_x = " + outer_x);
• }
• }
• class Outer {
• int outer_x = 100;
• void test() {
• for(int i=0; i<10; i++) {
• class Inner {
• void display() {
• System.out.println("display: outer_x = " + outer_x);
• }
• }
• Inner inner = new Inner();
• inner.display();
• }
• }
• }
• class InnerClassDemo {
• public static void main(String args[]) {
• Outer outer = new Outer();
• outer.test();
• }}

You might also like