0% found this document useful (0 votes)
130 views16 pages

Java2 - Koya

The document discusses object-oriented programming in Java. It begins by explaining the history and evolution of programming from assembly languages to structured programming and then to object-oriented programming. It describes some key concepts of OOP like encapsulation, inheritance and polymorphism. It then discusses classes and objects in Java, explaining how classes act as blueprints for objects and define their data and behaviors. The document also covers constructors and how they initialize new objects.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
130 views16 pages

Java2 - Koya

The document discusses object-oriented programming in Java. It begins by explaining the history and evolution of programming from assembly languages to structured programming and then to object-oriented programming. It describes some key concepts of OOP like encapsulation, inheritance and polymorphism. It then discusses classes and objects in Java, explaining how classes act as blueprints for objects and define their data and behaviors. The document also covers constructors and how they initialize new objects.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 16

Object Oriented Programming

in

Java
Second Year University of koya Faculty of Engineering School of Computer Department of Software Engineering
2011-2012

Object-Oriented Programming
Object-oriented programming is a new way of programming. Since its early days, programming has been practiced using a number of various methodologies. At each new stage, a new approach was created to make programming easier and help the programmer handle more complex programs. At first, programmers had to write programs using laborious binary instructions and data with switches. Later, assembly languages were invented which allowed longer programs to be written. In the 1950s the first high-level language (Fortran) was invented. Using a high-level language like Fortran, a programmer could write a program with several thousand lines of code. But that method only allowed for unstructured programs: programs without any structure and very ad hoc.
2

Object Oriented Programming in Java

Object-Oriented Programming 2
Later in 1960s, the need for structured programs became clear and languages like Algol, Pascal and C were introduced. C++ was invented in early 1980s and is also a structured language; it also supports objectoriented programming. Java is a pure object-oriented programming language. Structured programming relies on control structures, code blocks, procedures or functions and facilitates recursion. The main characteristic of structured programming is breaking programs into smaller parts. This in turn will help to write better, more structured and larger programs. Using structured programming an average programmer can write and maintain programs that are up 40,000-50,000 lines of code long.
Object Oriented Programming in Java 3

Object-Oriented Programming 3
With structured programming you can write quite complex programs. But after a certain point even structured programming or becomes very hard to follow. To write larger and more complex programs, a new programming approach was invented: object-oriented programming or OO for short. Object-oriented programming combines the best features of structured programming with some new powerful concepts that allows writing more complex and more organized programs. The main new concepts in OO are encapsulation, polymorphism and inheritance. Any programming language that supports these three concepts is said to be an OO programming language. Examples of OO programming languages are C++, Java, Smalltalk. Unlike C++, Java is a pure OO programming language.
Object Oriented Programming in Java 4

Object-Oriented Programming 4
Object-oriented programming encourages programmers to break problems into related subgroups. Each subgroup becomes a self-contained object with its own instructions and data. So OO programs consist of objects. An object is similar to an ordinary variable but with its own member functions. Writing large programs is made a lot easier using objects. Each object is a self-contained entity. It is an autonomous entity that can be used and reused in other programs. This also allows for composition of objects to create more complex programs. Its like the automobile manufacturing business where factories compose new cars out of pre-built parts (objects). These parts or objects may be manufactured by different companies. When a component or part becomes out-of-order or out-of-date, that component is replaced by a new one. Maintenance becomes much easier.
5

Object Oriented Programming in Java

Object-Oriented Programming 5
In procedural programming languages such as C and Pascal, programming tends to be action-oriented whereas in OO programming languages such as Java and Smalltalk, programming object-oriented. In action-oriented programming, the focus is on actions or functions; in OO programming, the focus is on objects. Suppose ob is an object: print(ob); //focus is function/action ob.print(); //focus is object Most things in the world are classified: a class of students, a class of fish, a class of birds, a class of objects. The word class means a type or a group of things which have some common attributes or properties. In most object oriented programming languages, the construct class is used to create a new type or class of objects. Definition: A class is a collection of data, stored in named fields, and code, organized into named methods, that operates on that data.
Object Oriented Programming in Java 6

Objects & Classes


A class is like a factory for creating objects. We have already used a number of pre-defined classes? You can create your own classes. The fields and methods of a class are called members of the class. The members of a class can be of two types: static or class members associated with the class itself and instance members associated with individual instances of the class (i.e., with objects): - class fields - instance fields - class methods - instance methods

