Advance Programming copy
Advance Programming copy
Unit - 1
Object - Oriented Programming
As the name suggests, Object-Oriented Programming or OOPs refers to languages that use objects in
programming. Object-oriented programming aims to implement real-world entities like inheritance, hiding,
polymorphism, etc in programming. The main aim of OOP is to bind together the data and the functions that
operate on them so that no other part of the code can access this data except that function.
OOPs Concepts:
● Class
● Objects
● Data Abstraction
● Encapsulation
● Inheritance
● Polymorphism
● Dynamic Binding
● Message Passing
Examples of OOP
1. Polymorphism
2. Inheritance
3. Encapsulation
4. Abstraction
5. Class
6. Association etc.
cont.
Examples of OOP
1. Class and Object
● Class: A blueprint for creating objects. It defines properties (fields) and behaviors (methods).
● Object: An instance of a class.
Examples of OOP
2. Inheritance
Inheritance allows one class to inherit the properties and methods of another class.
Examples of OOP
3. Polymorphism
Polymorphism allows objects to be treated as instances of their parent class, allowing one
method to have different behaviors.
Examples of OOP
4. Encapsulation
Encapsulation is the practice of wrapping the data (variables) and the code (methods) together as a
single unit and restricting access to some of the object's components.
Examples of OOP
5. Abstraction
Abstraction involves hiding complex implementation details and showing only the necessary
features.
Advantages of Java Abstraction
Rules for Java Abstract Class
Data Encapsulation
1. Encapsulation in Java refers to integrating data (variables) and code (methods) into a single unit.
2. In encapsulation, a class's variables are hidden from other classes and can only be accessed by
the methods of the class in which they are found.
3. Encapsulation in Java is an object-oriented procedure of combining the data members and data
methods of the class inside the user-defined class. It is important to declare this class as private.
4. Syntax:
private <Data_Members>;
private <Data_Methods>;
}
Difference between Abstraction and Encapsulation
Modularity
Modularity in Java is a software design principle that involves dividing your code into
smaller, independent, and reusable parts known as modules. By implementing
modularity, you can break down a large project into smaller, more manageable pieces,
making the code easier to understand, maintain, and improve.
Benefits of Modularity
Organized Code: Modularity helps in structuring your code into distinct sections, making it
easier to read and manage.
Reusability: Once a module is created, it can be reused in other projects or parts of the
same project.
Easier Debugging: If an issue arises, you can easily pinpoint and fix the problem within the
specific module.
public Main() {
Main myObj = new Main(); // Create an object of class Main (This will call the constructor)
// Outputs 5
Types of Constructors in Java
Now is the correct time to discuss the types of the constructor, so primarily
there are three types of constructors in Java are mentioned below:
● Default Constructor
● Parameterized Constructor
● Copy Constructor
1. Default Constructor in Java
A constructor that has no parameters is known as default the constructor. A
default constructor is invisible. And if we write a constructor with no
arguments, the compiler does not create a default constructor. It is taken out.
It is being overloaded and called a parameterized constructor. The default
constructor changed into the parameterized constructor. But Parameterized
constructor can’t change the default constructor. The default constructor can
be implicit or explicit. If we don’t define explicitly, we get an implicit default
constructor. If we manually write a constructor, the implicit one is overridded.
1. Default Constructor in Java
Example:
// Java Program to demonstrate
// Default Constructor
import java.io.*;
// Driver class
class GFG {
// Default Constructor
// Driver function
}
1. Default Constructor in Java
Output
Default constructor
Note: Default constructor provides the default values to the object like 0, null, etc. depending on the
type.
2. Parameterized Constructor in Java
A constructor that has parameters is known as parameterized constructor. If we want to
initialize fields of the class with our own values, then use a parameterized constructor.
2. Parameterized Constructor in Java
Example:
// Java Program for Parameterized Constructor
import java.io.*;
class Geek {
// data members of the class.
String name;
int id;
Geek(String name, int id)
{
this.name = name;
this.id = id;
}
2. Parameterized Constructor in Java
}
class GFG {
public static void main(String[] args)
{
// This would invoke the parameterized constructor.
Geek geek1 = new Geek("Avinash", 68);
System.out.println("GeekName :" + geek1.name
+ " and GeekId :" + geek1.id);
}
}
2. Parameterized Constructor in Java
Output
GeekName :Avinash and GeekId :68
There are no “return value” statements in the constructor, but the constructor returns the current class
instance. We can write ‘return’ inside a constructor.
3. Copy Constructor in Java
Unlike other constructors copy constructor is passed with another object which copies the data available
from the passed object to the newly created object.
3. Copy Constructor in Java
Example:
// Java Program for Copy Constructor
import java.io.*;
class Geek {
String name;
int id;
// Parameterized Constructor
this.name = name;
this.id = id;
}
3. Copy Constructor in Java
// Copy Constructor
Geek(Geek obj2)
this.name = obj2.name;
this.id = obj2.id;
class GFG {
System.out.println("First Object");
System.out.println();
System.out.println(
}
Class relationship(Association, Composition, Aggregation)
In object-oriented programming, relationships between classes play a
crucial role in defining how objects interact with each other. Java, being an
object-oriented language, provides mechanisms to model these
relationships through association, aggregation, and composition.
Aggregation, association, and composition in Java describe how instances of
classes relate to each other.
Association
An association can be considered a generic term to indicate the relationship
between two independent classes; the relationship may be one-to-one,
one-to-many, or many-to-many, but it need not indicate ownership.
Aggregation
Aggregation is a specific form of association in which one class, the whole,
contains a collection of other classes, the parts; here, however, the lifecycle of
the parts does not depend upon the whole.
For example, a library and books show aggregation, since books may exist
somewhere apart from the library.
Composition
In contrast, composition is a stronger form of aggregation that means
ownership and lifecycle dependence; if the whole gets destroyed, then the
parts no longer exist.
For composition, a good example would be a house and its different rooms; a
room cannot exist without a house.
Difference between Association, Composition & Aggregation
Features Association Aggregation Composition
One-to-one, One-to-one,
one-to-many, one-to-many,
Cardinality One-to-one, one-to-many
many-to-one, many-to-one,
many-to-many many-to-many
Example Teacher and Student Library and Books Car and Engine
Class Interface
In an interface, you must initialize variables
In class, you can instantiate variables and
as they are final but you can’t create an
create an object.
object.
A class can contain concrete (with The interface cannot contain concrete (with
implementation) methods implementation) methods.
The access specifiers used with classes are In Interface only one specifier is used-
private, protected, and public. Public.
Example of Interface
Example of Interface
cont.,
Bank Example
cont.,
Multiple Inheritance in Java by Interface
Multiple Inheritance Example
Interface Inheritance
Inheritance in Java
Refer link :
https://www.simplilearn.com/tutorials/java-tutorial/inheritence-in-java
Advance Programming
Unit - 2
Polymorphism in Inheritance