OOP Features Object Oriented Programming Main issues in software engineering – –maintainability, reusability, portability, security, integrity, user.

Slides:



Advertisements
Similar presentations
1 CLASSES. 2 Class Fundamentals It defines a new data type Once defined, this new type can be used to create objects of that type A class is a template.
Advertisements

Road Map Introduction to object oriented programming. Classes
Lecture 2 Classes and objects, Constructors, Arrays and vectors.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
04/07/041 Intro to JAVA By: Riyaz Malbari. 04/07/042 History of JAVA  Came into existence at Sun Microsystems, Inc. in  Was initially called “
Data Abstraction and Object- Oriented Programming CS351 – Programming Paradigms.
CS 2511 Fall  Abstraction Abstract class Interfaces  Encapsulation Access Specifiers Data Hiding  Inheritance  Polymorphism.
C++ fundamentals.
1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,
Programming Languages and Paradigms Object-Oriented Programming.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Inheritance. Introduction Inheritance is one of the cornerstones of object-oriented programming because it allows the creation of hierarchical classifications.
A Closer Look at Methods and Classes. Overloading Methods In Java it is possible to define two or more methods within the same class that share the same.
Overloading There is another aspect to polymorphism: Overloading Overloading is not overriding. In Turkish: Overridding: eskisini (geçersiz kılmak) Overloading:
C++ Programming Basic Learning Prepared By The Smartpath Information systems
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
1 Programming Paradigms Object Orientated Programming Paradigm (OOP)
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Introduction to c++ programming - object oriented programming concepts - Structured Vs OOP. Classes and objects - class definition - Objects - class scope.
Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 4.
What is an Object? Real world objects are things that have: 1) state 2) behavior Example: your dog: 1) state – name, color, breed, sits?, barks?, wages.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
 Objects versus Class  Three main concepts of OOP ◦ Encapsulation ◦ Inheritance ◦ Polymorphism  Method ◦ Parameterized ◦ Value-Returning.
