1/20
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
|---|
No study sessions yet.
OOP is a way to design software by modeling real-world entities as objects.
The software operates as a bunch of objects talking to each other. An object is a collection of data and the methods which operate on that data.
Main advantage: better manageable code.
Overall understanding of the software is increased.
Easier maintenance due to encapsulation. You can make changes to representations while keeping methods the same.
Useful for relatively big software
Code reusability
Easier to maintain and update
Provides better data security by restricting data access and avoiding unnecessary exposure.
Fast to implement & easy to redesign → minimizes the complexity of an overall program
The programmer should have good thinking in terms of objects as everything is treated as an object in OOP
Proper planning is required
A class is like a building block of OO Programs.
It is a user defined data type that contains the data members and member functions that operate on the data.
It is like a blueprint of of objects having common properties and methods
An object is an instance of a class.
Data members & methods of a class cannot be used directly.
We need to create an object (or instance) of the class to use them.
They are the actual entities that have a state and behavior.
Encapsulation
Data Abstraction
Polymorphism
Inheritance
The binding of data and methods that manipulate them into a single unit such that sensitive data is hidden from the users.
Data Hiding: Restricts access to members of an object.
Bundling of data and methods: the data members and methods that operate on an object are wrapped into a single unit known as a class.
The principle of hiding complex implementation details and showing only the essential features or functionality to the user, focusing on what an object does rather than how it does it
A class you cannot instantiate/cant create objects of this class.
Enforces and organizes exactly what every subclass of the abstract class MUST have.
You can however make subclasses of an abstract class that CAN be instantiated
Ex) public class Cat extends Animal: it makes sense to be able to instantiate a Cat object. But it doesn't make sense to instantiate an Animal object bc what kind of animal would it be? Therefore it would make sense to make the Parent Animal class an abstract class.
Abstract classes can contain abstract methods or can have regular methods too (these however do not NEED to be implemented in the subclasses)
Abstract method means the method has no body.
Ex) in the abstract animal class we have the abstract method makeNoise(). We don't want a body because every animal makes different noises. So now the child class must implement the abstract method using @Override and provide the body.
In an interface, all methods are assumed to be abstract.
You cannot instantiate objects using an interface
If a class implements an Interface, it must implement all of its methods using Override
Difference Between Abstract Class and interface (3)
Abstract classes can have abstract and non-abstract methods. In an interface, all methods are assumed to be abstract.
You can implement as many interfaces as you want but a class can only extend one abstract class. (You can extend a class and implement interfaces).
In interfaces, any declared fields are static and final. So they must be instantiated and can't be changed. So these will be applied to every class that implements the interface. (doesn't really make sense, which is why we have abstract classes). Abstract classes can have final, non-final, static and non-static variables.
When to create Abstract class vs Interface?
Create abstract class if you have a lot of closely related classes that you want to have the same functionality and same type of fields available
Create an interface if you have a lot of unrelated classes that you all want to do a certain thing. That makes it so that you can guarantee other types of classes will be able to “Poop” even if they aren't “animals”
It is the property of some code to behave differently for different contexts.
For example, defining multiple functions having the same name but working differently.
There are 2 types of polymorphism
Compile-Time Polymorphism
AKA static polymorphism or early binding
The binding of the call to its code is done at compile time.
Method overloading or operator overloading are examples of compile-time polymorphism
Runtime Polymorphism
AKA dynamic polymorphism or late binding
The actual implementation of the function is determined during runtime/execution.
Function overriding is an example of this method.
Keywords that are used to specify or control the accessibility of entities like classes or methods.
Public, private, and protected are examples
Overloading → compile time polymorphism: allows an entity to have numerous implementation of the same name
Overriding → runtime polymorphism: an entity with the same name but a different implementation is executed.
Single Inheritance: Child class derived directly from the base class
Multiple Inheritance (not supported in Java): Child class derived from multiple base classes
Multilevel Inheritance: Child class derived from a class which was also derived from a different base class
Hierarchical Inheritance: Multiple child classes derived from a single base class
Hybrid inheritance: Inheritance consisting of multiple inheritance types
A block of code that initializes a newly created object
Resembles an instance method but is not a method because it does not have a return type
Constructors can be overloaded in Java when we want constructors with different parameters.
Default Constructor: does not take any arguments. Automatically defined by the compiler when no constructor definition is provided
Non-parameterized Constructor: user-defined constructor with no args or parameters
Parameterized Constructor: A constructor that takes some args
Copy Constructor: initializes an object using another object of the same class.