Introduction to Object Oriented Programming Object Oriented Programming

  • Slides: 54
Download presentation
Introduction to Object Oriented Programming

Introduction to Object Oriented Programming

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

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

Features of OOP Language Abstraction Inheritance Encapsulation Polymorphism

Object The basic entity of object oriented programming language. Class itself does nothing but

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

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

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

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

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

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

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

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.

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

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.

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

Two types of polymorphism Compile-time polymorphism Runtime Polymorphism

Compile-time polymorphism Method to be invoked is determined at the compile time. Compile time

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

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

Dynamic binding

Declaring Classes class My. Class { //field, constructor, and method declarations

Declaring Classes class My. Class { //field, constructor, and method declarations

Constructors A class contains constructors that are invoked to create objects from the class

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

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

Differences between methods and constructors There is no return type given in a constructor

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

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

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

super(. . . ) Use super to call a constructor in a parent 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

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

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

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

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,

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 defined inside methods, constructors or blocks are called local variables.

Variable Types Local 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 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,

Transient Variables member variables are part of the persistent state of the object Member

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 total. Avg;

Volatile Variables If a member variable is modified asynchronously by concurrently running threads the

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,

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

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

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

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

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

Instantiating an Object used new operator to create a new instance of an object.

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

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

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

Calling an Object's Methods Calling an object's method is similar to getting an object's

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. dog. Obj 1. set. Name(“Lassie”);

The Garbage Collector The Java runtime environment has a garbage collector that periodically frees

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

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

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.

Abstract Classes a class that you define an abstract concept and should not be

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 get. Name();

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

 Final Classes declare a class as final; that is, that class cannot be

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

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

Declaring Constants To create a constant member variable in Java use the keyword final in your variable declaration. final double AVOGADRO = 6. 023 e 23;

Inheritance

Inheritance

Inheritance Classes can be derived from other classes, thereby inheriting fields and methods from

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,

Inheritance Can reuse the fields and methods of the existing class without having to

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.

* 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

Private Members in a Superclass A subclass does not inherit the private members of

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

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)

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