An example: public class Circle { //a class/static field public static final double PI=3.14159; //a class/static method public static double radiansToDegrees(double rads){
Object Oriented Programming in Java 7

Objects & Classes 2


return rads * 180 / PI; } //an instance field public double r; //two instance methods public double area() { return PI * r * r; } public double circumference(){ return 2 * PI * r; }} The static modifier says that the field is a class field. The final modifier says that this field does not change. The public modifier says that this field can be used by any code; its a visibility modifier which we will cover in more detail later.
Object Oriented Programming in Java 8

Object & classes

A class can contain any of the following variable types.


Local variables . variables defined inside methods, constructors or blocks are called local variables. The variable will be declared and initialized within the method and the variable will be destroyed when the method has completed. Instance variables . Instance variables are variables within a class but outside any method. These variables are instantiated when the class is loaded. Instance variables can be accessed from inside any method, constructor or blocks of that particular class. Class variables . Class variables are variables declared with in a class, outside any method, with the static keyword.

Objects & Classes 3


Its important to know that a class filed is associated with the class itself and not with any instances/objects of that class and that there is only a single copy of it. This field can be accessed inside the class by writing its name only PI, and to access it from outside the class you must write Circle.PI. A class filed is similar to a global variable; it is accessible from all parts and methods of that class. Class methods are declared with the static modifier. Like class fields, they can be called from within the class by just writing its name, but its good programming style to specify the class name in which it is a member of, to make it clear that a class method is being invoked. Class methods can use class fields or other class methods but they cannot use any instance fields or methods. Why?
Object Oriented Programming in Java 10

Objects & Classes 4


Any field declared without the static modifier is an instance field: public double r; Instance fields are associated with instances or objects of the class; each instance of the class has its own copy of an instance field. Any method not declared with the static modifier is an instance method. Instance methods operate on instances of the class, not on the class itself. Instance methods have access to both class fields and class methods. Instance methods operate on class instances or objects; they know which object they operate on because they are passed an implicit reference to the object they operate on; this reference is called this. The instance method area could have been written as: public double area(){ return Circle.PI * this.r * this.r; }
Object Oriented Programming in Java 11

Constructors
A constructor is a special method that is automatically called every time you create a new object. Constructors are used to initialize objects. Since constructors are called automatically, rather than manually, the programmer is relieved from having to initialize objects. A constructor method has the same name as the class name and returns no values.
Constructors:

To create and initialize an object: Circle c=new Circle(); The operator new creates a new object of type Circle. In the class Circle defined earlier, no constructors were written; Java provided a default constructor that takes no parameters and performs no initialization. It is always better to specify a constructor for every new class you define to specify how a new object of that class would be initialized: public Circle(double r) {this.r=r;}//why this.r?

Object Oriented Programming in Java

12

Constructors 2
To create an instance/object of type Circle: Circle c=new Circle(5.0); //object creation & //initialization on one line When writing a constructor for a class: - the constructor name is always the same the class name - the constructor has no return values but may take parameters - the constructor should perform initialization of the new object To provide flexibility in initializing a new object, often multiple constructors are defined: public Circle() { r=1.0;} public Circle(double r) { this.r=r;} The two constructors must have different parameter lists or signatures (method overloading), otherwise ?
Object Oriented Programming in Java 13

Constructors 3
One important thing about having multiple constructors is that always create a default constructor. If you need a constructor, make sure you have defined a default constructor. If you dont define any constructors for your class, then Java provides a default one that does nothing. One of the uses of the this keyword is to invoke a constructor from within another constructor: public Circle(double r) { this.r=r;} public Circle() { this(1.0);} Instance fields and class fields are initialized by default: primitive numeric types to 0, booleans to false, characters to and reference types to null. But local variables (declared inside methods) are not initialized by default. You should initialize them before use.

Object Oriented Programming in Java

14

This keyword :
The keyword this is useful when you need to refer to instance of the class from its method. The keyword helps us to avoid name conflicts. As we can see in the program that we have declare the name of instance variable and local variables same. Now to avoid the confliction between them we use this keyword. Here, this section provides you an example with the complete code of the program for the illustration of how to what is this keyword and how to use it. In the example, this.length and this.breadth refers to the instance variable length and breadth while length and breadth refers to the arguments passed in the method. We have made a program over this. After going through it you can better understand.

public class UseOfThisOperator{

You might also like