Java2 - Koya
Java2 - Koya
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 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 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
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
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?
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.
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.