Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.

Slides:



Advertisements
Similar presentations
Final and Abstract Classes
Advertisements

Python Objects and Classes
Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
Road Map Introduction to object oriented programming. Classes
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,
Java Methods By J. W. Rider. Java Methods Modularity Declaring methods –Header, signature, prototype Static Void Local variables –this Return Reentrancy.
Object Based Programming. Summary Slide  Instantiating An Object  Encapsulation  Inheritance  Polymorphism –Overriding Methods –Overloading vs. Overriding.
Programming Languages and Paradigms Object-Oriented Programming.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
C++ Object Oriented 1. Class and Object The main purpose of C++ programming is to add object orientation to the C programming language and classes are.
CSM-Java Programming-I Spring,2005 Objects and Classes Overview Lesson - 1.
Recap (önemli noktaları yinelemek) from last week Paradigm Kay’s Description Intro to Objects Messages / Interconnections Information Hiding Classes Inheritance.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
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.
Specialization and Inheritance Chapter 8. 8 Specialization Specialized classes inherit the properties and methods of the parent or base class. A dog is.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
JAVA BASICS Prepared by The Smartpath Information Systems
Inheritance in the Java programming language J. W. Rider.
C++ Programming Basic Learning Prepared By The Smartpath Information systems
Programming in Java CSCI-2220 Object Oriented Programming.
C# Classes and Inheritance CNS 3260 C#.NET Software Development.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Summing Up Object Oriented Design. Four Major Components: Abstraction modeling real-life entities by essential information only Encapsulation clustering.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 26 - Java Object-Based Programming Outline 26.1Introduction.
Lecture 10 Concepts of Programming Languages Arne Kutzner Hanyang University / Seoul Korea.
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
CS451 - Lecture 2 1 CS451 Lecture 2: Introduction to Object Orientation Yugi Lee STB #555 (816) * Acknowledgement:
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.
 Objects versus Class  Three main concepts of OOP ◦ Encapsulation ◦ Inheritance ◦ Polymorphism  Method ◦ Parameterized ◦ Value-Returning.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
Chapter 10: Introduction to Inheritance. Objectives Learn about the concept of inheritance Extend classes Override superclass methods Call constructors.
ISBN Object-Oriented Programming Chapter Chapter
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Introduction to Object-Oriented Programming Lesson 2.
JAVA Programming (Session 4) “When you are willing to make sacrifices for a great cause, you will never be alone.” Instructor: รัฐภูมิ เถื่อนถนอม
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
Classes, Interfaces and Packages
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Object Oriented Programming Session # 03.  Abstraction: Process of forming of general and relevant information from a complex scenarios.  Encapsulation:
Java Programming, Second Edition Chapter Three Using Methods, Classes, and Objects.
1 C# - Inheritance and Polymorphism. 2 1.Inheritance 2.Implementing Inheritance in C# 3.Constructor calls in Inheritance 4.Protected Access Modifier 5.The.
YG - CS Concept of Encapsulation What is encapsulation? - data and functions/methods are packaged together in the class normally.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
5.1 Basics of defining and using classes A review of class and object definitions A class is a template or blueprint for an object A class defines.
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
COMPUTER SCIENCE & TECHNOLOGY DEGREE PROGRAMME FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UVA WELLASSA ‏ Properties of Object Oriented Programming.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Chapter 5 Introduction to Defining Classes Fundamentals of Java.
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 and Polymorphism
Java Primer 1: Types, Classes and Operators
Chapter 3: Using Methods, Classes, and Objects
Road Map Introduction to object oriented programming. Classes
Nested class.
Chapter 3 Introduction to Classes, Objects Methods and Strings
Java Programming Language
Object Oriented Programming in java
Final and Abstract Classes
Lecture 10 Concepts of Programming Languages
C++ Object Oriented 1.
Presentation transcript:

Introduction to Object Oriented Programming

Object Oriented Programming Technique used to develop programs revolving around the real world entities In OOPs, every real life object has properties and behavior.

Features of OOP Language Abstraction Inheritance Encapsulation Polymorphism

Object The basic entity of object oriented programming language. Class itself does nothing but the real functionality is achieved through their objects. Object is an instance of the class. It takes the properties (variables) and uses the behavior (methods) defined in the class. The concept that all objects, while being unique, are also part of a set of objects that have characteristics and behaviors in common

State of an object The set of values of the attributes of a particular object is called its state

Class Defines the properties and behavior (variables and methods) that is shared by all its objects. A class is a piece of computer program that serves as a template for the creation of an object

