Presentation 2
Presentation 2
polymorphism
objects
- CH . SREEJA
- 23B81A62B3
What is the super Keyword in
Java?
The super keyword is a reference used in Java to refer to the immediate parent class of
a subclass. It plays a crucial role in object-oriented programming, especially in
inheritance.
Purposes of the super Keyword:
1.Access Parent Class Members:
•It allows a subclass to access methods and variables defined in its parent class, especially
when those members are overridden in the subclass.
2.Calling Parent Class Constructor:
•It can be used to invoke the constructor of the parent class, ensuring that the parent class's
initialization logic is executed when creating an instance of the subclass.
3.Resolving Name Conflicts:
•When a subclass has fields or methods with the same name as those in its parent class,
super helps differentiate between the subclass and parent class members, allowing
access to the parent’s version.
EXAMPLE
class Parent { void display() {
int value = 10; Parent() super.display(); // Calls Parent’s method
{ System.out.println("Parent Constructor"); }
System.out.println("Child value: " +
void display() { value); }
System.out.println("Parent value: " + value);
}
}
} public class Main { public static void
class Child extends Parent { main(String[] args) {
int value = 20; Child() { Child obj = new Child();
super(); // Calls Parent's constructor obj.display();
System.out.println("Child Constructor");
}
}
}
What is Polymorphism in Java?
• Polymorphism is a core concept in object-oriented programming that allows
objects to be treated as instances of their parent class. The term means
"many shapes" and it enables one interface to be used for a general class of
actions. The specific action is determined by the exact nature of the situation.
• Types of Polymorphism:
1.Compile-time Polymorphism (Method Overloading):
1. This occurs when multiple methods in the same class have the same name but
different parameters (type, number, or both).
2.Runtime Polymorphism (Method Overriding):
1. This occurs when a subclass provides a specific implementation of a method that is
already defined in its parent class. The method to be executed is determined at
runtime.
EXAMPLE
class Animal { public class Main {
void sound() { System.out.println("Animal
public static void main(String[]
makes a sound"); }
args) { Animal myDog = new
}
Dog();
class Dog extends Animal {
void sound() { System.out.println("Dog
Animal myCat = new Cat();
barks"); } myDog.sound(); // Output: Dog
} barks myCat.sound(); // Output:
class Cat extends Animal { Cat meows
void sound() { System.out.println("Cat }
meows"); }
}
}
What is an Object in Java?
In Java, an object is an instance of a class. Objects are the building blocks of
object-oriented programming (OOP) and represent real-world entities. Each object
has state (attributes) and behavior (methods), allowing it to perform specific tasks.
Key Characteristics of Objects:
1.State:
•The state of an object is represented by its attributes or fields. For example, a
Car object might have attributes like color, make, and model.
2.Behavior:
•The behavior of an object is defined by its methods. Continuing with the Car
example, methods might include start(), stop(), and accelerate().
3.Identity:
•Each object has a unique identity, which distinguishes it from other objects,
even if they have the same state.
EXAMPLE
class Dog {
String name; // Attribute
void bark() { // Method
System.out.println(name + " says: Woof!");
}
}
public class Main {
public static void main(String[] args) {
Dog myDog = new Dog(); // Creating an object
myDog.name = "Buddy"; // Setting the attribute
myDog.bark(); // Calling the method
}
}