Object-Oriented Programming Introduction PDF

Document Details

SociableFlugelhorn

Uploaded by SociableFlugelhorn

Mustansiriyah University

Tags

object-oriented programming OOP computer science programming languages

Summary

This document is an introduction to object-oriented programming (OOP). It covers fundamental concepts like classes, objects, methods, and constructors. The document provides explanations and examples to illustrate the concepts.

Full Transcript

Object-Oriented Programming Introduction Object-oriented Vs procedural programming Procedural programming is about writing procedures or methods that perform operations on the data, while object-oriented programming is about creating objects that contain both data and methods. Obj...

Object-Oriented Programming Introduction Object-oriented Vs procedural programming Procedural programming is about writing procedures or methods that perform operations on the data, while object-oriented programming is about creating objects that contain both data and methods. Object-Oriented Languages. The language that has the Object-Oriented feature, usually supports the following fundamental concepts: Polymorphism Inheritance Encapsulation Abstraction Classes Objects Instance Method Message Passing Object: Objects have states and behaviours. Example: A dog has states - colour, name, breed as well as behaviour such as wagging their tail, barking, eating. An object is an instance of a class. Class: A class can be defined as a template/blueprint that describes the behaviour/state that the object of its type supports. Methods: A method is basically a behaviour. A class can contain many methods. It is in methods where the logics are written, data is manipulated and all the actions are executed. Instance Variables: Each object has its unique set of instance variables. An object's state is created by the values assigned to these instance variables. Objects and classes A class is a definition about objects The attributes and methods in a class are thus declarations that do not contain values. objects are created instances of a class. Each has its own attributes and methods The values of the set of attributes describe the state of the objects. Objects Class Mercedes Car Volvo Toyota Objects Class Fruit Objects Class Student So, a class is a template for objects, and an object is an instance of a class. When the individual objects are created, they inherit all the variables and methods from the class. Objects in Programming languages If we consider the real-world, we can find many objects around us, cars, dogs, humans, etc. All these objects have a state and a behaviour. If we consider a dog, then its state is - name, breed, colour, and the behaviour is - barking, wagging the tail, running. If you compare the software object with a real-world object, they have very similar characteristics. Software objects also have a state and a behaviour. A software object's state is stored in fields and behaviour is shown via methods. So in software development, methods operate on the internal state of an object and the object-to-object communication is done via methods. States and behaviours State Behaviour Fields Functions Instance Variables Methods Properties Procedures Variables Members A class can contain any of the following 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 initialized when the class is instantiated. Instance variables can be accessed from inside any method, constructor or blocks of that particular class. Class variables: Class variables are variables declared within a class, outside any method, with the static keyword. A class can have any number of methods to access the value of various kinds of methods. In the above example, barking(), hungry() and sleeping() are methods. Constructors When discussing about classes, one of the most important sub topic would be constructors. Every class has a constructor. If we do not explicitly write a constructor for a class, the OOp language i.e. Java/ C# compiler builds a default constructor for that class. Each time a new object is created, at least one constructor will be invoked. The main rule of constructors is that they should have the same name as the class. A class can have more than one constructor. Syntax class ClassName { ClassName() { } } Basic Syntax About programs, it is very important to keep in mind the following points. Case Sensitivity: C# is case sensitive, which means identifier Hello and hello would have different meaning in C#. Class Names: For all class names the first letter should be in Upper Case. If several words are used to form a name of the class, each inner word's first letter should be in Upper Case. Example: class MyFirstJavaClass Method Names: All method names should start with a Lower Case letter. If several words are used to form the name of the method, then each inner word's first letter should be in Upper Case. Example: public void myMethodName() Creating an Object There are three steps when creating an object from a class: Declaration: A variable declaration with a variable name with an object type. Instantiation: The 'new' keyword is used to create the object. Initialization: The 'new' keyword is followed by a call to a constructor. This call initializes the new object. [Object type: [ClassName] variable= new [Constructor](); Example public class Book{ public Book (){ { } What about creating a new object of the Book Class? Book book= new Book(); public class Book { public Book(){ // this is constuctur has no parameters System.out.println("the title is empty"); } public Book (String title){ // this is constuctur has one parameter, "title" This is Java code System.out.println("the title is:" + title); } public static void main(String [] args){ Book book = new Book("Java"); Book book1=new Book(); } } public class Book { public Book(){ // this is constuctur has no parameters Console.WriteLine ("the title is empty"); } public Book (String title){ // this is constuctur has one parameter, "title" Console.WriteLine ("the title is:" + title); This is C# code } public static void main(String [] args){ Book book = new Book("Java"); Book book1=new Book(); } } UML: Representing a Class Graphically Dog - name : String State “-” means private access + bark() : void + eat(): String Behaviour “+” means public access UML: Representing a Class Graphically Car - model : String State “-” means private access + ride() : void + stop(): void Behaviour “+” means public access Classes A class is a blueprint from which individual objects are created. Following is a sample of a class. public class Dog { private String name ; void bark() { } String eat() { Return; } } Vehicle Car Airplane Ship Thank you

Use Quizgecko on...
Browser
Browser