Object Oriented
Programming with Java
www.jakir.me
mail@jakir.me
-with Jakir Hossain
Object-oriented
 An object-oriented language is one that is built around the concept of
objects.
 The basic unit of OOP ...
Class & Objects
 A Class is a blue print of a particular classification of Objects.
 An Object belongs to a Class of Obj...
Class
 class can be visualized as a three-compartment box, as illustrated:
1. Name (or identity): identifies the class.
2...
Object
 Objects are made up of attributes and methods.
 Objects have a lifespan but classes do not.
 An object is creat...
Naming Conventions
 Leading uppercase letter in class name
public class MyClass {
...
}
 Leading lowercase letter in fie...
Encapsulation
 Encapsulation means putting together all the variables (instance variables)
and the methods into a single ...
Inheritance
 An important feature of object-oriented programs is inheritance—the ability
to create classes that share the...
Inheritance
Polimorphism
 Polymorphism definition is that Poly means many and morphos means forms.
It describes the feature of langua...
Class Definition
 In Java, we use the keyword class to define a class. For examples:
 Creating Instances of a Class
publ...
Defining Methods (Functions Inside Classes)
 Basic method declaration:
public ReturnType methodName(type1 arg1,
type2 arg...
Class with Method
 Without Return Type:
 With Return Type:
public class Car{ // class name
int model; // variables
void ...
Dot Operator
 The variables and methods belonging to a class are formally called member
variables and member methods. To ...
Full Example
// File: car.java
public class Car{ // class name
int model; // variables
void printModel(){ // Define Method...
Constructors
 Constructors are special functions called when a class is created with
new
 Constructors are especially us...
The this Variable
 Within an instance method or a constructor, this is a reference
to the current object — the object who...
Full Example
// File: car.java
public class Car{ // class name
int model; // variables
Car(int model){ // Constractor
this...
Some Kye Points
 Class names should start with uppercase; method names with lowercase
 Methods must define a return type...
Questions?
Object Oriended Programming with Java
Upcoming SlideShare
Loading in …5
×

Object Oriended Programming with Java

352 views

Published on

Published in: Engineering, Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
352
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
11
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Object Oriended Programming with Java

  1. 1. Object Oriented Programming with Java www.jakir.me mail@jakir.me -with Jakir Hossain
  2. 2. Object-oriented  An object-oriented language is one that is built around the concept of objects.  The basic unit of OOP is a class.  Object-oriented languages allow us to define objects like Car or Mugs and access their properties in our code.  We can also send messages to objects, so for my mug I might want to know “Is it empty?” We can then create and manipulate all sorts of objects to do different things in our app.  For example, we can use the Camera object to take a photo. The Camera object represents the physical camera on an Android phone, but in a way that we can interact with in code.  Object can be vertual entity, like our fb account.
  3. 3. Class & Objects  A Class is a blue print of a particular classification of Objects.  An Object belongs to a Class of Objects  An Object is an instrance of a Class. For Example:  myCar, khansCar are instances of a class, called Car  myCat, khansDog are instance of a Class called, Animal.
  4. 4. Class  class can be visualized as a three-compartment box, as illustrated: 1. Name (or identity): identifies the class. 2. Variables (or attribute, state, field): contains the static attributes of the class. 3. Methods (or behaviors, function, operation): contains the dynamic behaviors of the class.
  5. 5. Object  Objects are made up of attributes and methods.  Objects have a lifespan but classes do not.  An object is created from a class.  Object has some Atributes  Has some Oparation we can to those Atributes  There are three steps when creating an object from a class: 1. Declaration: A variable declaration with a variable name with an object type. 2. Instantiation: The 'new' key word is used to create the object. 3. Initialization: The 'new' keyword is followed by a call to a constructor. This call initializes the new object.
  6. 6. Naming Conventions  Leading uppercase letter in class name public class MyClass { ... }  Leading lowercase letter in field, local variable, and method (function) names  myField, myVar, myMethod
  7. 7. Encapsulation  Encapsulation means putting together all the variables (instance variables) and the methods into a single unit called Class.  It also means hiding data and methods within an Object.  Encapsulation provides the security that keeps data and methods safe from inadvertent changes.  Programmers sometimes refer to encapsulation as using a “black box,” or a device that you can use without regard to the internal mechanisms. A programmer can access and use the methods and data contained in the black box but cannot change them.
  8. 8. Inheritance  An important feature of object-oriented programs is inheritance—the ability to create classes that share the attributes and methods of existing classes, but with more specific features.  Inheritance is mainly used for code reusability. So you are making use of already written class and further extending on that.  We can tell that deriving a new class from existing class, it’s called as Inheritance.
  9. 9. Inheritance
  10. 10. Polimorphism  Polymorphism definition is that Poly means many and morphos means forms. It describes the feature of languages that allows the same word or symbol to be interpreted correctly in different situations based on the context.
  11. 11. Class Definition  In Java, we use the keyword class to define a class. For examples:  Creating Instances of a Class public class Car{ // class name int model; // variables } Car myCar =new Car();
  12. 12. Defining Methods (Functions Inside Classes)  Basic method declaration: public ReturnType methodName(type1 arg1, type2 arg2, ...) { ... return(something of ReturnType); }  Exception to this format: if you declare the return type as void  This special syntax that means “this method isn’t going to return a value – it is just going to do some side effect like printing on the screen”  In such a case you do not need (in fact, are not permitted), a return statement that includes a value to be returned
  13. 13. Class with Method  Without Return Type:  With Return Type: public class Car{ // class name int model; // variables void printModel() { // Define Method System.out.println(“Moedl is:”+model); } } public class Car{ // class name int model; // variables Int getModel() { // Define Method return model; // Return a value } }
  14. 14. Dot Operator  The variables and methods belonging to a class are formally called member variables and member methods. To reference a member variable or method, we must: 1. first identify the instance you are interested in, and then 2. Use the dot operator (.) to reference the member (variable or method).
  15. 15. Full Example // File: car.java public class Car{ // class name int model; // variables void printModel(){ // Define Method System.out.println("Moedl is: "+model); } } // File: cardemo.java public class CarDemo { public static void main(String[] args) { Car newCar = new Car(); // Instantiating Class newCar.model = 2014; // Assigning Value newCar.printModel(); // Calling the Method } }
  16. 16. Constructors  Constructors are special functions called when a class is created with new  Constructors are especially useful for supplying values of fields  Constructors are declared through: public ClassName(args) { ... }  Notice that the constructor name must exactly match the class name  Constructors have no return type (not even void), unlike a regular method  Java automatically provides a zero-argument constructor if and only if the class doesn’t define it’s own constructor  That’s why you could say Car myCar = new Car(); in the first example, even though a constructor was never defined
  17. 17. The this Variable  Within an instance method or a constructor, this is a reference to the current object — the object whose method or constructor is being called.  The common uses of the this reference are: 1. To pass a reference to the current object as a parameter to other methods someMethod(this); 2. To resolve name conflicts  Using this permits the use of instance variables in methods that have local variables with the same name
  18. 18. Full Example // File: car.java public class Car{ // class name int model; // variables Car(int model){ // Constractor this.model = model; } void printModel(){ // Define Method System.out.println("Moedl is: "+model); } } // File: cardemo.java public class CarDemo { public static void main(String[] args) { Car newCar = new Car(2014); // Instantiating Class newCar.printModel(); // Calling the Method } }
  19. 19. Some Kye Points  Class names should start with uppercase; method names with lowercase  Methods must define a return type or void if no result is returned  Static methods do not require an instance of the class; static methods can be accessed through the class name  The this reference in a class refers to the current object  Class constructors do not declare a return type  Override methods - redefine inherited methods
  20. 20. Questions?

×