Name Age Color Weight Name-Lassie Age-4y Color-Brown/Black Weight-60Kg Name-Tommy Age-6y Color-Black Weight-65Kg Name-sprinter Age-4y Color-Brown/Black Weight-68Kg Name-Lany Age-6y Color-Brown/Black Weight-62Kg Dog Class Objects

Instance The actual object created at run-time The Lassie object is an instance of the Dog class

Abstraction The process of abstraction in Java is used to hide certain details and only show the essential features of the object.

Encapsulation Process of binding together the methods and data variables as a single entity. It keeps both the data and functionality code safe from the outside world. It hides the data within the class and makes it available only through the methods. Java provides different accessibility scopes (public, protected, private,default) to hide the data from outside.

Inheritance Allows a class (subclass) to acquire the properties and behavior of another class (superclass). In java, a class can inherit only one class (superclass) at a time but a class can have any number of subclasses. It helps to reuse,

Polymorphism Allows one interface to be used for a set of actions i.e. one name may refer to different functionality. Polymorphism allows a object to accept different requests of a client and responds according to the current state of the runtime system

Two types of polymorphism Compile-time polymorphism Runtime Polymorphism

Compile-time polymorphism Method to be invoked is determined at the compile time. Compile time polymorphism is supported through the method overloading concept Method overloading means having multiple methods with same name but with different signature (number, type and order of parameters).

Runtime Polymorphism Method to be invoked is determined at the run time. The example of run time polymorphism is method overriding. When a subclass contains a method with the same name and signature as in the super class then it is called as method overriding.

Dynamic binding

