OO programming

Cards (65)

  • OOP
    Object Oriented Programming
  • In procedural programming
    groups of actions that perform some task are formed into functions and functions are grouped to form programs
  • In OOP programmers concentrate on creating their own user-defined types called classes
  • In OOP each class contains data as well as the set of methods (procedures) that manipulate the data
  • In OOP an instance of a user-defined type (ie class) is called an object
  • OOP encapsulates data (attributes) and methods (behaviours) into objects which contain the data and methods specific to an object
  • Objects can information hide
  • Inheritance
    is a form of software reusability in which new classes are created from the existing classes by absorbing their attributes and behaviours
  • why use inheritance?
    code reusability - a new class can inherit attributes and behaviours of the existing class (superclass)
  • can you add more attributes and behaviours to a subclass?
    Yes, because of this subclasses can have more features than their super classes
  • what's an easy way to if its an inheritance relationship?
    Using the "is-a" relationship model. An UndergradStudent is a Student too.
  • Don’t use inheritance unless all or most inherited attributes and methods make sense.

    For example, mathematically a circle is-a (an) oval, however you should not inherit a class circle from a class oval. A class oval can have one method to set width and another to set height.
  • Whats an easy way to see if its an Association relationship?
    Using the "has-a" relationship model. For example, a Rectangle Is-NOT-a Line. However, we may use a Line to draw a Rectangle
  • What's an Association relationship?
    When a class object has an object of another class to store its state or do its work
  • What is a "has-a" relationship example of?
    its an example of creating new classes by Composition of existing classes
  • What's a class?

    A class is a collection of data and methods (procedures) that operate on that data.
  • What's an object?
    Objects are instances of a class.
  • What's a method?
    It's a function
  • Every class has a superclass
  • Abstract classes are classes that define only part of an implementation. They extend classes to provide additional functionality to some or all the methods.
  • The benefit of an abstract class is that the methods may be declared such that the programmer knows the interface definition of an object but methods can be implemented differently in different subclasses of the abstract class.
  • An abstract class is a class that is declared abstract.
  • If a class includes abstract methods, then the class itself must be declared abstract
  • An abstract class cannot be instantiated.
  • A subclass of an abstract class can be instantiated if it overrides each of the abstract methods of its superclass and provides an implementation for all of them
  • If a subclass of an abstract class does not implement all the abstract methods it inherits, that subclass is itself abstract.
  • In Java, a new class can extend exactly one superclass - a model known as single inheritance.
  • Some object-oriented languages employ multiple inheritance, where a new class can have two or more super classes.
  • • In multiple inheritance, problems arise when a superclass’s behaviour is inherited in two/multiple ways.
  • Variables declared in an interface must be static and final, that means, they must be constants.
  • Just like a class extends its superclass, it also can optionally implements an interface.
  • In order to implement an interface, a class must first declare the interface in an implements clause, and then it must provide an implementation for all of the abstract methods of the interface.
  • A class can “implements” more than one interfaces.
  • Method Forwarding
    If in class C, we want to use methods implemented in P we can do this by creating an object of type class P in class C, and through this object access all the methods implemented in P. Note that, in class C, we do need to provide required stubs for all the methods in the interface X. In the body of the methods we may simply call methods of class P via the object of class P.
  • Method Overriding (Polymorphism)

    When a class defines a method using the same name, return type, and by the number, type, and position of its arguments as a method in its superclass, the method in the class overrides the method in the superclass
  • Polymorphism
    An object’s ability to decide what method to apply to itself, depending on where it is in the inheritance hierarchy.
  • How do you use method overriding?
    With super.f()
  • Method Overloading
    Defining methods with the same name and different argument or return types
  • a method is distinguished by its method signature - its name, return type, and by the number, type, and position of its arguments
  • Good practice to define the required constructors for all classes