0% found this document useful (0 votes)
14 views19 pages

Non Text Magic Studio Magic Design For Presentations L&P

Uploaded by

kdeepatripathy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views19 pages

Non Text Magic Studio Magic Design For Presentations L&P

Uploaded by

kdeepatripathy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 19

Java

Concepts
SUCHISMITA RAUTARAY-
230714100017 LAXMIPRIYA
MOHAPATRA-230714100027
KRUTIDEEPA TRIPATHY-
230714100050
Inheritan
ce
Inheritance, polymorphism,
and abstraction are key
principles.

Understand Polymorphism Explained


ing Java It allows classes to inherit properties
and behaviors effectively.
Concepts Abstraction insights
Abstraction simplifies complex code
structures
for
clarity.
INHERITANCE
Inheritance is a fundamental concept in OOP that allows one class to inherit the
properties and behavior of another class. The class that is being inherited from is called
the parent class or superclass, while the class that is doing the inheriting is called the
child class or subclass.

Why Use Inheritance?


Inheritance is useful when you want to create a new class that is a modified version of an
existing class. By inheriting from the existing class, you can reuse the code and properties of
the parent class, and then add new features or override existing ones in the child class.
TYPES OF
INHERITANCE
There are several types of inheritance, including:
Single Inheritance: A child class inherits from a single parent
class.
Multiple Inheritance: A child class inherits from multiple
parent
classes.
Multilevel Inheritance: A child class inherits from a parent
class, which in turn inherits from another parent class.
Hierarchical Inheritance: A parent class is inherited by
multiple child classes.
Hybrid Inheritance: A combination of multiple inheritance types.
RULES FOR
1.
IMPLEMENTING
Child can acess the properties and behaviour of patent
class but the vice versa is not possible.

2.In case of inheritance child class constructor must


acess the parent class constructor first otherwise
inheritance is not possible.

3.A parent class reference vartiable can hold the child


class object but vice-versa is possible. Even if the parent
reference variable holds
the child class object again ,parent can never access the
Benefits of Inheritance
Code Reusability: Inheritance allows you to reuse code from the parent class, reducing duplication
and improving maintainability.
Easier Maintenance: Inheritance makes it easier to modify and maintain code by providing a clear and
organized structure.
Improved Readability: Inheritance helps to improve the readability of code by providing a clear hierarchy
of classes and relationships between them.

Disadvantages of Inheritance
Tight Coupling: Inheritance can lead to tight coupling between classes, making it harder to modify
and maintain code.
Fragile Base Class Problem: Changes to the parent class can break the child class, making it harder
to maintain code.
Multiple Inheritance: Multiple inheritance can lead to the diamond problem, where a child class
inherits conflicting methods from multiple parent classes.
cod // Parent class
public class

