Sie sind auf Seite 1von 64

Recall

• What is meant by OOP?


• What are classes and objects?
• What are the differences between OOP and Procedure oriented
Programming?
Introduction to OOP with java
Java
• Oop paradigm
Java is a computer programming language that is class-based, object-
oriented and makes maximum out of it

• “Write once run anywhere”


Java applications are typically compiled to bytecode (.class file) that can run
on any Java virtual machine(JVM) regardless of computer architecture
C Vs java
Syntax comparison
C Vs java
Data Types

• int
• Char ‚Exactly the same as of left side‛
• Float
• double
C Vs java
Control Structures

• Conditional control structures


– If
– If else
– Switch ‚Exactly the same as of left side‛
• Loops
– for
– while
– Do while
C Vs java
functions

int sum(int a, int b)


{
Int c; ‚Exactly the same as of left side‛
c=a + b
return c;
}
sum(12,13)
C Vs java
Arrays
int a[] = {1000, 2, 3, 50}; int a[]={1,2,3,4} // not a preferred way
int[] a={1,2,3,4} //preferred way

int a[10]; int[] myList = new int[10];


C Vs Java
Input / Output
Output Output
printf(“ value of a = %d”,a); System.out.println(“hello baabtra”);
Printf(“a = %d and b= %d”,a,b);

Out Println()
In Print()
System Write()

PrintStream

