0% found this document useful (0 votes)
6 views

Advance Programming copy

advanced programming

Uploaded by

mayank.7554s
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Advance Programming copy

advanced programming

Uploaded by

mayank.7554s
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 53

Advance Programming

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:

<Access_Modifier> class <Class_Name> {

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.

Team Collaboration: Modular code allows different developers to work on separate


modules simultaneously, facilitating teamwork.
Code Reuse
● Reusing code helps you avoid investing time and effort in redundantly developing
(and testing) the same thing over and over again.
● You may be able to reuse entire components across projects, boosting efficiency and
productivity.
● Maximizing code reuse is vital for scalability, so it’s a good idea to keep the following
tips in mind when you start working on a large application
Identifying Classes and Objects
Identifying Classes and Objects
Working with Objects
● Constructors
● Variable reassignment
● Instance variables
Java Constructor
A constructor in Java is a special method that is used to initialize objects. The constructor is called when an object of a class is created. It can be used to set initial values for
object attributes:

// Create a Main class

public class Main {

int x; // Create a class attribute

// Create a class constructor for the Main class

public Main() {

x = 5; // Set the initial value for the class attribute x

public static void main(String[] args) {

Main myObj = new Main(); // Create an object of class Main (This will call the constructor)

System.out.println(myObj.x); // Print the value of x

// 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

GFG() { System.out.println("Default constructor"); }

// Driver function

public static void main(String[] args)

GFG hello = new GFG();

}
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

Remember: Does constructor return any value?

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 {

// data members of the class.

String name;

int id;

// Parameterized Constructor

Geek(String name, int id)

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 {

public static void main(String[] args)

// This would invoke the parameterized constructor.

System.out.println("First Object");

Geek geek1 = new Geek("Avinash", 68);

System.out.println("GeekName :" + geek1.name


3. Copy Constructor in Java
+ " and GeekId :" + geek1.id);

System.out.println();

// This would invoke the copy constructor.

Geek geek2 = new Geek(geek1);

System.out.println(

"Copy Constructor used Second Object");

System.out.println("GeekName :" + geek2.name

+ " and GeekId :" + geek2.id);

}
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

A special form of A stronger form of


General relationship
Definition association with a “has-a” association with a
between two classes
relationship “part-of” relationship

Contained objects can Contained objects cannot


Classes are related but
Dependency exist independently of the exist without the container
can exist independently
container object object

Lifecycle Independent lifecycles Independent lifecycles Dependent lifecycles

Ownership No ownership implied Shared ownership Exclusive ownership

Strength Weak Moderate Strong


Difference between Association, Composition & Aggregation
Feature 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

Uses a reference to the Contains instances of the


Representation Uses a direct reference
contained object(s) contained object(s)

java class java class java class


Illustrative Example Teacher { private Library { private Car { private
Code Student student; List<Book> books; Engine engine;
} } }
Interface
Why use Java Interface?
Understand Interface
Relationship between Class and Interface
A class can extend another class similar to this an interface can extend another
interface. But only a class can extend to another interface, and vice-versa is not
allowed.
Difference between Class and Interface
Although Class and Interface seem the same there have certain differences between
Classes and Interface. The major differences between a class and an interface are
mentioned below:

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

You might also like