0% found this document useful (0 votes)
24 views18 pages

Ch07 OOP

The document discusses object-oriented programming (OOP) and classes/objects in Java. It defines OOP as creating objects that contain both data and methods, as opposed to procedural programming which focuses on writing procedures/methods to operate on data. It outlines several advantages of OOP including reusability and maintainability. The document then defines classes as templates for objects, and objects as instances of classes. It explains how to declare classes and create objects in Java, and introduces constructors as special methods used to initialize objects.

Uploaded by

Dalmar Duceesane
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)
24 views18 pages

Ch07 OOP

The document discusses object-oriented programming (OOP) and classes/objects in Java. It defines OOP as creating objects that contain both data and methods, as opposed to procedural programming which focuses on writing procedures/methods to operate on data. It outlines several advantages of OOP including reusability and maintainability. The document then defines classes as templates for objects, and objects as instances of classes. It explains how to declare classes and create objects in Java, and introduces constructors as special methods used to initialize objects.

Uploaded by

Dalmar Duceesane
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/ 18

+

Java Programming
Eng,Awil Ahmed Abdi
+
Objectives
Chapter 7
 Concept of OOP
 Object and Classes in java
 Constructors in java
Java OOP  This keyword
+
Object-Oriented Programming (OOP)

 Procedural programming is about writing procedures or methods that


perform operations on the data, while object-oriented programming is about
creating objects that contain both data and methods.

 Since Java is an object oriented language, complete java language is build


on classes and object. Java is also known as a strong Object oriented
programming language(OOPS).
+
Object-Oriented Programming (OOP)

 OOPS is a programming approach which provides solution to problems


with the help of algorithms based on real world. It uses real world approach
to solve a problem. So object oriented technique offers better and easy way
to write program then procedural programming model such as C, ALGOL,
PASCAL.
+
Object-Oriented Programming (OOP)

Object-oriented programming has several advantages over procedural


programming:
 OOP is faster and easier to execute
 OOP provides a clear structure for the programs
 OOP helps to keep the Java code and makes the code easier to maintain, modify and
debug
 OOP makes it possible to create full reusable applications with less code and shorter
development time
+
Object-Oriented Programming (OOP)

Object-Oriented Programming is a methodology to design a program using classes and


objects. It simplifies software development and maintenance by providing some concepts:

 Object
 Class
 Inheritance
 Polymorphism
 Abstract
 Encapsulation
+
Object-Oriented Programming (OOP)
+
Java Classes/Objects

In Java everything is encapsulated under classes. Class is the core of Java


language. Class can be defined as a template that describe the behaviors /states
of a particular entity. A class defines new data type. Once defined this new
type can be used to create object of that type. Object is an instance of class.
You may also call it as physical existence of a logical template class.
+
Java Classes/Objects

Collection of objects is called class. It is a logical entity.


Classes and objects are the two main aspects of object-oriented programming.

Everything in Java is associated with classes and objects, along with its
attributes and methods. For example: in real life, a car is an object. The car
has attributes, such as weight and color, and methods, such as drive and
brake.
+
Java Classes/Objects

A class is declared using class keyword. A class contain both data and code
that operate on that data. The data or variables defined within a class are
called instance variables and the code that operates on this data is known
as methods.

Thus, the instance variables and methods are known as class members. class is
also known as a user defined datatype.
+
Java Classes/Objects

A class is declared using class keyword. A class contain both data and code that operate on that
data. The data or variables defined within a class are called instance variables and the code
that operates on this data is known as methods.

Thus, the instance variables and methods are known as class members. class is also known as
a user defined datatype.

Example.

public class MyClass {

int x = 5;

}
+
Java Classes/Objects

In Java, an object is created from a class. We have already created the class named MyClass, so
now we can use this to create objects.
To create an object of MyClass, specify the class name, followed by the object name, and use the
keyword new
Example.

public class MyClass {


int x = 5;
public static void main(String[] args) {
MyClass myObj = new MyClass();
System.out.println(myObj.x);
}
}
+
Java Constructors

 A constructor is a special method that is used to initialize an object. Every class has a
constructor, if we don't explicitly declare a constructor for any java class the compiler builds a
default constructor for that class. A constructor does not have any return type.

 A constructor has same name as the class in which it resides. Constructor in Java can not be
abstract, static, final or synchronized. These modifiers are not allowed for constructor.
+
Java Constructors

Example.
public class MyClass {
int x;
MyClass() {
x = 5;
}
public static void main(String[] args) {
MyClass myObj = new MyClass();
System.out.println(myObj.x);
}
}
+
Java Constructors

There are two types of constructors:


 Default Constructor
 Parameterized constructor
Each time a new object is created at least one constructor will be invoked.

Example 1
Car c = new Car();

Car c = new Car(name);


+
Java Constructors

Constructor Parameters

Constructors can also take parameters, which is used to initialize attributes.


The following example adds an int y parameter to the constructor. Inside the constructor we set
x to y (x=y). When we call the constructor, we pass a parameter to the constructor (5), which
will set the value of x to 5:
+
Java Constructors

Example 2

public class MyClass {


int x;
public MyClass(int y) {
x = y;
}
public static void main(String[] args) {
MyClass myObj = new MyClass(5);
System.out.println(myObj.x);
}
}
+
END

You might also like