Input Input
scanf(“%d ", &a); Scanner sc = new scanner(System.in);
Scanf(“%d”,&a,&b); int i = sc.nextInt();

(import java.util.Scanner)
C Vs java
Strings
char str*+ = ‚hello‛; String str=‚Hello‛;

‚String is character array‛ ‚String is an object ‚


C Vs java
main() function
• As we are well aware c program Same as C, differs that the main() will be
execution begins at main() function a member function of some class.
Name of the class and file should be
the same.
Eg: main() class myFirstProgram
{ {
Printf(‚hello world‛); public static void main(String[] args)
printHello(); {
} System.out.println(‚hello baabtra‛);
}
What’s New in java?
What’s New in java?

• OOP concepts
• JVM and Platform independency
• Automatic garbage collection
OOP Concept

• Object-oriented programming (OOP) is a style of programming


that focuses on using objects to design and build applications.

• Think of an object as a model of the concepts, processes, or


things in the real world that are meaningful to your application.
Objects in real World
Real World
Objects
Objects in real world
• Object will have an identity/name
 Eg: reynolds, Cello for pen. Nokia,apple for mobile

• Object will have different properties which describes them best


 Eg:Color,size,width

• Object can perform different actions


 Eg: writing,erasing etc for pen. Calling, texting for mobile
Objects
I have an identity: I have different properties.
My color property is green
I'm Volkswagen
My no:of wheel property is 4

I have different properties.


My color property is silver
My no:of wheel property is 4
I can perform different actions
I can be drived
I can consume fuel
I can play Music

I can perform different actions


I have an identity: I can be drived
I'm Suzuki I can consume fuel
I can play Music
How these objects are created?
• All the objects in the real world are created out of a basic prototype
or a basic blue print or a base design
Objects in software World
Objects in the software world
• Same like in the real world we can create objects in computer
programming world
– Which will have a name as identity
– Properties to define its behaviour
– Actions what it can perform
How these objects are created
• We need to create a base design which defines the properties and
functionalities that the object should have.
• In programming terms we call this base design as Class

• Simply by having a class we can create any number of objects of


that type
Definition
• Class : is the base design of objects
• Object : is the instance of a class

• No memory is allocated when a class is created.


• Memory is allocated only when an object is created.
How to create class in Java
public class shape
{
private Int width;
private Int height;
public Int calculateArea()
{
return x*y
}
}
How to create class in Java
public class shape
{
Is the access specifier
private Int width;
private Int height;
public Int calculateArea()
{
return x*y
}
}
How to create class in Java
Is the keyword for
public class shape creating a class
{
private Int width;
private Int height;
public Int calculateArea()
{
return x*y
}
}
How to create class in Java
Is the name of the class
public class shape
{
private Int width;
private Int height;
public Int calculateArea()
{
return x*y
}
}
How to create class in Java
public class shape
{ Are two variable that
referred as the
private Int width;
properties. Normally
private Int height; kept private and access
public Int calculateArea() using getters and setters.
We will discuss getters
{ and setters later in this
return x*y slide

}
}
How to create class in Java
public class shape
{
private Int width;
private Int height;
Is the only member
public Int calculateArea() function of the class
{
return x*y
}
}
How to create objects in java
This is how we create an
shape rectangle = new shape(); object in java

rectangle.width=20; rectangle

Height:
recangle.height=35; width:

rArea=rectangle.calculateArea(); calculateArea()
{
return height*width;
}
How to create objects in C++
Is the class name
shape rectangle = new shape();
rectangle.width=20;
recangle.height=35;
rArea=rectangle.calculateArea();
How to create objects in C++
Is the object name which
shape rectangle = new shape(); we want to create

rectangle.width=20;
recangle.height=35;
rArea=rectangle.calculateArea();
How to create objects in C++
“new” is the keyword used
shape rectangle = new shape(); in java to create an object

rectangle.width=20;
recangle.height=35;
rArea=rectangle.calculateArea();
How to create objects in C++
What is this???
shape rectangle = new shape(); It looks like a function
because its having pair of
rectangle.width=20; parentheses (). And also
its having the same name
of our class . But what is it
recangle.height=35; used for ??
We will discuss it soon .
rArea=rectangle.calculateArea(); Just leave it as it is for now
How to create objects in Java
Setting up the property
shape rectangle = new shape(); values of object
“rectangle”
rectangle.width=20; rectangle

recangle.height=35; width: 20
Height: 35

rArea=rectangle.calculateArea(); calculateArea()
{
return width*height;
}
How to create objects in Java
shape rectangle = new shape(); Calling the method
calculateArea()
rectangle.width=20; rectangle

recangle.height=35; width: 20
Height: 35

rArea=rectangle.calculateArea(); calculateArea()
{
return 20*35;
}
Example
Object rectangle
Height:35
width:20
calculateArea()
{
Class : shape Return 20*35
}
Member variables
Height
width
Member function
calculateArea
{ Object square
return height*width; Height:10
} width:10

calculateArea()
{
Return 10*10;
}
What we just did was
• Created an object Rectangle
Width:
shape rectangle = new shape(); Height:
calculateArea()
• Same like we declare variable. eg: int a; {
return
width*height;
}

• And assigned values for it


Rectangle
recangle.height=35; width: 20
Height: 35
same like we assign variable value. eg: a=10; calculateArea()
{
return 20*35;
}
So, Can i assign values into an object at the time
of its creation(known as initialization)??
Something that we do with variables like int a=14;
So, Can i assign values into an object at the time
of its creation??
Something that we do with variables like int a=14;

‚Yes, you can . For that purpose we use something called


constructor‛
Constructors
• Constructors are just a method like any other methods in the
class but having the same name of the class.
• It is used to initialise the properties of an object
• will not have any return type, not even void
• If no user defined constructor is provided for a class, the implicit constructor
initializes the member variables to its default values
– numeric data types are set to 0
– char data types are set to null character(‘\0’)
– boolean data types are set to false
– reference variables are set to null
How to create Constructors
public class shape
{
private Int width;
private Int height;

private Int calculateArea()


{
return x*y
}
}
How to create Constructors
public class shape
{
private Int width;
private Int height;
Constructor
shape(int height,in width)
{
this.width=width;
this.height=height;
}

private Int calculateArea()


{ Rectangle
return x*y width: 20
} Height: 35

} calculateArea()
{
Shape rectangle=new shape(20,35); return 20*35;
}
Access Specifier
• Access specifies defines the access rights for the statements or
functions that follows it until another access specifier or till the
end of a class.
• The three types of access specifiers are
– Private
– Public
– Protected
– default
Getters and Setters
• There are several occasion we need to validate the data when assigning values
into it Rectangle
width: -10
• For eg: rectangle.width=-10; Height: -12

rectangle.height=-12; calculateArea()
{
return -10*-12;
}

• Suppose i want to check whether the user enters a negative value and if so i
wish to assign 0 to the properties
• In such scenarios we will create a method to do the above said actions
Getters and Setters
• Getters and setters are simply two function that gets and sets the value of
class properties.
• Setter
– Rectangle.width=15; // Normally this is how we set the values of class properties.
Rectangle.setWidth(15); // calling a method named ‚setWidth‛ so that the
method will set the property width to 15
• Getter
– Rectangle.width; // Normally this is how we get the values of class properties.
Rectangle.getWidth(); // calling a method named ‚getWidth‛ so that the method
will get the value of the property width
How to create getters and setters
public class shape
{
private Int width;
private Int height;
Int calculateArea()
{
return x*y;
}
Public setWidth(int a)
{
if (a>0)
width=a;
else
width=0
}
Public Int getWidth()
{
return width
}
}
How to create getters and setters
public class shape
{
private Int width;
private Int height;
Int calculateArea()
{
return x*y;
}
Public setWidth(int a) Setter function
{
if (a>0)
width=a;
else
width=0
}
Public Int getWidth()
{
return width
}
}
How to create getters and setters
public class shape
{
private Int width;
private Int height;
Int calculateArea()
{
return x*y;
}
Public setWidth(int a)
{
if (a>0)
width=a;
else
width=0
}
Public Int getWidth() Getter function
{
return width
}
}
OOP features
»Abstraction
»Encapsulation
»Polymorphism
»Inheritance
Abstraction
• Act of representing essential features only and hiding the implementation
details
• For example, a car would be made up of an Engine, but one does not need
to know how the diverse components work inside.
• In our example of class shape ; the users only have to create objects of type
shape like rectangle,square etc.
• And for finding out the area he dont really have to think about how the area
is calculated. He only need to know calling the method calculateArea will
reuslt him with the area.
Encapsulation
• Wrapping up of data(properties) and code(methods) into a single unit is
known as encapsulation
• Encapsulation also includes the process of hiding all the data and methods
within a class from outside world by restricting access to the object's
components.
• In programming languages, encapsulation is accomplished by using private
access specifier
Inheritance
• Is the process where one object acquires the properties of another.
• Inheritance is a type of relationship
– Ex: BMW is a Car
• Reusability of code – Methods and properties of parent class will be
accessible to child class
• In java inheritance is achieved by using keyword ‚extends‛
• Java supports only single inheritance
Example
public class shape public class threedimensionalshape extends shape
{ {
protected Int width; private Int depth;
protected Int height; Int calculateVolume()
Int calculateArea() {
{ return width*heigh*depth;
return x*y; }
}
}
int drawShape()
{
system.out.println(“shape”)
}

}
Example
• Shape rectangle = new rectangle(); Rectangle
Width:
Height:

calculateArea()
{
Return width*height
}

• threedimensionalshape cube =new threedimensionalshape()


cube
Width:
Height:
depth
calculateArea()
{
Return width*height
}

Int calculateVolume()
{
return
width*height*depth;
}
Polymorphism
• Polymorphism is the ability to take more than one form using
the same name
– Eg : function overloading, Operator overloading(not supported in java)
• Functions can have same name but different implementations
• There are two types of polymorphism
– Static
– Dynamic
Example – Static polymorphism

public class shape public class example


{ {
Int calculateArea(int width, int height) Public static void main{
{ shape sh =new shape();
return width * width; System.out. Println(sh.calculateArea(20, 10));
} System.out.println(sh.calculateArea(10));
Float calculateArea(int radius)
{ }
return (22/7) * radius * radius
} }
}
Output:
200
314.28
Example – Dynamic polymorphism
public class example
{
public class shape
Public static void main{
{
shape sh =new shape();
Void drawShape()
ThreeDshape tds =new ThreeDshape ();
{
system.out.println(“Shape”)
Sh.drawShape();
}
sh=tds;
}
Sh.drawShape;
}
public class ThreeDshape extends shape
{ }
Void drawShape()
{ Output:
system.out.println(“3D shape”)
}
Shape
} 3D shape
Static and Dynamic polymorphism
• Achieved using Overloading • Achieved by Overriding
• Signature difference • Uses inheritance and virtual
functions
• Inheritance not required for • Inheritance required for
implementation implementation
• Decision at compile time • Decision at runtime
• Early binding • Late binding
Questions?
‚A good question deserve a good grade…‛
End of day
If this presentation helped you, please visit our
page facebook.com/baabtra and like it.
Thanks in advance.
www.baabtra.com | www.massbaab.com |www.baabte.com
Contact Us

Emarald Mall (Big Bazar Building) NC Complex, Near Bus Stand


Mavoor Road, Kozhikode, Mukkam, Kozhikode,
Kerala, India. Kerala, India.
Ph: + 91 – 495 40 25 550 Ph: + 91 – 495 40 25 550

Start up Village
Eranakulam,
Kerala, India.
Email: info@baabtra.com

Das könnte Ihnen auch gefallen