0% found this document useful (0 votes)
9 views2 pages

Java Python OOP

The document provides a comprehensive guide to Object-Oriented Programming (OOP) concepts in Java and Python, focusing on method overriding and operator overloading. In Java, method overriding is demonstrated with an example of a Dog class extending an Animal class, while Python showcases both method overriding and operator overloading using magic methods. The document highlights that Java does not support user-defined operator overloading, unlike Python.

Uploaded by

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

Java Python OOP

The document provides a comprehensive guide to Object-Oriented Programming (OOP) concepts in Java and Python, focusing on method overriding and operator overloading. In Java, method overriding is demonstrated with an example of a Dog class extending an Animal class, while Python showcases both method overriding and operator overloading using magic methods. The document highlights that Java does not support user-defined operator overloading, unlike Python.

Uploaded by

Yuvajit Boruah
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

OOP in Java & Python

A Comprehensive Guide to Polymorphism & OOP Concepts

1. Object-Oriented Programming in Java

Method Overriding

In Java, overriding is used to provide the specific implementation of a method which is


already provided by its superclass. It is performed at runtime.

class Animal {
void sound() { [Link]("Animal makes a sound"); }
}

class Dog extends Animal {


@Override
void sound() { [Link]("Dog barks"); }
}

Note: Java does NOT support user-defined Operator Overloading (except for the + operator
with Strings).

2. Object-Oriented Programming in Python

Method Overriding

Python naturally supports overriding. If a method is defined in the subclass with the same
name as in the superclass, the subclass method replaces it.

class Parent:
def greet(self):
print("Hello from Parent")

class Child(Parent):
def greet(self):
print("Hello from Child")

Operator Overloading in Python

Python supports operator overloading through special "magic methods" or "dunder


methods" (e.g., __add__ , __sub__ ).

class Point:
def __init__(self, x):
self.x = x
def __add__(self, other):
return Point(self.x + other.x)

You might also like