Fundamentals of object oriented programming
Object oriented programming:-
object oriented programming is a methodology to design a program using classes and object.
As the name suggests, Object oriented programming or OOP's refers to 2 languages that uses object in programming
Object means a real-world entity such as computer chair pain etc.
The main aim of object oriented programming is to bind data han and methods together in a single location (object)
Object oriented programming increase flexibility and maintainability of program.
Object oriented concepts:- Classes and object
- Polymorphism
- Inheritance
- Encapsulation
- Abstraction
- Dynamic binding
1) classes and object in Java
- Classes:-
Collection of object is called as class.
Class is a logical entity, class does not consume any space in memory.
class represent the set of properties or methods that are common to all object of one type.
Constructors are used for initialising new object that are various types of classes that are used in real time application such as :
- Nested classes
- Anonymous classes
- Lambda expression
class declarations can include these components, in order:
Modifiers : A class can be public or has default access (Refer this for details).
Class name: The name should begin with a initial letter (capitalized by convention).
Superclass(if any): The name of the class’s parent (superclass), if any, preceded by the keyword extends. A class can only extend (subclass) one parent.
Interfaces(if any): A comma-separated list of interfaces implemented by the class, if any, preceded by the keyword implements. A class can implement more than one interface.
Body: The class body surrounded by braces, { }.
object
Object is a basic unit of object oriented programming
Object represents the real life entities.
An object can be defined as an instance of a class.
An object contain a address and take up some space in memory.
Object consists of :- State
- Behaviour
- Identity
State : It is represented by attributes of an object. It also reflects the properties of an object.
Behavior : It is represented by methods of an object. It also reflects the response of an object with other objects.
Identity : It gives a unique name to an object and enables one object to interact with other objects.
2) polymorphism
Polymorphism is the ability of a message to displayed in more than one form.
The word "poly" means many and " morphs" means forms.
polymorphism in java is a concept by which we can perform a single action in different ways.
we can perform polymorphism in Java by method overloading and method overriding.
There are two types of polymorphism in Java
- Compile time polymorphism
- Runtime polymorphism
3) inheritance
inheritance is the mechanism in Java by which one class is allowed to inherit the features of another class.
When one object acquires all the properties and behaviours of a parent object, it is known as inheritance.inheritance is used to achieve runtime polymorphism.
It provides the re is ability of code.
The parent class is called the base class or super class, the child class that extends the base class is called derived class or subclass or child class
- Super class: the class whose features are inherited is known as the superclass.
- Subclass: the class that inherits the other class known as subclass
- Reusability: a class has to write only the unique features & rest of the common properties and functionalities can be extended from the another class.
Syntax:
Class derived-class extends base-class
{
// Body/ method/ fields
}
4) encapsulation
Encapsulation is define as wrapping of of data under single unit.
Encapsulation is a protective shield the the prevent the data.
Technically in encapsulation, the variables or data of a class is hidden from any other class and can be accessed only through any member function of own class in which they are declared.
As in encapsulation, the data in a class is hidden from other classes, so it is also known as data-hiding.
Encapsulation can be achieved by Declaring all the variables in the class as private and writing public methods in the class to set and get the values of variables.
5) Abstraction:
Abstraction used for security reasons.
abstraction is a process of hiding the implementation details and showing only functionality to the user.
In java Abstraction is achieved by interfaces & abstract classes.
Abstract classes and Abstract methods
A abstract class is a class that is declared with abstract keyword.
Abstract class may or may not have all abstract methods some of them can be concrete methods.
There can be e no object of abstract class.
an abstract class parameterized constructor and defaults constructor is always present in an abstract class
An abstract method is a method that is declared without an implementation.
The Abstract method always declared using abstract keyword
Class that contain one or more abstract method must be declared using abstract keyword.
Abstract class also have static member methods.