Declaring Classes class MyClass { //field, constructor, and method declarations } The class body is the area between the braces. It contains constructors for initializing new objects, declarations for the fields that provide the state of the class and its objects, and methods to implement the behavior of the class and its objects.

Constructors A class contains constructors that are invoked to create objects from the class blueprint When you create a new instance (a new object) of a class using the new keyword, a constructor for that class is called Constructors are used to initialize the instance variables (fields) of an object.

Constructors Constructor name is class name. A constructors must have the same name as the class its in. Default constructor is created automatically by the compiler only if there are no constructors. If you define any constructor for your class, no default constructor is automatically created. The default constructor initializes all instance variables to default value (zero for numeric types, null for object references, and false for booleans).

Differences between methods and constructors There is no return type given in a constructor signature (header). The value is this object itself There is no return statement in the body of the constructor The first line of a constructor must either be a call on another constructor in the same class (using this), or a call on the superclass constructor (using super). If the first line is neither of these, the compiler automatically inserts a call to the parameterless super class constructor.

this(...) Calls another constructor in same class.

super(...) Use super to call a constructor in a parent class. Calling the constructor for the super class must be the first statement in the body of a constructor.

Passing Information to a Method or a Constructor Parameters refers to the list of variables in a method declaration. Arguments are the actual values that are passed in when the method is invoked. When you invoke a method, the arguments used must match the declaration's parameters in type and order.

Declaring Member Variables There are several kinds of variables * Member variables in a class—these are called fields. * Variables in a method or block of code— these are called local variables. * Variables in method declarations—these are called parameters.

Field declarations are composed of three components 1. Zero or more modifiers, such as public or private. 2. The field's type. 3. The field's name.

Access Modifiers control what other classes have access to a member field * public modifier—the field is accessible from all classes. * private modifier—the field is accessible only within its own class.

Types Variables must have a type. Use primitive types such as int, float, boolean, etc. Or you can use reference types, such as strings, arrays, or objects.

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.

Transient Variables member variables are part of the persistent state of the object Member variables that are part of the persistent state of an object must be saved when the object is archived transient keyword to indicate to the Java virtual machine that the indicated variable is not part of the persistent state of the object Transient int totalAvg;

Volatile Variables If a member variable is modified asynchronously by concurrently running threads the volatile variable is loaded from memory before each use, and stored to memory after each use thereby ensuring that the value of the variable is consistent and coherent within each thread

Defining Methods The required elements of a method declaration are the Modifiers—such as public, private method's return type Name a pair of parentheses () and parameter list in parenthesis and a body between braces {}

Overloading Methods distinguish between methods with different method signatures means that methods within a class can have the same name if they have different parameter lists

Method Overriding achieved when a subclass overrides non-static methods defined in the superclass. The new method definition must have the same method signature (i.e., method name and parameters) and return type. Only parameter types and return type are chosen as criteria for matching method signature

Creating Objects A class provides the blueprint for objects Create an object from a class Creating Objects includes three parts 1. Declaration: 2. Instantiation: The new keyword is a Java operator that creates the object. 3. Initialization: The new operator is followed by a call to a constructor, which initializes the new object.

Declaring a Variable to Refer to an Object To declare a variable.This declaration also reserves the proper amount of memory for the variable type name; declaring a variable to hold an object is just like declaring a variable to hold a value of primitive type its value will be undetermined until an object is actually created and assigned to it. Simply declaring a reference variable does not create an object. For that, you need to use the new operator,

Instantiating an Object used new operator to create a new instance of an object. The new operator instantiates a class by allocating memory for a new object of that type. new requires a single argument: a call to a constructor method. Constructor methods are special methods provided by each Java class that are responsible for initializing new objects of that type.

Initializing an Object classes provide constructor methods to initialize a new object of that type. A class may provide multiple constructors to perform different kinds of initialization on new objects. When looking at the implementation for a class, you can recognize the constructors because they have the same name as the class and have no return type.

Referencing an Object's Variables To access an object's variables, simply append the variable name to an object reference with an intervening '.' (period). dogObj1.name=“Lassie”; dogObj1.age=1;

Calling an Object's Methods Calling an object's method is similar to getting an object's variable. To call an object's method, simply append the method name to an object reference with an intervening '.' (period), provide any arguments to the method within enclosing parentheses. If the method does not require any arguments, just use empty parentheses. dogObj1.setName(“Lassie”);

The Garbage Collector The Java runtime environment has a garbage collector that periodically frees the memory used by objects that are no longer needed. The Java garbage collector is a mark-sweep garbage collector that scans Java's dynamic memory areas for objects, marking those that are referenced. After all possible paths to objects are investigated, those objects that are not marked (that is, not referenced) are known to be garbage and are collected.

Finalization Before an object gets garbage collected, the garbage collector gives the object an opportunity to clean up after itself through a call to the object's finalize method. This process is known as finalization. protected void finalize() throws Throwable

Public, Abstract, and Final Classes The public modifier declares that the class can be used by objects outside the current package. By default a class can only be used by other classes in the same package in which it is declared. The abstract modifier declares that your class is an abstract class. An abstract class may contain abstract methods (methods with no implementation). Abstract classes are intended to be subclassed and cannot be instantiated. the final modifier you declare that your class is final; that is, that your class cannot be subclassed.

Abstract Classes a class that you define an abstract concept and should not be instantiated. for example, Mammal class in the real world. Have you ever seen an instance of Mammal ? What you see instead are instances of Dogs,Cats,Lions,Bats abstract class Number { }

Abstract Methods methods with no implementation. abstract void getName();

Final Classes declare a class as final; that is, that class cannot be subclassed. final class Dog { }

Final Methods protect class's methods from being overridden, can use the final keyword in a method declaration to indicate to the compiler that the method cannot be overridden by subclasses

Declaring Constants To create a constant member variable in Java use the keyword final in your variable declaration. final double AVOGADRO = 6.023e23;

Inheritance

Classes can be derived from other classes, thereby inheriting fields and methods from those classes. A class that is derived from another class is called a sub class (also a derived class, extended class, or child class). The class from which the subclass is derived is called a super class (also a base class or a parent class). public class Dog extends Mammal { }

Inheritance Can reuse the fields and methods of the existing class without having to write them A subclass inherits the members (fields, methods, and nested classes) from its superclass. Constructors are not members, so they are not inherited by subclasses, but the constructor of the superclass can be invoked from the subclass.

* The inherited fields can be used directly, just like any other fields. * You can declare a field in the subclass with the same name as the one in the superclass, thus hiding it (not recommended). * You can declare new fields in the subclass that are not in the superclass. * The inherited methods can be used directly as they are. * You can write a new instance method in the subclass that has the same signature as the one in the superclass, thus overriding it. * You can write a new static method in the subclass that has the same signature as the one in the superclass, thus hiding it. * You can declare new methods in the subclass that are not in the superclass. * You can write a subclass constructor that invokes the constructor of the superclass, either implicitly or by using the keyword super.

Private Members in a Superclass A subclass does not inherit the private members of its parent class. However, if the superclass has public or protected methods for accessing its private fields, these can also be used by the subclass.

A subclass inherits all of the public and protected members of its parent, no matter what package the subclass is in. If the subclass is in the same package as its parent, it also inherits the package-private members of the parent.

Creating and Using Interfaces An interface is a collection of method definitions (without implementations) and constant values.