Focused more for interview Perspective and Easy Understanding
Object-Oriented Programming (OOP) is a software development paradigm that suggests developers to split a program in building blocks known as objects.
The OOP paradigm allows developers to define the object's data, functions, and its relationship with other objects.
Code Reusability
Enables to model real world object
Easy Modification
Easy Maintenance
More Flexible
Defines an Abstract thing of a thing (i.e. Object)
Explaining its Behavior
A user-defined data structure that groups properties and methods.
Class doesn’t occupies memory.
Class is a definition
Abstract classes are classes that can have no instances
A Single entity Which holds Data and Method.
It’s Structure.
Should be understandable by a non- Programmer
DOG (Class) Color, Shape ( Characteristic ) Bark, Pee, Run ( Behaviors ) Members Abstract characteristics of a thing Fields - _Color Properties - Color Fields - _Color Properties - Color
Specify Class Name
Declare Data members
Declare Methods
Define the Processing
Program them in the following order:
Namespace: The namespace is a keyword that defines a distinctive name or last name for the class. A namespace categorizes and organizes the library (assembly) where the class belongs and avoids collisions with classes that share the same name.
Class declaration: Line of code where the class name and type are defined.
Fields: Set of variables declared in a class block.
Constants: Set of constants declared in a class block.
Constructors: A method or group of methods that contains code to initialize the class.
Properties: The set of descriptive data of an object.
Events: Program responses that get fired after a user or application action.
Methods: Set of functions of the class.
Destructor: A method that is called when the class is destroyed.
In managed code, the Garbage Collector is in charge of destroying objects; however, in some cases developers need to take extra actions when objects are being released, such as freeing handles or de-allocating unmanaged objects.
In .NET, there is no concept of deterministic destructors. The Garbage Collector will call the Finalize() method at a non-deterministic time while reclaiming memory for the application.
Access keywords
Access keywords define the access to class members from the same class and from other classes. The most common access keywords are:
Public: Allows access to the class member from any other class.
Private: Allows access to the class member only in the same class.
Protected: Allows access to the class member only within the same class and from inherited classes.
Internal: Allows access to the class member only in the same assembly.
Protected internal: Allows access to the class member only within the same class, from inherited classes, and other classes in the same assembly. Static: Indicates that the member can be called without first instantiating the class.
A particular Instance of a Class
Objects are the building blocks of OOP
Represent real world things in an abstract way
Commonly defined as variables or data structures that encapsulate behavior and data in a programmed unit
The objects will all act as independent units in their own right, and they will be responsible for carrying out a certain process
Object identity:
Every object is unique and can be differentiated from other objects. Each time and object is created (instantiated) the object identity is defined.
Object behavior:
What the object can do. In OOP, methods work as functions that define the set of actions that the object can do.
Objec t state:
The data stored within the object at any given moment. In OOP, fields, constants, and properties define the state of an object.
Inheritance
Abstraction
Polymorphism
Encapsulation
Event
Is like inheriting the abstract characteristics from other class - It is a kind of hierarchy
Base Class will be inherited to derived class
is a way to form new classes using classes that have already been defined
is-a relationships represent a hierarchy between classes of objects
Use:
Reuse of Code – uses the attributes of the base class
Extensibility – Has it’s own Attributes
Simple / Single
Multilevel
Hierarchical
Multiple
Hybrid
Superclass: Parent class
Subclass: Child class
Base class: Parent class
Derived class: Child class
Is like inheriting from more than one base class.
.Net do not allow. But using Multiple interface inheritance this can be achieved.
Information Hiding
Visibility to the methods and not to the fields
E.g. A Report designer for a programmer
Packs the data and Method and protects from external tampering
Exposing only the details that are relevant: the public interface
Improves Security
Protected cName as string Protected Function ChangeName(NewName) Me.cName = NewName End Function
As we cannot hide entire object we give access to some specific part of data.
Conceptual boundaries of an object
Focuses on the essential characteristics of an object
a named entity made up of selected attributes and behavior specific to a particular usage of the originating entity
Improves Security
For example see notes below
One entity existing in multiple forms
Behavior depends on the type of data
Same message to diff objects results in different behavior
Linked to inheritance and assures that derived class to override the implementation of parents methods.
Class Employee Function PayEmployee() PayEmployee = Hours * HourlyRate End Function
Class CommissionedEmployee Inherits Employee Overrides Function PayEmployee() PayEmployee = BasePay + Commissions End Function
Upcasting
Casting an object of any type to another type which is above it in the inheritance hierarchy.
Downcasting
Casting an object of any type to another type which is below it in the inheritance hierarchy.
Example
Sealed Class
When applied to a class, the sealed modifier prevents other classes from inheriting from it.
static classes and static methods are used to create data and functions that can be accessed without creating an instance of the class.
class CompanyInfo { public string GetCompanyName() { return "CompanyName"; } public string GetComAddress() { return "ComAddress"; }
These methods do not need to be attached to a specific instance of the class. Therefore, instead of creating unnecessary instances of this class, you can declare it as a static class, like this:
static class CompanyInfo { public static string GetCompanyName() {return "CompanyName";} public static string GetComAddress() {return "ComAddress";}
0 comments
Post a comment