Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In."— Presentation transcript:

1 Introduction to Object Oriented Programming

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

3 Features of OOP Language Abstraction Inheritance Encapsulation Polymorphism

4 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

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

6 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

7 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

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

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

10 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.

11 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,

12 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

13 Two types of polymorphism Compile-time polymorphism Runtime Polymorphism

14 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).

15 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.

16 Dynamic binding

17 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.

18 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.

19 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).

20 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.

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

22 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.

23 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.

24 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.

25 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.

26 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.

27 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.

28 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.

29 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;

30 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

31 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 {}

32 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

33 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

34 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.

35 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,

36 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.

37 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.

38 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;

39 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”);

40 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.

41 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

42 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.

43 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 { }

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

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

46 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

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

48 Inheritance

49 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 { }

50 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.

51 * 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.

52 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.

53 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.

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


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

Similar presentations


Ads by Google