Professional Documents
Culture Documents
Concepts of OOP
Concepts of OOP
A.M.L. Chamini
It is used for:
A.M.L. Chamini
OOP and Java
• Java is a purely an object – oriented language.
• The central idea behind object-oriented programming is to
divide a program into isolated parts called Objects.
• Each object contain two parts.
Data
A.M.L. Chamini
Functions
OOP and Java
A.M.L. Chamini
Terminology
• Class- A collection of data and methods that operate on that data
• Method- A group of statements in a class that handle a task.
• Attribute- A property of an instance of a class.
• Interface- A skeleton class.
• Package- A group of logically related codes (classes & interfaces)
A.M.L. Chamini
Class
A class is a blueprint or prototype that defines the variables
and the methods common to all objects of a certain kind
Object
Object is an element or instance of a class which have the
behaviors of their class.
Data and routines (functions) required to process it are combined into a single entity
called an Object.
A collection of similar objects is known as a class. (Blueprint of Object).
In order to clearly understand the idea of objects, lets us now consider an example.
Suppose we are planning a birthday party and we wish to play some songs available in
our compact disk. there is an index card file, in which each card contains a song’s name,
its author and its playing time in the following format.
Song
name
A.M.L. Chamini
The above mentioned data in the index card file can be used to get the following types of
information about a particular song of interest.
Name of the song ,Name of the Author, Playing time of the song.
Name the routines (functions) to get the above mentioned three types of information
from the index card file as getName(), getAuthor(), and getPlayingTime() and the
routine for instructing the computer to play a song as playSong().
It should be carefully note here that all the above mentioned four routines are concerned
with the three data items.
A.M.L. Chamini
Song
Name
Data
Author
Playingtime
getName()
getAuthor()
Routines (i.e. member function)
getPlayingTime()
playSong()
A.M.L. Chamini
• The data and the related routines are combined to together to form an
object
• Once we define a class, we can declare a group of objects for that class.
• Each object in such a group contains the data and member functions that
• In other words, each object in such a group of objects imitates the exact
Since the Hello.java program is a very simple one involving any input or
output variables, it doesn’t contain any data members.
It contains only one method, namely the main(), which must be present
in every Java stand-alone application.
Since this method is not expected to return any value, the return type of
this method is indicated in the above program as void.
A.M.L. Chamini
A Simple Java Program
(Understanding)
HelloWorld.java
The main method
• Instructs the computer to display the data within brackets to the screen.
• “Hello World !” :String / String Literal. What you need
to display on screen
• System.out.println() moves the cursor to the next line after
displaying the data
A.M.L. Chamini
Exercise - 1
• Write a Java program to display your name and
address in 3 lines.
A.M.L. Chamini
Answer
System.out.println("Aaron Larson");
System.out.println("123 Center Ln.");
System.out.println("Plymouth, MN 33441");
A.M.L. Chamini
Printing Values
• System.out.println() and System.out.print()
A.M.L. Chamini
Variables
♥ Variable is name of reserved area allocated in memory. In
other words, it is a name of memory location.
♥ It is a combination of “vary + able” that means its value can
be changed
A.M.L. Chamini
Variables
A.M.L. Chamini
Variables - Rules
♥ The first character in a variable name should not be a digit.
♥ A variable name may consist of alphabets, digits, the
underscore character and the dollar character.
♥ A keyword should not be use as a variable name. White
spaces are not allowed within a variable name.
♥ A variable name can be of any length
A.M.L. Chamini
Variables
♥ Every variable has a data type.
♥ Every variable has a value.
♥ Every variable is the name of a storage location.
♥ Every variable has its size.
A.M.L. Chamini
Variables – Declaration
♥ Variable can declared as follows
access-specifier data-type variablename
♥ Here,
– access-specifier specifies which methods can access
the variable. Most variables are declared as private.
– data-type refers to the type of the variable. Here is a
valid type declaration statement.
A.M.L. Chamini
Variables –
Types of Variables
A.M.L. Chamini
Variables –
Types of Variables
1. Local Variable
A variable which is declared inside the method is called local variable.
2. Instance Variable
A variable which is declared inside the class but outside the method, is called
instance variable . It is not declared as static.
3. Static variable
A variable that is declared as static is called static variable. It cannot be local.
A.M.L. Chamini
Variables –
Types of Variables
class A
{
int data=50;//instance variable
static int m=100;//static variable
void method()
{
int n=90;//local variable
} //end of method.
}//end of class
A.M.L. Chamini
Data Types
A.M.L. Chamini
Data Types
A.M.L. Chamini
Printing Values
• System.out.println() and System.out.print()
A.M.L. Chamini
Calculations
A.M.L. Chamini
Printing Values
• System.out.println() and System.out.print()
A.M.L. Chamini
Inputting Values from the Keyboard
• Using java.util.Scanner
A.M.L. Chamini
Classes
• A class is the abstract definition of the data type. It includes the data elements that are
part of the data type, and the operations which are defined on the data type.
• It is an Entity which could be a thing, person or something that is imaginary.
A.M.L. Chamini
Classes
• An entity can be described by the data ( Properties ) and its behavior ( methods )
A.M.L. Chamini
Classes
• Eg : Person working in a Company
Employee
EmpNo
Name
Address
BasicSalary
OtHrs OtRate
CalculateOTAmount()
CalculateNetSalary()
A.M.L. Chamini PrintPaySlip()
A.M.L. Chamini
A.M.L. Chamini
Class in Java
class Student {
private int studentNo;
private String name;
private int CA_mark;
private int
Final_mark;
public void assignMarks(int pCA,
int pFin) {
}
public int calculateTotal() {
}
public void printDetails() {
}
}
A.M.L. Chamini
Create a Class
A.M.L. Chamini
Exercise - 2
1. Create a class named as Dog. There is no need to add variables
or methods. Just create and empty class.
A.M.L. Chamini
CreatingObjects
A.M.L. Chamini
Create an Object
A.M.L. Chamini
Multiple Objects
public class MyClass {
int x = 5;
A.M.L. Chamini }
Exercise - 3
1. Create following objects using the Dog class. “puppy”,
“myDog” and “Timmy”
A.M.L. Chamini
Using Multiple Classes
You can also create an object of a class and access it in another class. This is
often used for better organization of classes (one class has all the attributes
and methods, while the other class holds the main() method (code to be
executed)).
Remember that the name of the java file should match the class name. In this
example, we have created two files in the same directory/folder:
• MyClass.java
• OtherClass.java
A.M.L. Chamini
MyClass.java OtherClass.java
A.M.L. Chamini
Java Class Attributes
Create a class called "MyClass" with two attributes: x and y:
A.M.L. Chamini
Accessing Attributes
A.M.L. Chamini
Override existing values
A.M.L. Chamini
If you don't want the ability to override existing values, declare the attribute as final:
A.M.L. Chamini
Multiple Objects
A.M.L. Chamini
Java Class Methods
A.M.L. Chamini
Call a method inside the main method
public class MyClass {
static void myMethod() {
System.out.println("Hello World!");
}
A.M.L. Chamini
public class MyClass {
// Static method
static void myStaticMethod() {
System.out.println("Static methods can be called without creating
objects");
}
// Public method
public void myPublicMethod() {
System.out.println("Public methods must be called by creating objects");
}
// Main method
public static void main(String[] args) {
myStaticMethod(); // Call the static method
// myPublicMethod(); This would compile an error
A.M.L. Chamini
Exercise - 5
1. For the dog class add two methods and call these methods in the main class
using an object.
A.M.L. Chamini
Constructor
// default Constructor public static void main(String args[]) {
public Rectangle () { // default constructor called Rectangle r1
= new Rectangle();
width = 0; // Second constructor called Rectangle r2 =
length = 0; new Rectangle(10,20);
} }
A.M.L. Chamini
// Create a MyClass class
public class MyClass {
int x; // Create a class attribute
// Outputs 5
A.M.L. Chamini
Note that the constructor name must match the class name,
and it cannot have a return type (like void).
Also note that the constructor is called when the object is
created.
All classes have constructors by default: if you do not create a
class constructor yourself, Java creates one for you. However,
then you are not able to set initial values for object attributes.
A.M.L. Chamini
Constructor Parameters
// Outputs 5
A.M.L. Chamini
public class Car {
int modelYear;
String modelName;
A.M.L. Chamini
Exercise - 6
1. Create a constructor for the Dog class and create an object of dog class and
call one of methods of the Dog class.
A.M.L. Chamini
public static void main(String[] args) {
A.M.L. Chamini
public class Dog {
String name;
int age;
public Dog(){
name = name1;
age = age1;
}
public void Bark(){
System.out.println("Baw");
}
//Method 1
public String getInfo(){
return(“Breed is : ”+ breed+”Size is :
”+size+”Age is : ”+age+”Color is : ”+color);
}
}
public class Execute{
public static void main (String[] args){
Dog bolognese = new Dog();
bolognese type = “Bolognese”;
bolognese size = “Small”;
bolognese age = 1;
bolognese color = “White”;
system.out.println(bolognese.getInfo());
}
}
A.M.L. Chamini
default
A.M.L. Chamini
For attributes, methods and constructors, you can use the one of the following:
public
final
abstract
A.M.L. Chamini
final class Vehicle {
class MyClass {
protected String brand = "Ford";
public static void main(String[] args) {
public void honk() {
// create an object of the Student class (which
System.out.println("Tuut, tuut!");
inherits attributes and methods from Person)
}
Student myObj = new Student();
}
System.out.println("Name: " + myObj.fname + "
class Car extends Vehicle {
" + myObj.lname);
private String modelName = "Mustang";
System.out.println("Email: " + myObj.email);
public static void main(String[] args) {
System.out.println("Age: " + myObj.age);
Car myFastCar = new Car();
System.out.println("Graduation Year: " +
myFastCar.honk();
myObj.graduationYear);
System.out.println(myFastCar.brand + " " +
myObj.study(); // call abstract method
myFastCar.modelName);
}
}
}
}
A.M.L. Chamini
For attributes and methods, you can use the one of the following:
Modifier Description
final Attributes and methods cannot be overridden/modified
static Attributes and methods belongs to the class, rather than an
object
abstract Can only be used in an abstract class, and can only be used on
methods. The method does not have a body, for
example abstract void run();. The body is provided by the
subclass (inherited from).
A.M.L. Chamini
Final
public class MyClass {
final int x = 10;
final double PI = 3.14;
A.M.L. Chamini
Static
public class MyClass {
// Static method
static void myStaticMethod() {
System.out.println("Static methods can be called without creating objects");
}
// Public method
public void myPublicMethod() {
System.out.println("Public methods must be called by creating objects");
}
// Main method
public static void main(String[ ] args) {
myStaticMethod(); // Call the static method
// myPublicMethod(); This would output an error
}
Private & Public
• The private part of the definition specifies the data members of a class
• These are hidden from outside the class and can only be accessed though
the operations defined for the class
• The public part of the definition specifies the operations as function
prototypes
• These operations, or methods as they are called, can be accesses by the
main program.
A.M.L. Chamini
Private & Public
A.M.L. Chamini
Sample Client Program
• Write a program which will determine the square yardage to be mowed for a
rectangular yard, given the dimensions of the yard and the dimensions of the house
on that yard. It will also determine the cost of mowing the lawn which is so much
per square yard.
Yard
House
A.M.L. Chamini
Rectangle Problem
Rectangle.cpp Rectangle.java
A.M.L. Chamini
Classes and Objects
Rectangle rectangle1, rectangle2;
• This is somewhat similar to us defining simple variables. However rectangle1 and
rectangle2 are dynamic objects. Memory is allocated when you use the new command.
A.M.L. Chamini
Rectangle Problem
Rectangle.java
Rectangle.cpp
A.M.L. Chamini
Q & A
Ms. A.M.L. Chamini 102
Review Yourself
1. Briefly describes the errors can be seen in a program.
2. Identify the steps in the programming process.
3. Describe a real life embedded system.
References
Introduction to C Programming