Abstraction; Encapsulation;
Inheritance; Polymorphism;
Overriding
Abstraction in Java
• Abstraction in Java is a fundamental concept of object-oriented programming (OOP) that
enables developers to create simplified models of real-world entities. Abstraction is
achieved in Java through the use of abstract classes and interfaces, which provide a
blueprint for derived classes to follow.
What is Abstract Class?
• An abstract class in Java is a class that cannot be instantiated on its own but serves as a
blueprint for other classes. It allows you to define common methods, fields, and behavior
that can be shared by multiple subclasses.
• Abstract classes may contain both regular and abstract methods, where the latter are
declared but not implemented in the abstract class itself. Subclasses must implement
these abstract methods to provide their own implementation.
• An abstract class in Java provide a level of abstraction, allowing for code reuse and
promoting a hierarchical structure among related classes.
abstract class Shape {
private String color;
public Shape(String color) {
this.color = color;
}
public abstract double area(); // Abstract method
public void display() {
System.out.println("Color: " + color);
}
}
class Circle extends Shape {
private double radius;
public Circle(String color, double radius) {
super(color);
this.radius = radius; }
public double area() {
return Math.PI * radius * radius;
}
}
public class TutorialsAbstractClass {
public static void main(String[] args) {
Circle circle = new Circle("Red", 5.0);
circle.display();
System.out.println("Area: " + circle.area());
}
What is an Abstract Method?
• An abstract method in Java is a method declared in an abstract class or
interface that does not have an implementation in the class or interface
where it is declared. It serves as a placeholder for a method that must be
implemented in the subclasses or implementing classes.
• Abstract methods are meant to be overridden by the subclasses to
provide their own implementation. They define the contract or behavior
that the subclasses must adhere to.
• An abstract method in Java is denoted by the "abstract" keyword and
does not have a body, only the method signature.
abstract class Vehicle {
public abstract void start(); // Abstract method
}
class Car extends Vehicle {
public void start() {
System.out.println("Car started");
}
}
public class TutorialsAbstractMethod {
public static void main(String[] args) {
Car car = new Car();
car.start();
}
}
What are Interfaces in Java?
• The interface in Java is a mechanism to achieve abstraction. Traditionally,
an interface could only have abstract methods (methods without a body)
and public, static, and final variables by default. It is used to achieve
abstraction and multiple inheritances in Java. In other words, interfaces
primarily define methods that other classes must implement. Java
Interface also represents the IS-A relationship.
• In Java, the abstract keyword applies only to classes and methods,
indicating that they cannot be instantiated directly and must be
implemented.
• When we decide on a type of entity by its behavior and not via attribute
we should define it as an interface
Syntax for Java Interfaces
interface interfacename{
// declare constant fields
// declare methods that abstract
// by default.
}
interface Polygon {
void getArea(int length, int breadth);
}
// implement the Polygon interface
class Rectangle implements Polygon {
// implementation of abstract method
public void getArea(int length, int breadth) {
System.out.println("The area of the rectangle is " + (length * breadth));
}
}
class Main {
public static void main(String[] args) {
Rectangle r1 = new Rectangle();
r1.getArea(5, 6);
}
}
Output
The area of the rectangle is 30
Why do Classes Have to Implement Interfaces?
• Classes in Java implement interfaces to fulfill a specific set of requirements.
By implementing an interface, a class promises to provide certain methods
or behavior defined by the interface.
• This allows objects of that class to be treated as instances of the interface,
promoting code organization and reusability and ensuring consistent
behavior across different classes.
Which Classes Should Implement Interfaces?
• In Java, any class can implement an interface. Implementing an interface is
a way to ensure that a class adheres to a contract and provides the
necessary methods and behavior defined by the interface
When to Use Abstract Classes and Abstract Methods?
• Abstract classes are used when you want to define a common blueprint for a
group of related classes. They allow you to share attributes and methods among
subclasses and enforce a structure or hierarchy. Abstract methods are used when
you want to define a method in an abstract class or interface without
implementing it.
Why do we Use Abstraction?
• Abstraction in Java, specifically abstract classes and abstract methods, are used to
create a blueprint for related classes and enforce common behavior. Abstract
classes provide a way to share attributes and methods among subclasses, while
abstract methods ensure that subclasses provide their own implementation.
• Abstraction promotes code reusability, modularity, and contract adherence,
allowing for more flexible and maintainable code.
Polymorphism
Characteristics/Features of Polymorphism
Following are the significant characteristics of Polymorphism in Java:
The functionality of a method behaves differently in different scenarios.
The behavior of a method depends on the data provided.
It allows the same name for a member or method in a class with different
types.
Polymorphism supports implicit type conversion.
Types of Polymorphism
Compile-Time Polymorphism
A typical Java program encounters Compile-Time Polymorphism during the
compilation stage. Here, the overloading method resolution takes place in the
compilation stage.
Method Overloading
Method Overloading is the process in which the class has two or more
methods with the same name. Nevertheless, the implementation of a specific
method occurs according to the number of parameters in the method call.
Example:
//Method Overloading.
package polymorphism;
public class Addition {
public int add(int x, int y) {
return (x + y);
}public double add(double d,
double e, double f, double g) {
return (d + e + f + g);
}public double add(double a,
double b) {
return (a + b);
}
public static void main(String args[]) {
Addition a = new Addition();
System.out.println(a.add(25, 30));
System.out.println(a.add(10.0, 15.0, 20.0,
25.0));
System.out.println(a.add(127.5, 123.5));
}
}
//Output:
55
70.0
251.0
Run-Time Polymorphism
Run-Time Polymorphism is a procedure where the program execution takes place during Run-Time. Here, the
resolution of an overriding happens in the execution stage.
Method Overriding
Method Overriding is a procedure in which the compiler can allow a child class to implement a specific method
already provided in the parent class.
Example:
//Method Overriding.
package polymorphism;
class CargoPilot {
public void FlyPlane() {
System.out.println("This is the Cargo Pilot, Ready to Take
off");
}
}
class CivilianPilot extends CargoPilot {
public void FlyPlane() {
System.out.println("This is the Civilian Pilot, Ready to
Takeoff");
}
}
public class Takeoff {
public static void main(String args[]) {
CargoPilot CPObj = new CargoPilot();
CPObj.FlyPlane();
}
}
//Output:
This is the Cargo Pilot, Ready to Takeoff
In Java, "extends" and "tagging" are related to inheritance and interfaces, respectively.
Extends:
•The extends keyword is used for inheritance, where a new class (subclass) inherits
properties and methods from an existing class (superclass).
•This creates an "is-a" relationship, meaning the subclass is a type of the superclass.
•For example, if Car extends Vehicle, then a Car is a type of Vehicle.
•A class can only extend one other class.
Tagging:
•Tagging refers to using interfaces to mark or categorize classes.
•A tagging interface is an interface with no methods, used solely to identify a class as
belonging to a specific type.
•A class can implement multiple interfaces, allowing it to be tagged with multiple types.
•For example, the Serializable interface is a tagging interface that marks a class as
serializable (can be converted to a byte stream).
Java Packages
A package in Java is used to group related classes.
Packages are divided into two categories:
Built-in Packages (packages from the Java API)
User-defined Packages (create your own packages)
Built-in Packages
These packages consist of a large number of classes which are a part of Java API.Some of the commonly used
built-in packages are:
1.java.lang: Contains language support classes(e.g classes which defines primitive data types, math operations).
This package is automatically imported.
2. java.io: Contains classes for supporting input / output operations.
3. java.util: Contains utility classes which implement data structures like Linked List, Dictionary and support ;
for Date / Time operations.
4. java.applet: Contains classes for creating Applets.
5. java.awt: Contain classes for implementing the components for graphical user interfaces (like button , ;menus
etc). 6)
6. java.net: Contain classes for supporting networking operations.
User-Defined Packages
package com.example;
public class Calculator {
public int add(int a, int b) {
return a + b;
}
public int subtract(int a, int b) {
return a - b;
}
public int multiply(int a, int b) {
return a * b;
}
public int divide(int a, int b) {
if (b != 0) {
return a / b;
} else {
throw new ArithmeticException("Cannot divide by zero!");
}
}
}
import com.example.Calculator;
public class PackageExample {
public static void main(String[] args) {
Calculator calculator = new Calculator();
int result = calculator.add(5, 3);
System.out.println("Addition: " + result);
result = calculator.subtract(5, 3);
System.out.println("Subtraction: " + result);
result = calculator.multiply(5, 3);
System.out.println("Multiplication: " + result);
result = calculator.divide(10, 2);
System.out.println("Division: " + result);
}
}
Output:
Addition: 8
Subtraction: 2
Multiplication: 15
Division: 5

Abstraction encapsulation inheritance polymorphism

