Oop intro, Encapsulation, Inheritance and Polymorphism(1.2.4 e) Flashcards
What is a paradigm?
a way of thinking
a change of paradigm is a change in the way you think
Programming languages in paradigms
different programming languages live in different paradigms some live in multiple
What are classes?
templates that define the behaviour
What are objects?
manifestations (instance) of a class
What are the two main aspects of classes?
attributes
methods
What are attributes?
what data is stored and how it is stored (similar to variables)
What are methods?
how the class behaves (similar to subroutines)
What should you do before coding a class when designing it?
draw a diagram
What does private attribute/method mean?
that the given attribute or method cannot be accessed from outside the class definition
What does public attribute/method mean?
that the given attribute or method can be accessed from outside
What are getters?
retrieves the data from an attribute and returns it to the wider program
depending on the required behaviour can modify the output to match what the wider program needs
What are setters?
takes in a value from the wider program and modifies the relevant attributes accordingly
can include validation within the method meaning every time the attribute is modified the value is validated
Defining getters and setters
read only attribute only a get method is provided
are some basic methods we add to any class definition to enable access to attribute (as/when we need to)
generally good idea to add validation to prevent attributes being messed with improperly
What is encapsulation?
bundling data and methods together so the direct access to the data is unnecessary
What does encapsulation entail?
restricts access to attributes (declared private)
only way to access the attributes is through the methods (declared public)
Benefits of encapsulation
enforces data integrity
complex behaviour (e.g fancy validation) can be abstracted away and reused at will
increase program security
Drawbacks of encapsulation
lots of boiler code
requires lengthy design work and planning (difficult to prototype functional oop)
What is inheritance?
where we create a subclass of a class
Parent/super class and child/sub class
parent/super class is the original class
child/sub class is the new class
child class inherit all the attributes and methods from the parent class
child class can have additional attributes and/or methods of its own
saves time as methods and attributes don’t need rewritten
What is dynamic polymorphism?
where a method is defined in both the parent and child classes with the same name but different functionality
What is static polymorphism?
wherre a method had different functionality depending upon the parameters passed to it
Why polymorphism?
allows for programmers to use sensible self-documenting method names while having the flexibility to redefine these methods to better suit a child class
Polymorphism super
can call methods from superclass using keyword super
this allows us to use methods from both superclass and the child class
Polymorphism constructor
using the superclass method can be benificial especially when defining constructor of subclass
by invoking constructor of superclass and amending it we can reduce duplicate code and remove potential future maitenance issues
child/sub class method overridess parent/super class methods