It is OOP, not oops! It stands for object oriented programming, a paradigm of organising /structure computer codes.
Any questions first?
Definitions and Examples (poly section)
Keywords: Inheritance, extends, multiple base, super(), overriding, new members, overloading; polymorphism, data type, choice of method, arraylist, array; Dependency, relationship, association, composition, aggregation.
The FOUR pillars of OOP are abstraction, encapsulation, inheritance, and polymorphism.
There are two major steps in OOP at the highest level: Design the design (blue prints) and Create using the blue prints.
The blue prints are called CLASS. The products made from the plans are called INSTANCE (object).
The OBJECT/INSTANCE encapsulate all properties and methods within. The access to them is only via public interfaces (setters and getters). - enhanced security - extra work for programmers! Properties are the features of the object - e.g. colour, size, name, etc. Methods are the functions of the object - what it can do and done to it.
Abstraction - a must-do process with any computer based solutions - to ignore unnecessary details and keep only the important details - to make up the properties and methods. - In Procedural programming, this is where you determine what variables and what functions to use. AN EXTRA LAYER OF MEANING to ABSTRACTION: Using function / method calls to hide the design details, even using the class design to hide the internal changes of object. (e.g. no need to learn new command even if the design of print() has been changed)
OOP makes it easier to communicate to non-programmers, enhanced team work.
Procedural-oriented programming! Procedural - 1 - step by step, sequence based. 2. relying on procedures (functions and methods). This is prior to OOP and still in use.
Another example of programmers learning from the world around us! OOP makes certain problem solving tasks easier and better, more scalable, robust etc.
Java
import java.util.*;public class Demos { public static void main(String args[]) { int x=10; int y=25; int z=x+y; System.out.println("Sum of x+y = " + z); Employee e = new Employee("John","Davies", new Date("01/01/1990")); System.out.println(e.toString()); Teacher t = new Teacher("John","Davies", new Date("01/01/1990"), "HOD"); System.out.println(t.toString()); Teacher t1 = new Teacher( "TA"); System.out.println(t1.toString()); Teacher t2 = new Teacher(); System.out.println(t2.toString()); }}class Employee { String lastname; String firstname; Date dob; public Employee(String fname, String last, Date dob){ this.firstname = fname; this.lastname = last; this.dob = dob; } public String toString(){ return this.firstname + " " + this.lastname + " was born on " + this.dob.toString() +"."; } public void setName(String last, String first){ this.lastname = last; this.firstname = first; }}Output:
Sum of x+y = 35John Davies was born on Mon Jan 01 00:00:00 UTC 1990.John Davies was born on Mon Jan 01 00:00:00 UTC 1990, and works as a HODTA TA was born on Sun Feb 01 00:00:00 UTC 3880, and works as a TAteacher teacher was born on Sun Feb 01 00:00:00 UTC 3880, and works as a teacherPython
class Employee: def __init__(self, name): self.name = name def get_name(self): return self.nameAbstraction; Inheritance; Encapsulation; Polymorphism.
The class and instance design enables controlled access to properties and methods.
scope keywords: public, package and private denote different access right.
Public - accessible by all (all object in the program)
Private - only accessible to calls within the same class.
package - the default access right - if not declared.
Properties and methods can be inherited by sub-classes.
Let's use an example to illustrate. Model a team of 11 football players as part of a match.
"There are basically four types of players in a team: K, S, M, D. Each has its own features and functions and they all share certain features and functions/behaviours. So design one base class with the common members, then sub-classes for each type of player." (Arrow to show dependency, extends to inherit from a class).
methods from super-class and sub-classes could share the same name and the same signatures, but they do different things according to the datatype (to choose which version to use). For example, animalSound() could be used by dog, cat, cow, and duck to make their own sounds.
If not implemented in the sub-class, then the one in the super class (base class) will be used and this becomes an inheritance issue.
BELOW, get_name() is used to demonstrate polymorphism; get_pos() is for inheritance. (copy and paste into https://www.jdoodle.com/online-java-compiler/ , then run it to get this output:
a of unknown position! of 22 years old. POS = keeperb of unknown position! of 21 years old. POS = defenderc of unknown position! of 23 years old. POS = defenderd of unknown position! of 19 years old. POS = defendere as the striker! of 30 years old. POS = strikerf as the striker! of 27 years old. POS = strikerg of unknown position! of 39 years old. POS = defenderh as the mid-fielder! of 55 years old. POS = mid-fielderj as the mid-fielder! of 55 years old. POS = mid-fielderk as the mid-fielder! of 55 years old. POS = mid-fielderl as the mid-fielder! of 55 years old. POS = mid-fielder-------------public class Football { public static void main(String args[]) { Keeper a = new Keeper("a", 22, "keeper", 2.4, 0.95); Defender b =new Defender("b", 21, "defender", 0.90); Defender c =new Defender("c", 23, "defender", 0.90); Defender d =new Defender("d", 19, "defender", 0.90); Striker e =new Striker("e", 30, "striker", 0.90); Striker f =new Striker("f", 27, "striker",0.90); Defender g = new Defender("g", 39, "defender", 0.8); Mid h = new Mid("h", 55, "mid-fielder", 0.8, 0.3); Mid j = new Mid("j", 55, "mid-fielder", 0.7, 0.1); Mid k = new Mid("k", 55, "mid-fielder", 0.3, 0.2); Mid l = new Mid("l", 55, "mid-fielder", 0.5, 0.9); // Player xa = (Player)a; // System.out.println(xa.getClass().getName()); // System.out.println((xa instanceof Keeper) + " - XA as Keeper after casting as Player"); // System.out.println((xa instanceof Striker) + " - XA as Striker after casting as Player"); Player[] teamA = new Player[]{a, b, c, d, e, f, g, h, j, k, l}; //polymorphism // System.out.println(teamA[0] instanceof Keeper); // System.out.println(teamA[0] instanceof Player); // System.out.println(teamA[0].getClass().getName()); for (int cc = 0; cc<teamA.length; cc++){ System.out.println(teamA[cc].get_name() + " of " + teamA[cc].get_age() + " years old. POS = " + teamA[cc].get_pos()); } System.out.println("-------------"); }}class Player{ String name; int age;// String team; String pos; public Player(String name, int age, String pos){ this.name =name; this.age = age; this.pos = pos; } void set_name(String name){ this.name = name; } void set_age(int age){ this.age = age; } String get_name(){ return this.name + " of unknown position!"; } int get_age(){ return this.age; } String get_pos(){ return this.pos; }}class Keeper extends Player{ double height; double saves; public Keeper(String name, int age, String pos, double jump, double saves){ super(name, age, pos); this.height = jump; this.saves = saves; } //String get_name(){ // enable to test polymorphism // return this.name + " as the keeper!"; //} int get_age(){ return this.age; }// String get_pos(){ // disable to test inheritance// return this.pos;// } double get_jump(){ return this.height; } double get_saves(){ return this.saves; } void set_saves(double saves){ this.saves = saves; }}class Defender extends Player{ double tackles; public Defender(String name, int age, String pos, double tackles){ super(name, age, pos); this.tackles = tackles; } //String get_name(){ // return this.name + " as the defender!"; //} int get_age(){ return this.age; } //String get_pos(){ // return this.pos; //} double get_tackles(){ return this.tackles; } void set_tackles(double tkls){ this.tackles = tkls; }}class Striker extends Player{ double goals; public Striker(String name, int age, String pos, double goals){ super(name, age, pos); this.goals = goals; } String get_name(){ return this.name + " as the striker!"; } int get_age(){ return this.age; } String get_pos(){ return this.pos; } double get_goals(){ return this.goals; } void set_goals(int gls){ this.goals = gls; }}class Mid extends Player{ double goals; double tackles; public Mid(String name, int age, String pos, double tackles, double goals){ super(name, age, pos); this.goals = goals; this.tackles = tackles; } String get_name(){ return this.name + " as the mid-fielder!"; } int get_age(){ return this.age; } //String get_pos(){ // return this.pos;// } double get_goals(){ return this.goals; } void set_goals(int gls){ this.goals = gls; } double get_tackles(){ return this.tackles; } }interface to hide design details - function/method call at the lower level, the entire class design at the higher level.
The object / class is the abstraction of the real world entities (object, things, people, etc.)
On a different level, the setters and getters are the abstractions of the internal designs of the functions, and they hide the complexity by just providing the interface.
Benefits: to isolate changes - ease of use, easy to modify without interfering with users, .to speed up design, aid communication between co-workers and...
Where to place a feature?
Where to place a feature?
Class, object, instantiation, property, attribute, method, constructor,
default constructor, signature of a method, parameter, setter, getter, public interface,
inheritance, encapsulation, abstraction, polymorphism
zoo and animals
people and various roles
a zoo has a dog, a cat, a bird, a frog, a lion, a tiger, and an elephant. ...
Class diagram: Class name + Attributes + Methods
ERD : Class name + Attributes (RDB)
UML = unified modelling language
Class diagram, arrow points at the base class.
Distinguishing characteristics of object-oriented programming languages against other types of programming languages.
Java and C# are OOP only languages while C++ and Python are versatile, that is they can be used for procedural programming too. Each programming language has its own syntax - language rules. Some of them are similar or the same, some are unique. More examples will be added. 'new' is not used in Python for new instance of a class, while all others require it, "new ClassConstructor()". Python also allows easy/flexible access to properties en without setter/getter. While others forbid that.
Differences between paradigms - OOP and procedural programming. modularity is the key, plus the FOUR pillars of OOP.
The benefits of object-oriented programming, e.g. reusability, reliability, flexibility, multiplatform.
Multiplatform is not directly derived from OOP. - all benefits root from the structure of the code! Modularity, state maintenance in object, reusability of code and objects in application/between.
Multiplatform refers to compatibility with mobile, web, desktop etc.
It provides a clear modular structure for programs which makes it good for defining abstract datatypes in which implementation details are hiddenObjects can also be reused within and across applications. The reuse of software also lowers the cost of development. More effort is put into the object-oriented analysis and design, which lowers the overall cost of development.It makes software easier to maintain. Since the design is modular, part of the system can be updated in case of issues without a need to make large-scale changesReuse also enables faster development. Object-oriented programming languages come with rich libraries of objects, and code developed during projects is also reusable in future projects.It provides a good framework for code libraries where the supplied software components can be easily adapted and modified by the programmer. This is particularly useful for developing graphical user interfaces.Better Productivity as OOP techniques enforce rules on a programmer that, in the long run, help her get more work done; finished programs work better, have more features and are easier to read and maintain. OOP programmers take new and existing software objects and "stitch" them together to make new programs. Because object libraries contain many useful functions, software developers don't have to reinvent the wheel as often; more of their time goes into making the new program.Features of object-oriented programs, including:
o objects, e.g. instance identity, state, behaviour
The state is maintained by the attributes(properties, members) and behaviour is from the methods (functions, procedures and also members). The identifier is has the reference as the value, memory location.
o classes, e.g. constructors, destructors, abstract - destructor - destroy unused obj to reclaim memory - GC in java by default, C++ needs its own destroy… abstract class - abstract method - one without a body (implementation) just signature. Constructors are used to instantiate (construct, create, make) the object (instance). Default constructor is provided by the compiler if none provided by the programmer.
o methods, e.g. procedures, functions, arguments. (arg is passed in, param at design)
Features of object-oriented programming and their importance, including:
o encapsulation, e.g. objects, public, private, protected (the protected keyword is an access modifier for method and variable of a class. When a method or a variable is marked as protected, it can be accessed from: Within the enclosing class. Other classes in the same package as the enclosing class.) (package keyword creates a package, at the top of the module)
o data abstraction, e.g. data hiding; (via setters and getters only, not to access the properties directly.
o inheritance, e.g. subclasses, method overriding, extends, effect of public versus private on inheritance (private properties cannot be inherited - scope is private, only within the defining class)
o polymorphism, e.g. multiple implementation of methods
o relationships, e.g. association, aggregation, composition - relationship between objects - association. Composition is the association when one owns (contains) the other - body = hands + legs + .... Aggregation is an association when the two objects can interact but not dependent on each other. One dies while the other could still exist. Player and team for example..
o interfaces, e.g. implicit, explicit (Java is implicit. Explicit is where you are implementing multiple interfaces that have the same method signatures in them and you explicitly state which interface the implementation is for. ... In Java you can only have one Paint() implementation.) (In C#, you can implement interfaces in both ways. In simple words the difference between “Implicit” interface and “Explicit” interface is that in implicit the interface methods are publicly implemented while in explicit the methods are privately implemented. - accessibility changed)
o overloading and overriding, e.g. method, operator (Overloading occurs when two or more methods in one class have the same method name but different parameters. Overriding means having two methods with the same method name and parameters (i.e., method signature). One of the methods is in the parent class and the other is in the child class.)
o predefined libraries, e.g. math, functions (random, pi, calculus) - this is common with all languages -
o modularity, e.g. reusability, portability (between platforms), plugability (Plug nPlay).
Factors affecting performance, safety and security
Elements that can affect performance, safety and security, such as:
• platform, e.g. desktop, web, distributed
• garbage collection, e.g. destroying unused objects (JVM does it auto-default, )
• interpreters, e.g. Java virtual machine, interpreted languages.
Java is compiled into bytecode which is then interpreted by the JVM which is on each device. Python is an interpreted language with which the interpreter translate each line of code for execution before translating the next.
Different output message when translating: Python: stops at the first error (syntax error)
Java: displays all errors in one go after the entire code is checked through.
Computational thinking in object-oriented programming
The mathematical and logical processes that underpin the design of object-oriented programs:
• algorithms, e.g. calculation, structured solution
• application of predefined functions, e.g. math libraries
• graphical user interface (GUI) resolutions and element position, e.g. x- and y-coordinates
• creation of 2D shapes, e.g. circle, rectangle
• use of programming logic and conditional statements
• use of Boolean algebra in conditional statements.
a of 22 years old. POS = keeperb of 21 years old. POS = defenderc of 23 years old. POS = defenderd of 19 years old. POS = defendere of 30 years old. POS = strikerf of 27 years old. POS = strikerg of 39 years old. POS = defenderh of 55 years old. POS = mid_fielderj of 55 years old. POS = mid_fielderk of 55 years old. POS = mid_fielderl of 55 years old. POS = mid_fielder-------------A of 22 years old. POS = keeperB of 21 years old. POS = defenderC of 23 years old. POS = defenderD of 19 years old. POS = defenderC of 30 years old. POS = strikerD of 27 years old. POS = strikerG of 69 years old. POS = defenderH of 69 years old. POS = mid_fielderJ of 69 years old. POS = mid_fielderK of 69 years old. POS = mid_fielderL of 69 years old. POS = mid_fielder-------------a of 22 years old. POS = keeperb of 21 years old. POS = defenderpublic class Football { public static void main(String args[]) { Keeper a = new Keeper("a", 22, 2.4, 0.95); Defender b =new Defender("b", 21, 0.90); Defender c =new Defender("c", 23, 0.90); Defender d =new Defender("d", 19, 0.90); Striker e =new Striker("e", 30, 0.90); Striker f =new Striker("f", 27, 0.90); Defender g = new Defender("g", 39, 0.8); Mid h = new Mid("h", 55, 0.8, 0.3); Mid j = new Mid("j", 55, 0.7, 0.1); Mid k = new Mid("k", 55, 0.3, 0.2); Mid l = new Mid("l", 55, 0.5, 0.9); Player[] teamA = new Player[]{a, b, c, d, e, f, g, h, j, k, l}; for (int cc = 0; cc<teamA.length; cc++){ System.out.println(teamA[cc].get_name() + " of " + teamA[cc].get_age() + " years old. POS = " + teamA[cc].get_pos()); } System.out.println("-------------"); Player[] teamB = new Player[11]; Keeper A = new Keeper("A", 22, 2.4, 0.95); Defender B =new Defender("B", 21, 0.90); Defender C =new Defender("C", 23, 0.90); Defender D =new Defender("D", 19, 0.90); Striker E =new Striker("C", 30, 0.90); Striker F =new Striker("D", 27, 0.90); Defender G =new Defender("G", 69, 0.90); Mid H =new Mid("H", 69, 0.90, 0.3); Mid J =new Mid("J", 69, 0.90, 0.4); Mid K =new Mid("K", 69, 0.90, 0.5); Mid L =new Mid("L", 69, 0.90,0.6); teamB[0] = A; teamB[1] = B; teamB[2] = C; teamB[3] = D; teamB[4] = E; teamB[5] = F; teamB[6] = G; teamB[7] = H; teamB[8] = J; teamB[9] = K; teamB[10] = L; for (int cc = 0; cc<teamB.length; cc++){ System.out.println(teamB[cc].get_name() + " of " + teamB[cc].get_age() + " years old. POS = " + teamB[cc].get_pos()); } System.out.println("-------------"); Player[] teamC; teamC = new Player[2]; teamC[0] = a; teamC[1] = b; for (int cc = 0; cc<2; cc++){ System.out.println(teamC[cc].get_name() + " of " + teamC[cc].get_age() + " years old. POS = " + teamC[cc].get_pos()); } }}class Player{ String name; int age;// String team;// String pos; // add this to BASECLASS as all players have this field! then public Player(String name, int age){ this.name =name; this.age = age; } void set_name(String name){ this.name = name; } void set_age(int age){ this.age = age; } String get_name(){ return this.name + " of no position!"; } int get_age(){ return this.age; } String get_pos(){ # new version change ??? the other classes does not need to implement this return ""; } }class Keeper extends Player{ String pos="keeper"; double height; double saves; public Keeper(String name, int age, double jump, double saves){ super(name, age); this.height = jump; this.saves = saves; } String get_name(){ return this.name; } int get_age(){ return this.age; } String get_pos(){ return this.pos; } double get_jump(){ return this.height; } double get_saves(){ return this.saves; } void set_saves(double saves){ this.saves = saves; } }class Defender extends Player{ String pos="defender"; double tackles; public Defender(String name, int age, double tackles){ super(name, age); this.tackles = tackles; } String get_name(){ return this.name; } int get_age(){ return this.age; } String get_pos(){ return this.pos; } double get_tackles(){ return this.tackles; } void set_tackles(double tkls){ this.tackles = tkls; }}class Striker extends Player{ String pos="striker"; double goals; public Striker(String name, int age, double goals){ super(name, age); this.goals = goals; } String get_name(){ return this.name; } int get_age(){ return this.age; } String get_pos(){ return this.pos; } double get_goals(){ return this.goals; } void set_goals(int gls){ this.goals = gls; }}class Mid extends Player{ String pos="mid_fielder"; double goals; double tackles; public Mid(String name, int age, double tackles, double goals){ super(name, age); this.goals = goals; this.tackles = tackles; } String get_name(){ return this.name; } int get_age(){ return this.age; } String get_pos(){ return this.pos; } double get_goals(){ return this.goals; } void set_goals(int gls){ this.goals = gls; } double get_tackles(){ return this.tackles; } }public class Football { public static void main(String args[]) { Keeper a = new Keeper("a", 22, "keeper", 2.4, 0.95); Defender b =new Defender("b", 21, "defender", 0.90); Defender c =new Defender("c", 23, "defender", 0.90); Defender d =new Defender("d", 19, "defender", 0.90); Striker e =new Striker("e", 30, "striker", 0.90); Striker f =new Striker("f", 27, "striker",0.90); Defender g = new Defender("g", 39, "defender", 0.8); Mid h = new Mid("h", 55, "mid-fielder", 0.8, 0.3); Mid j = new Mid("j", 55, "mid-fielder", 0.7, 0.1); Mid k = new Mid("k", 55, "mid-fielder", 0.3, 0.2); Mid l = new Mid("l", 55, "mid-fielder", 0.5, 0.9); Player[] teamA = new Player[]{a, b, c, d, e, f, g, h, j, k, l}; for (int cc = 0; cc<teamA.length; cc++){ System.out.println(teamA[cc].get_name() + " of " + teamA[cc].get_age() + " years old. POS = " + teamA[cc].get_pos()); } System.out.println("-------------"); Player[] teamB = new Player[11]; Keeper A = new Keeper("A", 22, "keeper", 2.4, 0.95); Defender B =new Defender("B", 21, "defender", 0.90); Defender C =new Defender("C", 23, "defender", 0.90); Defender D =new Defender("D", 19, "defender", 0.90); Striker E =new Striker("C", 30, "striker", 0.90); Striker F =new Striker("D", 27, "striker", 0.90); Defender G =new Defender("G", 69, "defender", 0.90); Mid H =new Mid("H", 69, "mid-fielder", 0.90, 0.3); Mid J =new Mid("J", 69, "mid-fielder", 0.90, 0.4); Mid K =new Mid("K", 69, "mid-fielder", 0.90, 0.5); Mid L =new Mid("L", 69, "mid-fielder", 0.90,0.6); teamB[0] = A; teamB[1] = B; teamB[2] = C; teamB[3] = D; teamB[4] = E; teamB[5] = F; teamB[6] = G; teamB[7] = H; teamB[8] = J; teamB[9] = K; teamB[10] = L; for (int cc = 0; cc<teamB.length; cc++){ System.out.println(teamB[cc].get_name() + " of " + teamB[cc].get_age() + " years old. POS = " + teamB[cc].get_pos()); } System.out.println("-------------"); Player[] teamC; teamC = new Player[2]; teamC[0] = a; teamC[1] = b; for (int cc = 0; cc<2; cc++){ System.out.println(teamC[cc].get_name() + " of " + teamC[cc].get_age() + " years old. POS = " + teamC[cc].get_pos()); } }}class Player{ String name; int age;// String team; String pos; public Player(String name, int age, String pos){ this.name =name; this.age = age; this.pos = pos; } void set_name(String name){ this.name = name; } void set_age(int age){ this.age = age; } String get_name(){ return this.name + " of no position!"; } int get_age(){ return this.age; } String get_pos(){ return this.pos; } }class Keeper extends Player{ double height; double saves; public Keeper(String name, int age, String pos, double jump, double saves){ super(name, age, pos); this.height = jump; this.saves = saves; } String get_name(){ return this.name; } int get_age(){ return this.age; }// String get_pos(){// return this.pos;// } double get_jump(){ return this.height; } double get_saves(){ return this.saves; } void set_saves(double saves){ this.saves = saves; } }class Defender extends Player{ double tackles; public Defender(String name, int age, String pos, double tackles){ super(name, age, pos); this.tackles = tackles; } String get_name(){ return this.name; } int get_age(){ return this.age; } //String get_pos(){ // return this.pos; //} double get_tackles(){ return this.tackles; } void set_tackles(double tkls){ this.tackles = tkls; }}class Striker extends Player{ double goals; public Striker(String name, int age, String pos, double goals){ super(name, age, pos); this.goals = goals; } String get_name(){ return this.name; } int get_age(){ return this.age; } String get_pos(){ return this.pos; } double get_goals(){ return this.goals; } void set_goals(int gls){ this.goals = gls; }}class Mid extends Player{ double goals; double tackles; public Mid(String name, int age, String pos, double tackles, double goals){ super(name, age, pos); this.goals = goals; this.tackles = tackles; } String get_name(){ return this.name; } int get_age(){ return this.age; } //String get_pos(){ // return this.pos;// } double get_goals(){ return this.goals; } void set_goals(int gls){ this.goals = gls; } double get_tackles(){ return this.tackles; } }