Java - object-oriented design Flashcards
object-oriented design
In an object oriented manner, rather than a sequence of steps, you would create different self-contained programs. objects. Each object has its own list of what it can do. the objects let you re-use code in different ways.
which languages are object-oriented
pretty much all of them
object
an instance of a class. inherits the behaviors and attributes of the class from which it was created
three main aspects of an object
identity, attributes, and behaviors
class
the template from which an object is created. when you create an object, you can instantiate it (create an instance of it) using parameters to initialize the object with specific values
method
actions that the object can do
function
a subroutine that might include certain actions or operations involving multiple objects
methods versus functions
Methods are defined as part of a class. Methods can access attributes from that class. But functions aren’t part of that class.
Instantiation
creating an instance of a particular class.
built-in class
Many classes are already defined in object-oriented languages: Strings, Dates, Collections, File I/O, Networking, etc.
Abstraction
“Data abstraction is the process of hiding certain details and showing only essential information to the user.
Focus on the essential qualities of something rather than one specific example. “Person” has a sense of an idea. You write one Person class with attributes (name, height, gender). If you don’t care about some of the attributes for your particular scenario, then you don’t need to define that in your object. To abstract something would be to look at all the objects and then abstract the original template from which these instances were created, or something.
literal definition of “abstract”: existing in thought or as an idea but not having a physical or concrete existence.
Encapsulation
protecting an attribute for the class. This helps you restrict access to some of the object’s components. Let’s say you don’t want anyone to change some aspect of a class. If you change a number or something, it might be an invalid number. The attribute is kept inside the object. You use a method to modify the value in the class. The object doesn’t make known the attributes of things.
Black box testing
closing off the inner workings of the class and only revealing the inputs and outputs. You don’t need to know the guts of how something actually works to use it. You know, every time you make an include with parameters, it’s like object-oriented design. Those parameters get passed into some code logic that runs, and a return output is generated. You don’t need to know the details of the include, for the most part. The logic of the include is encapsulated away.
You don’t want to worry about other arguments that might break the logic of the code.
It’s not about being secretive. It’s about reducing dependencies. You want to safeguard the case where changing one value ends up messing up code elsewhere.
Inheritance
Base a new object or class on an existing one. the new object or class inherits the existing attributes and methods of the class it inherits. The child classes can also have their own unique attributes and methods.
To determine whether inheritance is happening, use “is a kind of.” or “is a type of.” Inheritance describes an “is a kind of like” relationships. e.g., a cargo shuttle is a space ship.
Polymorphism
Polymorphism means “many forms”, and it occurs when we have many classes that are related to each other by inheritance.
Inheritance lets us inherit attributes and methods from another class. Polymorphism uses those methods to perform different tasks. This allows us to perform a single action in different ways.
For example, think of a superclass called Animal that has a method called animalSound(). Subclasses of Animals could be Pigs, Cats, Dogs, Birds - And they also have their own implementation of an animal sound (the pig oinks, and the cat meows, etc.):
Summary of polymorphism:
- defining different classes that can be used with the same interfaces
- using a method from the same class that can take different sets of parameters
superclass
There’s no limit to the number of child classes you can create based on the parent. Benefits from code reuse. If you make a change in a superclass, that change bubbles down to each of the subclasses. e.g., if you add an email attribute to Person, then all the other subclasses based on person will also inherit this.