Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
O bject  o riented  p rogramming  java OOP  Understanding MOHAMMAD ALSHEHRI LS0806205
What is Object-Oriented Programming? Object oriented programming or OOP is a way of writing programs using objects.  {   A...
There are four main pillars of an OOP: <ul><li>Inheritance </li></ul><ul><li>Encapsulation: </li></ul><ul><li>Polymorphism...
Inheritance provide the facility to drive one class by another using simple syntax. You can say that it is a process of cr...
Encapsulation is the ability to bundle the property and method of the object and also operate them. It is the mechanism of...
As the name suggest one name multiple form, Polymorphism is the way that provide the different functionality by the functi...
It is the way that provide the maximum functionality to a program for a specific type at runtime. There are two type of bi...
<ul><li>As the languages like C, C++ fulfills the above four characteristics yet they  are not fully object oriented langu...
First program in java <ul><li>public class FirstProgram  </li></ul><ul><li>{  </li></ul><ul><li>      public static void m...
<ul><li>Class:  A class defines the properties and behavior (variables and methods) that is shared by all its objects. It ...
class  Check{   private int amount=0;    public int  getAmount(){      return amount;   }    public void  setAmount( int  ...
Using classes and objects Before you can create an object you need to create a class. A class is the code for an object an...
Creating a class You have already been creating classes in the programs you have written so far. To show you how they work...
Now we will add 2 variables to the class which are the student's name and student number. public class Student {    String...
Next we must add methods for getting and setting these variables. public class Student {    String studentName;    int stu...
Instantiating an object Now that we have finished writing the class we must create the main program that will instantiate ...
Using the object Now we can call the methods from the stu object to set the values of its variables. You can't set the val...
Constructors A constructor is a method that is run when an object is instantiated. Constructors are useful for initializin...
<ul><li>public class Student {    String studentName;    int studentNumber;      Student()    {       studentName = &quot;...
You can also pass parameters to a constructor when you instantiate an object. All you need to do is add the parameters in ...
<ul><li>public class Student </li></ul><ul><li>{    String studentName;    int studentNumber;      Student(String s, int i...
Now all you need to do is put the parameters in the brackets when you instantiate the object in the main program. public c...
How to use inheritance We will first create a parent class called Person. We will then create a child class called Student...
Now we will create the Student class that has a variable for the student number and the get and set methods for student nu...
public class AnimalReference { public static void main(String args[]) Animal ref  // set up var for an Animal Cow aCow = n...
<ul><li>شكرا </li></ul><ul><li>谢谢 </li></ul><ul><li>Thanks </li></ul>中沙友谊地久天长
 
Upcoming SlideShare
Loading in …5
×

Oop

1,000 views

Published on

Published in: Technology
  • Be the first to comment

  • Be the first to like this

Oop

  1. 1. O bject o riented p rogramming java OOP Understanding MOHAMMAD ALSHEHRI LS0806205
  2. 2. What is Object-Oriented Programming? Object oriented programming or OOP is a way of writing programs using objects. { An object is a data structure in memory that has attributes and methods. { The attributes of an object are the same as variables and the methods of an object are the same as functions or procedures. The reason for using objects instead of the old procedural method of programming is because objects group the variables and methods about something together instead of keeping them all apart as in procedural programming
  3. 3. There are four main pillars of an OOP: <ul><li>Inheritance </li></ul><ul><li>Encapsulation: </li></ul><ul><li>Polymorphism </li></ul><ul><li>Dynamic binding </li></ul>
  4. 4. Inheritance provide the facility to drive one class by another using simple syntax. You can say that it is a process of creating new class and use the behavior of the existing class by extending them for reuse the existing code and adding the additional features as you need. It also use to manage and make well structured software. <ul><ul><li>Inheritance: </li></ul></ul>
  5. 5. Encapsulation is the ability to bundle the property and method of the object and also operate them. It is the mechanism of combining the information and providing the abstraction as well. <ul><ul><li>Encapsulation : </li></ul></ul>
  6. 6. As the name suggest one name multiple form, Polymorphism is the way that provide the different functionality by the functions  having the same name based on the signatures of the methods. There are two type of polymorphism first is run-time polymorphism and second is compile-time polymorphism. <ul><ul><li>Polymorphism: </li></ul></ul>
  7. 7. It is the way that provide the maximum functionality to a program for a specific type at runtime. There are two type of binding first is dynamic binding and second is static binding. <ul><ul><li>Dynamic binding: </li></ul></ul>
  8. 8. <ul><li>As the languages like C, C++ fulfills the above four characteristics yet they  are not fully object oriented languages because they are structured as well as object oriented languages. But in case of java,  it is a fully Object Oriented language because object is at the outer most level of data structure in java. No stand alone methods, constants, and variables are there in java. Everything in java is object even the primitive data types can also be converted into object by using the wrapper class. </li></ul>
  9. 9. First program in java <ul><li>public class FirstProgram </li></ul><ul><li>{ </li></ul><ul><li>     public static void main(String[] args) </li></ul><ul><li>     { </li></ul><ul><li>          System.out.println(&quot;Hey! you are going to compile and run your first Java program&quot;); </li></ul><ul><li>     } </li></ul><ul><li>} </li></ul>
  10. 10. <ul><li>Class: A class defines the properties and behavior (variables and methods) that is shared by all its objects. It is a blue print for the creation of  objects. The primitive data type and keyword void is work as a class object. </li></ul><ul><li>Object:  Object is 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.  </li></ul>
  11. 11. class  Check{   private int amount=0;   public int  getAmount(){      return amount;   }   public void  setAmount( int  amt){      amount=amt;   }  } public class  Mainclass{   public static void  main(String[] args){      int amt=0;      Check obj=  new  Check();      obj.setAmount(200);      amt=obj.getAmount();       System.out.println(&quot;Your current amount is :&quot;+amt);      } }
  12. 12. Using classes and objects Before you can create an object you need to create a class. A class is the code for an object and an object is the instance of a class in memory. When you create an object from a class it is called instantiating the object. To help you understand think of the plan for a house. The plan for the house is like the class and the house that will be built from the plan is the object.
  13. 13. Creating a class You have already been creating classes in the programs you have written so far. To show you how they work we will need to create a separate class in a separate file. This class will be called Student. public class Student { }
  14. 14. Now we will add 2 variables to the class which are the student's name and student number. public class Student {    String studentName;    int studentNumber; }
  15. 15. Next we must add methods for getting and setting these variables. public class Student {    String studentName;    int studentNumber;      public void setStudentName(String s)    {       studentName = s;    }      public void setStudentNumber(int i)    {       studentNumber = i;    }      public String getStudentName()    {       return studentName;    }      public int getStudentNumber()    {        return studentNumber;    } }
  16. 16. Instantiating an object Now that we have finished writing the class we must create the main program that will instantiate the object. We will call the main program class TestClass. It should look just like how every other java program starts off. public class TestClass {    public static void main(String[] args)    {    } } Now we will instantiate the object. To do this we declare a reference to the object called stu and set it equal to a newly created object in memory. public class TestClass {    public static void main(String[] args)    {       Student stu = new Student();    } }
  17. 17. Using the object Now we can call the methods from the stu object to set the values of its variables. You can't set the values of the variables in an object unless they are declared as public. This makes it safer to work with variables because they can't be changed by mistake by another object. public class TestClass {    public static void main(String[] args)    {       Student stu = new Student();       stu.setStudentName(&quot;John Smith&quot;);       stu.setStudentNumber(12345);       System.out.println(&quot;Student Name: &quot; + stu.getStudentName());       System.out.println(&quot;Student Number: &quot; + stu.getStudentNumber());    } }
  18. 18. Constructors A constructor is a method that is run when an object is instantiated. Constructors are useful for initializing variables. The constructor is declared with only the name of the class followed by brackets. Here is an example of the Student class that initializes both student name and student number.
  19. 19. <ul><li>public class Student {    String studentName;    int studentNumber;      Student()    {       studentName = &quot;No name&quot;;       studentNumber = 1;    }      public void setStudentName(String s)    {       studentName = s;    }      public void setStudentNumber(int i)    {       studentNumber = i;    }      public String getStudentName()    {       return studentName;    }      public int getStudentNumber()    {        return studentNumber;    } } </li></ul>
  20. 20. You can also pass parameters to a constructor when you instantiate an object. All you need to do is add the parameters in the brackets after the constructor declaration just like a normal method.
  21. 21. <ul><li>public class Student </li></ul><ul><li>{    String studentName;    int studentNumber;      Student(String s, int i)    {       studentName = s;       studentNumber = i;    }      public void setStudentName(String s)    {       studentName = s;    }      public void setStudentNumber(int i)    {       studentNumber = i;    }      public String getStudentName()    {       return studentName;    }      public int getStudentNumber()    {        return studentNumber;    } } </li></ul>
  22. 22. Now all you need to do is put the parameters in the brackets when you instantiate the object in the main program. public class TestClass {    public static void main(String[] args)    {       Student stu = new Student(&quot;John Smith&quot;,12345);       System.out.println(&quot;Student Name: &quot; + stu.getStudentName());       System.out.println(&quot;Student Number: &quot; + stu.getStudentNumber());    } }
  23. 23. How to use inheritance We will first create a parent class called Person. We will then create a child class called Student. Then we will create a program to use the Student class. The following is a Person class which has a variable for the person's name. There are also set and get methods for the name. class Person {    private String name;      public void setName(String n)    {       name = n;    }      public String getName()    {       return name;    } }
  24. 24. Now we will create the Student class that has a variable for the student number and the get and set methods for student number. The extends keyword is used to inherit from the Person class in the following example. class Student extends Person {    private String stuNum;      public void setStuNum(String sn)    {       stuNum = sn;    }      public String getStuNum()    {       return stuNum;    } }
  25. 25. public class AnimalReference { public static void main(String args[]) Animal ref // set up var for an Animal Cow aCow = new Cow(&quot;Bossy&quot;); // makes specific objects Dog aDog = new Dog(&quot;Rover&quot;); Snake aSnake = new Snake(&quot;Earnie&quot;); // now reference each as an Animal ref = aCow; ref.speak(); ref = aDog; ref.speak(); ref = aSnake; ref.speak(); } Polymorphism
  26. 26. <ul><li>شكرا </li></ul><ul><li>谢谢 </li></ul><ul><li>Thanks </li></ul>中沙友谊地久天长

×