Beruflich Dokumente
Kultur Dokumente
• int
• Char ‚Exactly the same as of left side‛
• Float
• double
C Vs java
Control Structures
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‛;
• OOP concepts
• JVM and Platform independency
• Automatic garbage collection
OOP Concept
}
}
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;
}
} 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
}
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
Start up Village
Eranakulam,
Kerala, India.
Email: info@baabtra.com