Object Oriented Programming
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 9 Java Fundamentals Objects/ClassesMethods Mon.
Introduction to Object-Oriented Programming Lesson 2.
Classes, Interfaces and Packages
Overloading Methods In Java it is possible to define two or more methods within the same class that share the same name, as long as their parameter declarations.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
1 CLASSES. 2 Class Fundamentals It defines a new data type Once defined, this new type can be used to create objects of that type A class is a template.
In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The import statement and using prewritten.
Topics Instance variables, set and get methods Encapsulation
Internet Computing Module II. Syllabus Creating & Using classes in Java – Methods and Classes – Inheritance – Super Class – Method Overriding – Packages.
Methods and Classes. Method Overloading Two or more methods within the same class that share the same name but different parameters class OverloadDemo.
COMPUTER SCIENCE & TECHNOLOGY DEGREE PROGRAMME FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UVA WELLASSA ‏ Properties of Object Oriented Programming.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
Methods, classes, and Objects Dr. Jim Burns. Question  Which of the following access modifiers is the default modifier?  public  private  protected.
Inheritance. Inheritance is a mechanism in which one object acquires all the properties and behaviors of parent object Inheritance represents the IS-A.
Object Oriented Programming. Constructors  Constructors are like special methods that are called implicitly as soon as an object is instantiated (i.e.
Object-oriented programming (OOP) is a programming paradigm using "objects" – data structures consisting of data fields and methods together with their.
Modern Programming Tools And Techniques-I
Topic: Classes and Objects
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
Static data members Constructors and Destructors
Inheritance-Basics.
Final and Abstract Classes
Java Primer 1: Types, Classes and Operators
Chapter 3: Using Methods, Classes, and Objects
Modern Programming Tools And Techniques-I Inheritance
Subroutines Idea: useful code can be saved and re-used, with different data values Example: Our function to find the largest element of an array might.
Constructor Overloading
Introduction to Classes
Week 6 Object-Oriented Programming (2): Polymorphism
OBJECT ORIENTED PROGRAMMING
Week 3 Object-based Programming: Classes and Objects
Object Oriented Programming in java
Java – Inheritance.
JAVA 22 February 2019 DEPARTMENT OF CSE.
UNIT I OBJECT ORIENTED PROGRAMMING FUNDAMENTALS
Index Understanding static Final Inheritance Super
Object Oriented Programming in java
Java Programming Language
Lecture 8 Object Oriented Programming (OOP)
Chapter 7 Objects and Classes
class Box { double width; double height; double depth; }
Presentation transcript:

OOP Features

Object Oriented Programming Main issues in software engineering – –maintainability, reusability, portability, security, integrity, user friendliness etc. Software become complex. –Programs structure should be easy to implement and modify in a wide variety of situations. Many programming approaches are tried –Modular programming –Top-down programming –Bottom-up programming –Structure programming

Programming Approaches Modular programming - –program is broken into several independently compiled modules. –Modular programming is a precursor of object-oriented programming. Top-down programming - –implements a program in top-down fashion. –Typically, this is done by writing a main body with calls to several major routines –Each routine is then coded, calling other, lower-level, routines

Programming Approaches Bottom-up programming – –a technique in which lower-level functions are developed and tested first; –higher-level functions are then built using the lower-level functions, and so on. Structure programming – –statements are organized in a specific manner to minimize error or misinterpretation.

OOP Object oriented programming is an approach that provides – –a way of modularizing programs by creating partitioned memory area for both data and functions –templates for creating copies of such modules on demand. –e.g. Smalltalk, Ada, C++, Objective C, Delphi, Java. Data Method Object = data + method

Different Paradigms in OOP In order to achieve many facilities, OOP adopts the following paradigms: Data abstraction- –facility for the users to define their objects Encapsulation- –definition of an object (physical view) and various functions for communicating with other objects (behavioral view) can be fit into together. –Wrapping data and code in single unit. Data hiding/information hiding- –different data/functions can be protected from unauthorized access, –i.e. data and function can be kept secret

Different Paradigms in OOP Polymorphism- –means ‘many forms’ –expressed by ‘one interface, multiple method’ –allows one interface to be used for a general class of actions, which is determined by nature of situation. e.g method overloading, method overriding Inheritance- –the usual features in real world which will allow a programmer to reuse and share data as well as code, in hierarchical manner.

Object-Oriented Programming Concepts and Java  Concepts related to object-oriented programming (JAVA) include – –Objects –Encapsulation and message passing –Classes –Libraries (packages in Java) –Inheritance –Access modifiers

Data Abstraction In Java there are three types of data values: –primitive data values (int, double, boolean, etc.) –arrays (actually a special type of object) –objects Objects in a program are used to represent "real" (and sometimes not so real) objects from the world around us.

Constructed Types: Classes Class is a data type/template/design of an object. –Declaration of a Java class shares many common aspects with classes in C++ Java only supports single inheritance –Effect of multiple inheritance may be achieved through the use of the interface construct. A class may be declared to be abstract - No method implementations provided - All derived classes must provide implementations for all methods defined in the abstract class

Constructed Types: Classes Java classes may declare both variables and methods –Variable defined inside of a class is an instance variable - one copy per object –Class methods invoked by applying them to an instance of a class object e.g. ob1.add(x, y)

Objects Object is an instance of a class. An object might represent –a string of characters, a planet, a type of food, a student, an employee, a piece of ,... anything that can't be (easily) represented by a primitive value or an array. –Just as ‘3’ is a primitive value of type int, every object must also have a type. –These types are called classes.

Classes A class describes a set of objects. –specifies what information will be used to represent an object from the set e.g. name and salary for an employee –specifies what operations can be performed on such an object e.g getName( )// the name of the student sendMail( )// send an message

Elements of a Simple Class A class describes the data values used to represent an object and any operations that can be performed on that object. –The data values are stored in instance variables, also known as fields, or data members. –The operations are described by instance methods, sometimes called procedure members.

Class Definition The general form of class definition: class ClassName [extends SuperClassName ] [implements Interface ] { [declaration of member elements] [declaration of methods] } nb: Those included with […] are optional.

class An empty class could be: class Empty{ //………… }

class Box class Box { double width; double height; double depth; } Box mybox = new Box(); // create a Box object called mybox

BoxDemo.java class Box { double width; double height; double depth; } // This class declares an object of type Box. class BoxDemo { public static void main(String args[]) { Box mybox; // type declaration mybox = new Box();// instantiation double vol; // assign values to mybox's instance variables mybox.width = 10; mybox.height = 20; mybox.depth = 15; // compute volume of box vol = mybox.width * mybox.height * mybox.depth; System.out.println("Volume is " + vol); }

BoxDemo2.java class Box { double width; double height; double depth; } class BoxDemo2 { public static void main(String args[]) { Box mybox1 = new Box(); Box mybox2 = new Box(); double vol; // assign values to mybox1's instance variables mybox1.width = 10; mybox1.height = 20; mybox1.depth = 15; /* assign different values to mybox2's instance variables */ mybox2.width = 3; mybox2.height = 6; mybox2.depth = 9; // compute volume of first box vol = mybox1.width * mybox1.height * mybox1.depth; System.out.println("Volume is " + vol); // compute volume of second box vol = mybox2.width * mybox2.height * mybox2.depth; System.out.println("Volume is " + vol); }

Declaring Objects Obtaining an Object is a two-step process –First, declare a variable of any class type –Doesn’t define an object, a variable to refer an object Box mybox; –Second, acquire a physical copy of that object and assign it to that variable, using new operator mybox = new Box ( ); null Width Height Depth mybox

Assigning Object Reference Box b1 = new Box( ); Box b2 = b1; Now, if – b1 = null; Here, b1 is set to null, but b2 still points to the original object Width Height Depth b1 b2

Adding method // This program includes a method inside the box class. class Box { double width; double height; double depth; // display volume of a box void volume() { System.out.print("Volume is "); System.out.println(width * height * depth); }

class BoxDemo3 { public static void main(String args[]) { Box mybox1 = new Box(); Box mybox2 = new Box(); // assign values to mybox1's instance variables mybox1.width = 10; mybox1.height = 20; mybox1.depth = 15; /* assign different values to mybox2's instance variables */ mybox2.width = 3; mybox2.height = 6; mybox2.depth = 9; // display volume of first box mybox1.volume(); // display volume of second box mybox2.volume(); }

Returning value by method // Now, volume() returns the volume of a box. class Box { double width; double height; double depth; // compute and return volume double volume() { return width * height * depth; }

class BoxDemo4 { public static void main(String args[]) { Box mybox1 = new Box(); Box mybox2 = new Box(); double vol; // assign values to mybox1's instance variables mybox1.width = 10; mybox1.height = 20; mybox1.depth = 15; /* assign different values to mybox2's instance variables */ mybox2.width = 3; mybox2.height = 6; mybox2.depth = 9; // get volume of first box vol = mybox1.volume(); System.out.println("Volume is " + vol); // get volume of second box vol = mybox2.volume(); System.out.println("Volume is " + vol); }

Adding methods with parameter class Box { double width; double height; double depth; // compute and return volume double volume() { return width * height * depth; } // sets dimensions of box void setDim(double w, double h, double d) { width = w; height = h; depth = d; }

class BoxDemo5 { public static void main(String args[]) { Box mybox1 = new Box(); Box mybox2 = new Box(); double vol; // initialize each box mybox1.setDim(10, 20, 15); mybox2.setDim(3, 6, 9); // get volume of first box vol = mybox1.volume(); System.out.println("Volume is " + vol); // get volume of second box vol = mybox2.volume(); System.out.println("Volume is " + vol); }

Constructor Constructor initializes an object upon creation. It has the same name as the class resides Box mybox = new Box( ); //new Box( ), is calling Box( ) constructor. If you don’t define a constructor, Java creates a default constructor. Default constructor automatically initializes all variables to ‘zero’.

Constructor /* Here, Box uses a constructor to initialize the dimensions of a box. */ class Box { double width; double height; double depth; // This is the constructor for Box. Box() { System.out.println("Constructing Box"); width = 10; height = 10; depth = 10; } // compute and return volume double volume() { return width * height * depth; }

class BoxDemo6 { public static void main(String args[]) { // declare, allocate, and initialize Box objects Box mybox1 = new Box(); Box mybox2 = new Box(); double vol; // get volume of first box vol = mybox1.volume(); System.out.println("Volume is " + vol); // get volume of second box vol = mybox2.volume(); System.out.println("Volume is " + vol); }

Parameterized Constructor /* Here, Box uses a parameterized constructor to initialize the dimensions of a box. */ class Box { double width; double height; double depth; // This is the constructor for Box. Box(double w, double h, double d) { width = w; height = h; depth = d; } // compute and return volume double volume() { return width * height * depth; }

class BoxDemo7 { public static void main(String args[]) { // declare, allocate, and initialize Box objects Box mybox1 = new Box(10, 20, 15); Box mybox2 = new Box(3, 6, 9); double vol; // get volume of first box vol = mybox1.volume(); System.out.println("Volume is " + vol); // get volume of second box vol = mybox2.volume(); System.out.println("Volume is " + vol); }

Use of this Sometimes a method need to refer the object that invoked it. –Allowing this Java provide a way to refer current object from inside the method. –Can be used this to resolve name-space collisions. Between ‘local variables’ & ‘parameters’

Use of this Box(double width, double height, double depth) { this.width = width; this.height = height; this.depth = depth; }

Method Overloading It is possible to define two or more methods within same class – –Share the same name. –Their parameters declaration will be different. –One ways of implementing ‘polymorphism’ –When an overloaded method invoked, Java uses type & number of arguments to determine, which one will be called. –Overloaded methods may have different return types, but this is not enough to distinguish two versions.

// Demonstrate method overloading. class OverloadDemo { void test() { System.out.println("No parameters"); } // Overload test for one integer parameter. void test(int a) { System.out.println("a: " + a); } // Overload test for two integer parameters. void test(int a, int b) { System.out.println("a and b: " + a + " " + b); } // overload test for a double parameter double test(double a) { System.out.println("double a: " + a); return a*a; }

class Overload { public static void main(String args[]) { OverloadDemo ob = new OverloadDemo(); double result; // call all versions of test() ob.test(); ob.test(10); ob.test(10, 20); result = ob.test(123.25); System.out.println("Result of ob.test(123.25): " + result); }

// Automatic type conversions apply to overloading. class OverloadDemo { void test() { System.out.println("No parameters"); } // Overload test for two integer parameters. void test(int a, int b) { System.out.println("a and b: " + a + " " + b); } // overload test for a double parameter and return type void test(double a) { System.out.println("Inside test(double) a: " + a); } Automatic type conversion

class Overload { public static void main(String args[]) { OverloadDemo ob = new OverloadDemo(); int i = 88; ob.test(); ob.test(10, 20); ob.test(i); // this will invoke test(double) ob.test(123.2); // this will invoke test(double) }

Method overloading supports polymorphism – –Java implements ‘one interface, multiple methods’ –In ‘C’ language, don’t support overloading for ‘abs’ function, they need all types of function for each input type, abs(), fabs(), labs() etc. Lots of name is required, even though they perform same operation. –In ‘Java’, its ‘Math’ class handles all types of numeric types using ‘abs()’ method Method Overloading

Constructor Overloading

/* Here, Box defines three constructors to initialize the dimensions of a box various ways. */ class Box { double width; double height; double depth; // constructor used when all dimensions specified Box(double w, double h, double d) { width = w; height = h; depth = d; } // constructor used when no dimensions specified Box() { width = -1; // use -1 to indicate height = -1; // an uninitialized depth = -1; // box } // constructor used when cube is created Box(double len) { width = height = depth = len; } // compute and return volume double volume() { return width * height * depth; }

class OverloadCons { public static void main(String args[]) { // create boxes using the various constructors Box mybox1 = new Box(10, 20, 15); Box mybox2 = new Box(); Box mycube = new Box(7); double vol; // get volume of first box vol = mybox1.volume(); System.out.println("Volume of mybox1 is " + vol); // get volume of second box vol = mybox2.volume(); System.out.println("Volume of mybox2 is " + vol); // get volume of cube vol = mycube.volume(); System.out.println("Volume of mycube is " + vol); }

Using objects as Parameter // Here, Box allows one object to initialize another. class Box { double width; double height; double depth; // construct clone of an object Box(Box ob) { // pass object to constructor width = ob.width; height = ob.height; depth = ob.depth; } // constructor used when all dimensions specified Box(double w, double h, double d) { width = w; height = h; depth = d; }

// constructor used when no dimensions specified Box() { width = -1; // use -1 to indicate height = -1; // an uninitialized depth = -1; // box } // constructor used when cube is created Box(double len) { width = height = depth = len; } // compute and return volume double volume() { return width * height * depth; }

class OverloadCons2 { public static void main(String args[]) { // create boxes using the various constructors Box mybox1 = new Box(10, 20, 15); Box mybox2 = new Box(); Box mycube = new Box(7); Box myclone = new Box(mybox1); double vol; // get volume of first box vol = mybox1.volume(); System.out.println("Volume of mybox1 is " + vol); // get volume of second box vol = mybox2.volume(); System.out.println("Volume of mybox2 is " + vol); // get volume of cube vol = mycube.volume(); System.out.println("Volume of cube is " + vol); // get volume of clone vol = myclone.volume(); System.out.println("Volume of clone is " + vol); }

Call by value // Simple Types are passed by value. class Test { void meth(int i, int j) { i *= 2; j /= 2; } class CallByValue { public static void main(String args[]) { Test ob = new Test(); int a = 15, b = 20; System.out.println("a and b before call: " + a + " " + b); ob.meth(a, b); System.out.println("a and b after call: " + a + " " + b); } a and b before call: a and b after call: 15 20

Call by Reference /* Objects are passed by reference. */ class Test { int a, b; Test(int i, int j) { a = i; b = j; }//const. // pass an object void meth(Test o) { o.a *= 2; o.b /= 2; }//meth }//class class CallByRef { public static void main(String args[]) { Test ob = new Test(15, 20); System.out.println("ob.a and ob.b before call: " + ob.a + " " + ob.b); ob.meth(ob); System.out.println("ob.a and ob.b after call: " +ob.a + " " + ob.b); }//main }//class ob.a and ob.b before call: ob.a and ob.b before call: 30 10

Returning an object. class Test { int a; Test(int i) { a = i; } Test incrByTen() { Test temp = new Test(a+10); return temp; } class RetOb { public static void main(String args[]) { Test ob1 = new Test(2); Test ob2; ob2 = ob1.incrByTen(); System.out.println("ob1.a: " + ob1.a); System.out.println("ob2.a: " + ob2.a); ob2 = ob2.incrByTen(); System.out.println("ob2.a after second increase: “ + ob2.a); } }//class

Introducing access control Encapsulation provides access control. How a member can be accessed, determined by access specifiers –Public – can be accessed by any other code in the program. –Private – can be accessed by other members in the class –Default – when no specifier is declared, by default the member is public within own package. –Protected – used in inheritance [discussed later]

public int i; private double j; private int myMethod(int a, char b) { //... } Access Specifier An access specifier precedes the member’s type specification –

/* This program demonstrates the difference between public and private. */ class Test { int a; // default access public int b; // public access private int c; // private access // methods to access c void setc(int i) { // set c's value c = i; } int getc() { // get c's value return c; } class AccessTest { public static void main(String args[]) { Test ob = new Test(); // These are OK, a and b may be accessed directly ob.a = 10; ob.b = 20; // This is not OK and will cause an error ob.c = 100; // Error! // You must access c through its methods ob.setc(100); // OK System.out.println("a, b, and c: " + ob.a + " " + ob.b + " " + ob.getc()); }

Use of ‘static’ There sometimes needs to define a member to be used independently of any object. –It is possible to create a member that can be used without the reference to object. Precede the declaration with ‘static’ keyword. Both methods and variables can be static

Use of ‘static’ Instance variables declared as static are – –Global variables –When objects of its class are declared, no copy of a static variable is made. –All objects of class share the same static variable. Methods declared as static has restrictions – –They can only call other static methods. –They must access static data. –They can’t refer to ‘this’ or ‘super’ in any way. Static block can be declared which will execute exactly once –When the class will be loaded first.

class UseStatic { static int a = 3; static int b; static void meth(int x) { System.out.println("x = " + x); System.out.println("a = " + a); System.out.println("b = " + b); } static { System.out.println("Static block initialized."); b = a * 4; } public static void main(String args[]) { meth(42); } Static block initialized x = 42 a = 3 b = 12

class StaticDemo { static int a = 42; static int b = 99; static void callme() { System.out.println("a = " + a); } class StaticByName { public static void main(String args[]) { StaticDemo.callme(); System.out.println("b = " + StaticDemo.b); }

Introducing ‘final’ Variable declared as ‘final’ prevents its contents from being modified. –Similar to ‘const’ in C/C++ Its common to choose uppercase identifiers for final variables. final int COUNT = 5;