Object-Oriented Programming Flashcards
Object-Oriented Programming
a methodology or paradigm to design a program using classes and objects. It simplifies the software development and maintenance by providing some concepts:
Object
Classes
Inheritance
Polymorphism
Abstraction
Encapsulation
Object
Any entity that has state and behavior is known as an object. For example a chair, pen, table, keyboard, bike, etc. It can be physical or logical.
An Object can be defined as an instance of a class. An object contains an address and takes up some space in memory. Objects can communicate without knowing the details of each other’s data or code. The only necessary thing is the type of message accepted and the type of response returned by the objects.
Example: A dog is an object because it has states like color, name, breed, etc. as well as behaviors like wagging the tail, barking, eating, etc.
Class
A class, in the context of Java, are templates that are used to create objects, and to define object data types and methods. Core properties include the data types and methods that may be used by the object
A class can also be defined as a blueprint from which you can create an individual object. Class doesn’t consume any space.
Advantages of OOPs over Procedure Oriented Programming
OOPs makes development and maintenance easier whereas in a procedure-oriented programming language it is not easy to manage if code grows as project size increases.
OOPs provides data hiding whereas in a procedure-oriented programming language a global data can be accessed from anywhere.
Constructor
In Java, a constructor is a block of codes similar to the method. It is called when an instance of the object is created, and memory is allocated for the object.
It is a special type of method which is used to initialize the object.
When is a constructor called?
When an object is created, compiler makes sure that constructors for all of its subobjects (its member and inherited objects) are called. If members have default constructors or constructor without parameter then these constructors are called automatically, otherwise parameterized constructors can be called using initializer list.
Note: It is called constructor because it constructs the values at the time of object creation. It is not necessary to write a constructor for a class. It is because java compiler creates a default constructor if your class doesn’t have any.
Rules to remember while creating a constructor
There are three rules defined for the constructor.
Constructor name must be the same as its class name
A Constructor must have no explicit return type
A Java constructor cannot be abstract, static, final, and synchronized
Types of constructors
Default Constructor
Parameterized Constructor
Object Oriented Programming Pillars
Inheritance
Polymorphism
Encapsulation
Abstraction
Inheritance
Inheritance in Java is a mechanism in which one object acquires all the properties and behaviors of a parent object. It is an important part of OOPs (Object Oriented programming system).
The idea behind inheritance in Java is that you can create new classes that are built upon existing classes. When you inherit from an existing class, you can reuse methods and fields of the parent class. Moreover, you can add new methods and fields in your current class also.
Inheritance represents the IS-A relationship which is also known as a parent-child relationship.
Advantages of Inheritance
For Method Overriding (so runtime polymorphism can be achieved).
For Code Reusability.
Class
A class is a group of objects which have common properties. It is a template or blueprint from which objects are created. Term used in inheritance.
Subclass/ child class
Subclass is a class which inherits the other class. It is also called a derived class, extended class, or child class. Term used in inheritance.
Super Class/Parent Class:
Superclass is the class from where a subclass inherits the features. It is also called a base class or a parent class. Term used in inheritance.
Reusability
As the name specifies, reusability is a mechanism which facilitates you to reuse the fields and methods of the existing class when you create a new class. You can use the same fields and methods already defined in the previous class. Term used in inheritance.