e
Animal { public
void sound() {
System.out.println("The
animal makes a sound.");
}
}

// Child class
public class Dog
extends Animal {
@Override
public void
sound() {
System.out.println("
The dog barks.");
}
}

// Child class
public class Cat
extends Animal {
@Override
public void
sound() {
System.out.println("
The cat meows.");
}
}

// Using the child


classes public
class Main {
POLYMORPHISM
Polymorphism is a fundamental concept in object-oriented programming (OOP)
that allows for multiple forms of a single entity. In other words, polymorphism
enables an object to take on multiple forms, depending on the context in which it
is used.

Polymorphism is the ability of an object to behave in different ways, depending on


the situation. This can be achieved through method overriding or method
overloading.

Types of Polymorphism
There are two main types of polymorphism:

1.Method Overloading (Compile-time


METHOD
OVERLOADING
1. Method Overloading (Compile-time Polymorphism) Method overloading
occurs when multiple methods with the same name can be defined, but with
different parameter lists.
The method to be called is determined by the number and types of
parameters passed to it.

COD
E
public class Calculator {
public int add(int a, int b) { return a +
b; } public double add(double a, double b)
{ return a + b; }
public int add(int a, int b, int c)
{ return a + b + c; }
}
METHOD
OVERRIDING
2. Method Overriding (Run-time Polymorphism) Method overriding occurs when
a subclass provides a specific implementation of a method that is already
defined in its superclass. The
method to be called is determined by the type of object being referred to at
runtime.

CODE
public class Animal {
public void sound() { System.out.println("The animal makes a sound."); }
}

public class Dog extends Animal {


@Override
public void sound() { System.out.println("The dog barks."); }
}
public class Cat extends Animal {
@Override
REAL WORLD
EXAMPLES IN
POLYMORPHIS
M
Polymorphism is used in many real-world scenarios, such as:
A shape can be a circle, rectangle, or triangle, and each shape can have its own
implementation of methods like area() and perimeter().
A vehicle can be a car, truck, or motorcycle, and each vehicle can have its own
implementation of methods like accelerate() and brake().
A payment gateway can process different types of payments, such as credit card,
PayPal, or bank transfer, and each payment type can have its own implementation
of methods like processPayment().
In summary, polymorphism is a powerful concept in OOP that allows for multiple
forms of a single entity, enabling more flexibility, code reuse, and easier
maintenance.
ADVANTAGES OF
POLYMORPHISM
Polymorphism provides several benefits, including:
Increased flexibility: Polymorphism allows for more flexibility in programming, as
objects can behave in different ways depending on the situation.
Code reuse: Polymorphism enables code reuse, as a single method or class
can be used in multiple contexts.
Easier maintenance: Polymorphism makes it easier to maintain code, as changes
can be made at a single point without affecting other parts of the program.

DISADVANTAGES OF POLYMORPHISM
While polymorphism provides many benefits, it also has some disadvantages.
Here are some of the main drawbacks:
Increased complexity
Overriding vs. overloading
confusion
Runtime errors
Performance
ABSTRACTIO
N
Abstraction is a fundamental concept in object-
oriented programming (OOP) that allows us to show
only the necessary information to the outside world
while hiding the internal details. In Java, abstraction
is achieved through abstract classes and interfaces.

Abstraction is the process of exposing only the


necessary information to the outside world while
hiding the internal details. It helps to reduce
complexity and improve modularity in software
design.
KEY CONCEPTS OF
ABSTRACTION ARE
Here are the key concepts of abstraction:
1. Hiding Internal Details: Abstraction involves hiding the internal details of an object or system and
exposing only th
necessary information to the outside world.
2. Showing Only Necessary Information: Abstraction involves showing only the necessary information
to the outsid
world and hiding the internal details.
3. Modularity: Abstraction helps to improve modularity in software design by allowing us to break
down complex
systems into smaller, more manageable pieces.
4. Encapsulation: Abstraction is closely related to encapsulation, which involves bundling data and
methods that
operate on that data into a single unit.
5. Abstract Classes and Interfaces: Abstraction is achieved through the use of abstract classes and
interfaces in Java
6.Inheritance: Abstraction is also related to inheritance, which involves creating a new class based on
an existing clas
7. Polymorphism: Abstraction is also related to polymorphism, which involves the ability of an
object to take on
multiple forms.
8. Data Hiding: Abstraction involves hiding the internal data of an object or system and exposing
ABSTRACT CLASS AND
METHOD
An abstract class is a class that cannot be instantiated and is used to provide a
blueprint for other classes to follow. An abstract method is a method that is
declared in an abstract class and must be implemented by any non-abstract
subclass.
1 Abstract
Cannot Class
be instantiated
. Can have both abstract and non-abstract
2 methods Can have state (data members)
. Can have
3 constructors Can
.
have static methods
4
. Abstract
5 Method
.
1 Is declared in an abstract
. class Does not have an
2 implementation
. Must be implemented by any non-abstract
3 subclass Can be declared with or without
COD
E
public abstract class
Animal { public abstract
void sound();

public void eat()


{ System.out.println("The animal is
eating.");
}
}

public class Dog


extends Animal {
@Override
public void
sound() {
System.out.println(
"The dog barks.");
}
}

public class Cat


extends Animal {
@Override
public void
sound() {
System.out.println(
INTERFACE IN JAVA
An interface in Java is a abstract class that contains only abstract methods
and constants. It is used to define a contract that must be implemented
by any class that implements it.

1. Abstract: An interface is an abstract class that cannot be


instantiated.
2. Abstract methods: An interface can only contain abstract methods,
which are methods that are declared but not implemented.
3. Constants: An interface can contain constants, which are variables that
are
declared as final and static.
4.No state: An interface cannot have state, which means it cannot have
instance
variables.
COD
E
public interface
Animal { void
sound();
void eat();
}

public class Dog


implements Animal {
@Override
public void
sound() {
System.out.println(
"The dog barks.");
}

@Override
public void
eat() {
System.out.println("
The dog is eating.");
}
}

public class Cat


implements Animal {
@Override
public void
sound() {
System.out.println(
"The cat meows.");
}
Thank
you!

You might also like