Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Introduction to OOP - ITFest 2016 - Mobile Workshop

v-rusu
April 11, 2016

Introduction to OOP - ITFest 2016 - Mobile Workshop

v-rusu

April 11, 2016
Tweet

Other Decks in Programming

Transcript

  1. Introduction to OOP • Classes & instances • Member variables

    and methods • Constructors • Method overloading • Inheritance • Method overriding
  2. Classes • A definition of objects of the same kind

    • Describes attributes and behaviors of objects
  3. Class definition public class Circle { // class name double

    radius; // variables String color; double getRadius() { ...... } // methods double getArea() { ...... } } A class name shall be a noun All the words shall be initial-capitalized (camel-case) Use a singular noun for class name
  4. Class instances • An instance is a realization of a

    particular item of a class • All the instances of a class have similar properties, as described in the class definition • The term object refers to the instance of a class
  5. Class instances // Declare 2 instances of the class Circle,

    c1 and c2 Circle c1, c2; // Allocate and construct the instances via new operator c1 = new Circle(); c2 = new Circle(2.0); // You can declare and construct in the same statement Circle c4 = new Circle(2.0, “red”); To create an instance of a class, you have to: Declare an instance identifier (instance name) of a particular class. Construct the instance (i.e., allocate storage for the instance and initialize the instance) using the "new" operator.
  6. Member variables • A member variable has a name (or

    identifier) and a type • It holds a value of that particular type • A member variable can also be an instance of a certain class
  7. Member variables public class Circle { // class name double

    radius; // variables String color; double getRadius() { ...... } // methods double getArea() { ...... } } A variable name shall be a noun or a noun phrase made up of several words. The first word is in lowercase and the rest of the words are initial- capitalized (camel-case) Take note that variable name begins with an lowercase, while class name begins with an uppercase.
  8. Member methods • A method receives parameters from the caller

    • It performs the operations defined in the method body • A method returns a piece of result (or void) to the caller
  9. Member methods public class Circle { // class name double

    radius; // variables String color; double getArea() { return radius * radius * Math.PI; } } A method name shall be a verb, or a verb phrase made up of several words The first word is in lowercase and the rest of the words are initial- capitalized (camel-case) Take note that variable name is a noun (denoting a static attribute), while method name is a verb (denoting an action).
  10. Constructors • A constructor is a special method that has

    the same method name as the class name • A constructor is used to construct and initialize all the member variables • To create a new instance of a class, you need to use a special "new" operator followed by a call to one of the constructors
  11. Constructors Circle c1 = new Circle(); Circle c2 = new

    Circle(2.0); Circle c3 = new Circle(3.0, "red"); The name of the constructor method is the same as the class name, and by classname convention, begins with an uppercase. Constructor has no return type (or implicitly returns void). Hence, no return statement is allowed inside the constructor's body. Constructor can only be invoked via the "new" operator. It can only be used once to initialize the instance constructed. You cannot call the constructor afterwards. Constructors are not inherited.
  12. Method overloading public class Circle {
 double radius;
 String color;


    
 public Circle() { ...... } 
 public Circle(int radius) { ...... }
 
 public Circle(int radius, String color) { ...... } } Method overloading means that the same method name can have different implementations The different implementations must be distinguishable by their argument list
  13. Inheritance • The process by which a class is derived

    from another class • The derived class is called a subclass, the class from which it is derived is called a superclass • The derived class inherits all the member variables and methods from it’s superclass • The constructors are not members, so they are not inherited.
  14. Inheritance public class Person { String fullName; void makeBreakfast() {

    ...... } } public class Student extends Person { int studentID; } Student inherits from Person. Student is the subclass, Person is the superclass Inheritance is marked by the keyword extends. Now an instance of the Student class will have access to the studentID member as well as the fullName and makeBreakfast() members of it’s superclass
  15. Method overriding • Allows a class to inherit the behavior

    from a superclass and then to modify it as needed • The overriding method has the same name, number and type of parameters, and return type as the method that it overrides • Optional: use the @Override annotation to signal the compiler that you want to override a method
  16. Method overriding public class Person { String fullName; void makeBreakfast()

    { ...... } } public class Student extends Person { int studentID; @Override void makeBreakfast() { System.out.println(“I’m not that hungry”); } }