64
Recall What is meant by OOP? What are classes and objects? What are the differences between OOP and Procedure oriented Programming?

Introduction to java and oop

Embed Size (px)

Citation preview

Page 1: Introduction to java and oop

Recall

• What is meant by OOP?

• What are classes and objects?

• What are the differences between OOP and Procedure oriented

Programming?

Page 2: Introduction to java and oop

Introduction to OOP with java

Page 3: Introduction to java and oop

Java

• Oop paradigmJava 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

Page 4: Introduction to java and oop

C Vs javaSyntax comparison

Page 5: Introduction to java and oop

C Vs java

• int

• Char

• Float

• double

‚Exactly the same as of left side‛

Data Types

Page 6: Introduction to java and oop

C Vs java

• Conditional control structures

– If

– If else

– Switch

• Loops

– for

– while

– Do while

‚Exactly the same as of left side‛

Control Structures

Page 7: Introduction to java and oop

C Vs java

int sum(int a, int b)

{

Int c;

c=a + b

return c;

}

sum(12,13)

‚Exactly the same as of left side‛

functions

Page 8: Introduction to java and oop

C Vs java

int a[] = {1000, 2, 3, 50};

int a[10];

int a[]={1,2,3,4} // not a preferred way

int[] a={1,2,3,4} //preferred way

int[] myList = new int[10];

Arrays

Page 9: Introduction to java and oop

C Vs Java

Output

printf(“ value of a = %d”,a);

Printf(“a = %d and b= %d”,a,b);

Input

