1 / 25

Software Engineering Foundations

Dept of Computer Science. Software Engineering Foundations. Summary Object Oriented Concepts. Monica Farrow EM G30 email : M.Farrow@hw.ac.uk Material available on Vision. Key OOP concepts. Basic OOP OOP, objects, classes Encapsulation and data/information hiding

shiloh
Télécharger la présentation

Software Engineering Foundations

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Dept of Computer Science Software Engineering Foundations Summary Object Oriented Concepts Monica Farrow EM G30 email : M.Farrow@hw.ac.uk Material available on Vision F21SF1 Maps End

  2. Key OOP concepts • Basic OOP • OOP, objects, classes • Encapsulation and data/information hiding • Coupling and cohesion • Static • Inheritance • Superclasses and subclasses • Abstract methods and classes • Interfaces • Polymorphism, overloading and overriding.

  3. OOP • OOP – object oriented programming • A style of designing programs based on ‘objects’ • Read the java tutorial • http://docs.oracle.com/javase/tutorial/java/concepts/ • Here is a good summary • http://www.cis.upenn.edu/~matuszek/cit591-2011/Pages/o-o-concepts.html • Also • http://www.ntu.edu.sg/home/ehchua/programming/java/J3a_OOPBasics.html

  4. Objects • An object is often a real-world object such as a car. • An object has • state (attributes/properties/data/fields) • e.g. model name, tank size, fuel in the tank • And • Behaviour (operations/functions/methods) • e.g. • How far can it travel on a full tank • Whether the tank is empty or not • The tank can be filled (amount of fuel in the tank altered)

  5. Class • A class is a template for an object • It describes the attributes (instance variables) and operations (methods) for any object of the class • A class doesn’t define actual objects Car model : String tankSize : int fuelInTank : double getModel() : String getTankSize() : int setFuelInTank (double fuel): void Etc etc

  6. Objects (again) • An object is an instance of the class. • There can be many objects of a class • E.g. Person -> Monica Farrow, Hamish Taylor, etc etc • Objects can be instantiated (created)

  7. How a program works https://www3.ntu.edu.sg/home/ehchua/programming/cpp/cp3_OOP.html

  8. Encapsulation • In English ‘encapsulate’ means to enclose as if in a capsule. • In OOP, it means that the attributes and operations for objects of a class are held together. • http://www.c-sharpcorner.com/UploadFile/e881fb/learn-object-oriented-programming-using-C-Sharp-part-5/Images/Encapsulation.jpg F21SF1 Maps End

  9. Data hiding / Information hiding • Encapsulation enables data hiding, also called information hiding. • The internal state of the object (the instance variables) are hidden from objects of other classes. • Their access modifier is private • These variables are then accessible by providing accessor (‘get’) methods • Their access modifier is public • Similarly, they can be modified by providing mutator (‘set’) methods • Their access modifier is public F21SF1 Maps End

  10. Using information hiding • The objects pass messages to each other using the public methods. • The method signature is known (method name, parameters, return type) • This is the interface • The implementation details (what the body of the code does) is hidden, encapsulated within the class F21SF1 Maps End

  11. Coupling • Coupling is about the inter-connected-ness of different classes. • Loose coupling means that changes in one class should not usually require changes in other classes. • Information hiding supports loose coupling • E.g. change the way that a name is stored inside the class, from a single String to 2 Strings for first names and last names • Change the instance variables and the public method bodies • Do NOT change the method signatures (the public interface) • The classes using the methods do not need altering. F21SF1 Maps End

  12. Cohesion • Cohesion is about a class having responsibility for set of closely related tasks. • E.g. • Put a method to get how far a car can travel inside the Car class • Put a method to get the initials inside the Name class • Don’t put lots of methods to manipulate the owner name string inside a Car class – they’re not about cars. F21SF1 Maps End

  13. Coupling and cohesion • An OOP program should have loose coupling and high cohesion. • Separating entities into separate logical units makes them modular, easier to code, understand, analyze, test, and maintain. • The logical units can also be re-used in other programs. F21SF1 Maps End

  14. Static variables and constants • A class may contain static variables or constants. • A static constant has the same value for all objects of the class and never changes E.g. • the number of days in a week • the number of holidays that all Employees can take (if this will NOT change) • A static variable has the same value for all objects of the class and might change during the program run E.g. • number of holidays that all Employees can take (if this might be changed during a program run) F21SF1 Maps End

  15. Static methods • A class may contain static methods • These can only access static variables, constants and other static methods • They often provide useful functionality • They do NOT use instance variables or non-static methods • E.g. method to return the days of the week • E.g. Math.round, String.format, main method F21SF1 Maps End

  16. Inheritance • A class can • Get some characteristics (instance variables, methods) from a parent or superclass. A subclass extends a superclass. A subclass is a superclass. • Provide its own characteristics specific to itself. It is the child or sub-class. • E.g. • Superclass Shape specifies the colour. All shapes have a colour. • Subclass Circle specifies radius, subclass Square specifies side. These attributes are specific to Circle and Square. F21SF1 Maps End

  17. Shape colour:String getColour() : String setColour (String c) :void Inheritance Circle radius:double getRadius() : double Square side:double getSide() : double

  18. Object toString() : String equals(o : Object) : boolean Shape Circle Square Object class • The Object class is the superclass for all classes • It contains default toString and equals methods, based only upon class name and location in memory.

  19. Overriding • Subclasses often provide their own toString and equals methods. • This is called overriding, the method in the subclass is used in preference to the method in the superclass. • The method in the superclass can be called, using ‘super’ e.g. super.toString().

  20. Overloading • Overloading is using the same operation name with a different purpose. E.g. • System.out.println(5); //an int • System.out.println(“Hello”); // a String • Overloading is also used for the + operator in java • int y = 3; int z = y + 2; • String s = “Hel” + “lo”;

  21. Polymorphism • Polymorphism means ‘changing form’ • In Java, this means • Overloading – same method name, different parameters • Overriding – same method name, different subclass

  22. Abstract methods • An abstract method contains no code • An abstract method is a method signature, defining the interface for a method which must be supplied by the subclass. • E.g. • Shape superclass could define an abstract method getArea(). • The implementation is in each subclass.

  23. Abstract class • An abstract class contains • Some abstract methods. No implementation. E.g. Shape getArea() • Some concrete variables and methods, including the implementation. E.g. Shape colour, getColour(). • You can NOT call the constructor of an abstract class directly • You can create (instantiate) objects of the subclass. Their constructors contain a call to the superclass constructor (super() )

  24. Interface • An interface is like an abstract class without the concrete parts. An interface is a specification without implementation. • All variables are static • All methods are abstract • A class can implement an interface. • This means that the class MUST provide (implement) all methods defined by that interface (e.g. actionPerformed in GUIs)

  25. Multiple inheritance • In Java, a subclass can only extend one superclass. • In Java, a class can extend any number of interfaces. • E.g. • class Class extends Superclass implements Interface1, Interface2, .....

More Related