  • 1.
  • 2.
    Abstraction in Java •Abstraction in Java is a fundamental concept of object-oriented programming (OOP) that enables developers to create simplified models of real-world entities. Abstraction is achieved in Java through the use of abstract classes and interfaces, which provide a blueprint for derived classes to follow. What is Abstract Class? • An abstract class in Java is a class that cannot be instantiated on its own but serves as a blueprint for other classes. It allows you to define common methods, fields, and behavior that can be shared by multiple subclasses. • Abstract classes may contain both regular and abstract methods, where the latter are declared but not implemented in the abstract class itself. Subclasses must implement these abstract methods to provide their own implementation. • An abstract class in Java provide a level of abstraction, allowing for code reuse and promoting a hierarchical structure among related classes.
  • 3.
    abstract class Shape{ private String color; public Shape(String color) { this.color = color; } public abstract double area(); // Abstract method public void display() { System.out.println("Color: " + color); } } class Circle extends Shape { private double radius; public Circle(String color, double radius) { super(color); this.radius = radius; } public double area() { return Math.PI * radius * radius; } } public class TutorialsAbstractClass { public static void main(String[] args) { Circle circle = new Circle("Red", 5.0); circle.display(); System.out.println("Area: " + circle.area()); }
  • 4.
    What is anAbstract Method? • An abstract method in Java is a method declared in an abstract class or interface that does not have an implementation in the class or interface where it is declared. It serves as a placeholder for a method that must be implemented in the subclasses or implementing classes. • Abstract methods are meant to be overridden by the subclasses to provide their own implementation. They define the contract or behavior that the subclasses must adhere to. • An abstract method in Java is denoted by the "abstract" keyword and does not have a body, only the method signature.
  • 5.
    abstract class Vehicle{ public abstract void start(); // Abstract method } class Car extends Vehicle { public void start() { System.out.println("Car started"); } } public class TutorialsAbstractMethod { public static void main(String[] args) { Car car = new Car(); car.start(); } }
  • 6.
    What are Interfacesin Java? • The interface in Java is a mechanism to achieve abstraction. Traditionally, an interface could only have abstract methods (methods without a body) and public, static, and final variables by default. It is used to achieve abstraction and multiple inheritances in Java. In other words, interfaces primarily define methods that other classes must implement. Java Interface also represents the IS-A relationship. • In Java, the abstract keyword applies only to classes and methods, indicating that they cannot be instantiated directly and must be implemented. • When we decide on a type of entity by its behavior and not via attribute we should define it as an interface
  • 7.
    Syntax for JavaInterfaces interface interfacename{ // declare constant fields // declare methods that abstract // by default. }
  • 8.
    interface Polygon { voidgetArea(int length, int breadth); } // implement the Polygon interface class Rectangle implements Polygon { // implementation of abstract method public void getArea(int length, int breadth) { System.out.println("The area of the rectangle is " + (length * breadth)); } } class Main { public static void main(String[] args) { Rectangle r1 = new Rectangle(); r1.getArea(5, 6); } } Output The area of the rectangle is 30
  • 9.
    Why do ClassesHave to Implement Interfaces? • Classes in Java implement interfaces to fulfill a specific set of requirements. By implementing an interface, a class promises to provide certain methods or behavior defined by the interface. • This allows objects of that class to be treated as instances of the interface, promoting code organization and reusability and ensuring consistent behavior across different classes. Which Classes Should Implement Interfaces? • In Java, any class can implement an interface. Implementing an interface is a way to ensure that a class adheres to a contract and provides the necessary methods and behavior defined by the interface
  • 10.
    When to UseAbstract Classes and Abstract Methods? • Abstract classes are used when you want to define a common blueprint for a group of related classes. They allow you to share attributes and methods among subclasses and enforce a structure or hierarchy. Abstract methods are used when you want to define a method in an abstract class or interface without implementing it. Why do we Use Abstraction? • Abstraction in Java, specifically abstract classes and abstract methods, are used to create a blueprint for related classes and enforce common behavior. Abstract classes provide a way to share attributes and methods among subclasses, while abstract methods ensure that subclasses provide their own implementation. • Abstraction promotes code reusability, modularity, and contract adherence, allowing for more flexible and maintainable code.
  • 11.
  • 12.
    Characteristics/Features of Polymorphism Followingare the significant characteristics of Polymorphism in Java: The functionality of a method behaves differently in different scenarios. The behavior of a method depends on the data provided. It allows the same name for a member or method in a class with different types. Polymorphism supports implicit type conversion. Types of Polymorphism
  • 13.
    Compile-Time Polymorphism A typicalJava program encounters Compile-Time Polymorphism during the compilation stage. Here, the overloading method resolution takes place in the compilation stage. Method Overloading Method Overloading is the process in which the class has two or more methods with the same name. Nevertheless, the implementation of a specific method occurs according to the number of parameters in the method call.
  • 14.
    Example: //Method Overloading. package polymorphism; publicclass Addition { public int add(int x, int y) { return (x + y); }public double add(double d, double e, double f, double g) { return (d + e + f + g); }public double add(double a, double b) { return (a + b); } public static void main(String args[]) { Addition a = new Addition(); System.out.println(a.add(25, 30)); System.out.println(a.add(10.0, 15.0, 20.0, 25.0)); System.out.println(a.add(127.5, 123.5)); } } //Output: 55 70.0 251.0
  • 15.
    Run-Time Polymorphism Run-Time Polymorphismis a procedure where the program execution takes place during Run-Time. Here, the resolution of an overriding happens in the execution stage. Method Overriding Method Overriding is a procedure in which the compiler can allow a child class to implement a specific method already provided in the parent class. Example: //Method Overriding. package polymorphism; class CargoPilot { public void FlyPlane() { System.out.println("This is the Cargo Pilot, Ready to Take off"); } } class CivilianPilot extends CargoPilot { public void FlyPlane() { System.out.println("This is the Civilian Pilot, Ready to Takeoff"); } } public class Takeoff { public static void main(String args[]) { CargoPilot CPObj = new CargoPilot(); CPObj.FlyPlane(); } } //Output: This is the Cargo Pilot, Ready to Takeoff
  • 16.
    In Java, "extends"and "tagging" are related to inheritance and interfaces, respectively. Extends: •The extends keyword is used for inheritance, where a new class (subclass) inherits properties and methods from an existing class (superclass). •This creates an "is-a" relationship, meaning the subclass is a type of the superclass. •For example, if Car extends Vehicle, then a Car is a type of Vehicle. •A class can only extend one other class. Tagging: •Tagging refers to using interfaces to mark or categorize classes. •A tagging interface is an interface with no methods, used solely to identify a class as belonging to a specific type. •A class can implement multiple interfaces, allowing it to be tagged with multiple types. •For example, the Serializable interface is a tagging interface that marks a class as serializable (can be converted to a byte stream).
  • 17.
    Java Packages A packagein Java is used to group related classes. Packages are divided into two categories: Built-in Packages (packages from the Java API) User-defined Packages (create your own packages) Built-in Packages These packages consist of a large number of classes which are a part of Java API.Some of the commonly used built-in packages are: 1.java.lang: Contains language support classes(e.g classes which defines primitive data types, math operations). This package is automatically imported. 2. java.io: Contains classes for supporting input / output operations. 3. java.util: Contains utility classes which implement data structures like Linked List, Dictionary and support ; for Date / Time operations. 4. java.applet: Contains classes for creating Applets. 5. java.awt: Contain classes for implementing the components for graphical user interfaces (like button , ;menus etc). 6) 6. java.net: Contain classes for supporting networking operations.
  • 18.
    User-Defined Packages package com.example; publicclass Calculator { public int add(int a, int b) { return a + b; } public int subtract(int a, int b) { return a - b; } public int multiply(int a, int b) { return a * b; } public int divide(int a, int b) { if (b != 0) { return a / b; } else { throw new ArithmeticException("Cannot divide by zero!"); } } }
  • 19.
    import com.example.Calculator; public classPackageExample { public static void main(String[] args) { Calculator calculator = new Calculator(); int result = calculator.add(5, 3); System.out.println("Addition: " + result); result = calculator.subtract(5, 3); System.out.println("Subtraction: " + result); result = calculator.multiply(5, 3); System.out.println("Multiplication: " + result); result = calculator.divide(10, 2); System.out.println("Division: " + result); } } Output: Addition: 8 Subtraction: 2 Multiplication: 15 Division: 5