scanf(“%d ", &a);

Scanf(“%d”,&a,&b);

Output

System.out.println(“hello baabtra”);

Input

Scanner sc = new scanner(System.in);

int i = sc.nextInt();

(import java.util.Scanner)

Input / Output

OutIn

System

Println()Print()Write()

PrintStream

Page 10: Introduction to java and oop

C Vs java

char str*+ = ‚hello‛;

‚String is character array‛

String str=‚Hello‛;

‚String is an object ‚

Strings

Page 11: Introduction to java and oop

C Vs java

• As we are well aware c program

execution begins at main() function

Eg: main()

{

Printf(‚hello world‛);

printHello();

}

Same as C, differs that the main() will be

a member function of some class.

Name of the class and file should be

the same.

class myFirstProgram

{

public static void main(String[] args)

{

System.out.println(‚hello baabtra‛);

}

}

main() function

Page 12: Introduction to java and oop

What’s New in java?

Page 13: Introduction to java and oop

What’s New in java?

• OOP concepts

• JVM and Platform independency

• Automatic garbage collection

Page 14: Introduction to java and oop

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.

Page 15: Introduction to java and oop

Objects in real World

Page 16: Introduction to java and oop

Real World Objects

Page 17: Introduction to java and oop

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

Page 18: Introduction to java and oop

Objects

I have an identity: I'm Volkswagen

I have different properties.My color property is green

My no:of wheel property is 4

I can perform different actionsI can be drived

I can consume fuelI can play Music

I have an identity: I'm Suzuki

I have different properties.My color property is silver

My no:of wheel property is 4

I can perform different actionsI can be drived

I can consume fuelI can play Music

Page 19: Introduction to java and oop

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

Page 20: Introduction to java and oop

Objects in software World

Page 21: Introduction to java and oop

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

Page 22: Introduction to java and oop

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

Page 23: Introduction to java and oop

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.

Page 24: Introduction to java and oop

How to create class in Java

public class shape

{

private Int width;

private Int height;

public Int calculateArea()

{

return x*y

}

}

Page 25: Introduction to java and oop

How to create class in Java

public class shape

{

private Int width;

private Int height;

public Int calculateArea()

{

return x*y

}

}

Is the access specifier

Page 26: Introduction to java and oop

How to create class in Java

public class shape

{

private Int width;

private Int height;

public Int calculateArea()

{

return x*y

}

}

Is the keyword for creating a class

Page 27: Introduction to java and oop

How to create class in Java

public class shape

{

private Int width;

private Int height;

public Int calculateArea()

{

return x*y

}

}

Is the name of the class

Page 28: Introduction to java and oop

How to create class in Java

public class shape

{

private Int width;

private Int height;

public Int calculateArea()

{

return x*y

}

}

Are two variable that referred as the properties. Normally kept private and access using getters and setters. We will discuss getters and setters later in this slide

Page 29: Introduction to java and oop

How to create class in Java

public class shape

{

private Int width;

private Int height;

public Int calculateArea()

{

return x*y

}

}

Is the only member function of the class

Page 30: Introduction to java and oop

How to create objects in java

shape rectangle = new shape();

rectangle.width=20;

recangle.height=35;

rArea=rectangle.calculateArea();

This is how we create an object in java

rectangle

Height: width:

calculateArea(){return height*width;

}

Page 31: Introduction to java and oop

How to create objects in C++

shape rectangle = new shape();

rectangle.width=20;

recangle.height=35;

rArea=rectangle.calculateArea();

Is the class name

Page 32: Introduction to java and oop

How to create objects in C++

shape rectangle = new shape();

rectangle.width=20;

recangle.height=35;

rArea=rectangle.calculateArea();

Is the object name which we want to create

Page 33: Introduction to java and oop

How to create objects in C++

shape rectangle = new shape();

rectangle.width=20;

recangle.height=35;

rArea=rectangle.calculateArea();

“new” is the keyword used in java to create an object

Page 34: Introduction to java and oop

How to create objects in C++

shape rectangle = new shape();

rectangle.width=20;

recangle.height=35;

rArea=rectangle.calculateArea();

What is this???It looks like a function because its having pair of parentheses (). And also its having the same name of our class . But what is it used for ?? We will discuss it soon . Just leave it as it is for now

Page 35: Introduction to java and oop

How to create objects in Java

shape rectangle = new shape();

rectangle.width=20;

recangle.height=35;

rArea=rectangle.calculateArea();

Setting up the property values of object “rectangle”

rectangle

width: 20Height: 35

calculateArea(){return width*height;

}

Page 36: Introduction to java and oop

How to create objects in Java

shape rectangle = new shape();

rectangle.width=20;

recangle.height=35;

rArea=rectangle.calculateArea();

Calling the method calculateArea()

rectangle

width: 20Height: 35

calculateArea(){return 20*35;

}

Page 37: Introduction to java and oop

Example

Class : shape

Height:35 width:20

Object rectangle

calculateArea(){Return 20*35}

Height:10 width:10

Object square

calculateArea(){Return 10*10;}

Member variablesHeightwidth

Member functioncalculateArea{return height*width;

}

Page 38: Introduction to java and oop

What we just did was

• Created an object

shape rectangle = new shape();

• Same like we declare variable. eg: int a;

• And assigned values for it

recangle.height=35;

same like we assign variable value. eg: a=10;

Rectangle

Width:Height:

calculateArea(){return width*height;

}

Rectangle

width: 20Height: 35

calculateArea(){return 20*35;

}

Page 39: Introduction to java and oop

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;

Page 40: Introduction to java and oop

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‛

Page 41: Introduction to java and oop

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

Page 42: Introduction to java and oop

public class shape

{

private Int width;

private Int height;

private Int calculateArea()

{

return x*y

}

}

How to create Constructors

Page 43: Introduction to java and oop

public class shape

{

private Int width;

private Int height;

shape(int height,in width)

{

this.width=width;

this.height=height;

}

private Int calculateArea()

{

return x*y

}

}

Shape rectangle=new shape(20,35);

Constructor

Rectangle

width: 20Height: 35

calculateArea(){return 20*35;

}

How to create Constructors

Page 44: Introduction to java and oop

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

Page 45: Introduction to java and oop
Page 46: Introduction to java and oop

Getters and Setters

• There are several occasion we need to validate the data when assigning values

into it

• For eg: rectangle.width=-10;

rectangle.height=-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

Rectangle

width: -10Height: -12

calculateArea(){return -10*-12;

}

Page 47: Introduction to java and oop

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

Page 48: Introduction to java and oop

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

Page 49: Introduction to java and oop

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

Setter function

Page 50: Introduction to java and oop

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

Getter function

Page 51: Introduction to java and oop

OOP features

»Abstraction

»Encapsulation

»Polymorphism

»Inheritance

Page 52: Introduction to java and oop

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.

Page 53: Introduction to java and oop

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

Page 54: Introduction to java and oop

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

Page 55: Introduction to java and oop

public class shape

{

protected Int width;

protected Int height;

Int calculateArea()

{

return x*y;

}

int drawShape()

{

system.out.println(“shape”)

}

}

public class threedimensionalshape extends shape

{

private Int depth;

Int calculateVolume()

{

return width*heigh*depth;

}

}

Example

Page 56: Introduction to java and oop

Example

• Shape rectangle = new rectangle();

• threedimensionalshape cube =new threedimensionalshape()

Rectangle

Width:Height:

calculateArea(){Return width*height

}

cubeWidth:Height:depth

calculateArea(){Return width*height}

Int calculateVolume(){return width*height*depth;}

Page 57: Introduction to java and oop

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

Page 58: Introduction to java and oop

public class shape

{

Int calculateArea(int width, int height)

{

return width * width;

}

Float calculateArea(int radius)

{

return (22/7) * radius * radius

}

}

public class example

{

Public static void main{

shape sh =new shape();

System.out. Println(sh.calculateArea(20, 10));

System.out.println(sh.calculateArea(10));

}

}

Output:

200

314.28

Example – Static polymorphism

Page 59: Introduction to java and oop

public class shape

{

Void drawShape()

{

system.out.println(“Shape”)

}

}

public class ThreeDshape extends shape

{

Void drawShape()

{

system.out.println(“3D shape”)

}

}

public class example

{

Public static void main{

shape sh =new shape();

ThreeDshape tds =new ThreeDshape ();

Sh.drawShape();

sh=tds;

Sh.drawShape;

}

}

Output:

Shape

3D shape

Example – Dynamic polymorphism

Page 60: Introduction to java and oop

Static and Dynamic polymorphism

• Achieved using Overloading

• Signature difference

• Inheritance not required for

implementation

• Decision at compile time

• Early binding

• Achieved by Overriding

• Uses inheritance and virtual

functions

• Inheritance required for

implementation

• Decision at runtime

• Late binding

Page 61: Introduction to java and oop

Questions?

‚A good question deserve a good grade…‛

Page 62: Introduction to java and oop

End of day

Page 63: Introduction to java and oop

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

Page 64: Introduction to java and oop

Contact Us

Emarald Mall (Big Bazar Building)Mavoor Road, Kozhikode,Kerala, India.Ph: + 91 – 495 40 25 550

NC Complex, Near Bus StandMukkam, Kozhikode,Kerala, India.Ph: + 91 – 495 40 25 550

Start up VillageEranakulam,Kerala, India.

Email: [email protected]