Unit 3
Unit 3
Archana NS
Department of Computer Applications
Don Bosco College
TC Palya
• Inheritance is a mechanism in Java by
which one class inherits the properties
(fields) and behaviors (methods) of
another class.
• Purpose: Promotes code reusability and
Introduction to establishes a natural hierarchy between
classes.
Inheritance in • Syntax
Java class SubClass extends SuperClass {
// additional fields and methods
}
• Single Inheritance
• A subclass inherits from one superclass
class Animal {
void eat() {
System.out.println("This animal eats
Types of food.");
}
Inheritance in }
Java class Dog extends Animal {
void bark() {
System.out.println("The dog barks.");
}
}
Types of Inheritance
Multilevel
Inheritance:
Hierarchical Inheritance
The overridden
method must be
accessible in the
subclass.
class Animal {
void makeSound() {
System.out.println("Animal makes a sound.");
}
}
Uses
System.out.println("dog is created");
}
}
class TestSuper3{
public static void main(String args[]){
Dog d=new Dog();
}}
Final Keyword
classes
inherited.
}
class Honda extends Bike{
void run(){System.out.println("running safely");}
public static void main(String args[]){
Honda obj = new Honda();
obj.run();
}
}
Interface in Java
interface animal{
int age=10;
void eat();
void travel();
}
• Implementing interfaces
• Keyword: public
• Visibility: Any class from any package
can access.
• Usage: Applied to classes, methods,
and variables.
Access
specifiers Protected
• Keyword: protected
• Visibility: Accessible within the same
package and by subclasses (including
those in different packages).
• Usage: Applied to methods and
variables. Not applicable to top-level
classes.
Default (Package-Private)
• Keyword: private
• Visibility: Accessible only within the
same class.
• Usage: Applied to methods and
variables.
• Not applicable to top-level classes.
This Photo by Unknown Author is licensed under CC BY-SA
java.lang package.
class Obj=null;
Obj=new Object();
Obj=std;
Object •class java.lang.Object
•getClass()
• Returns class name of the object.
•class Test
class • Object.getClass();
• Public class Test{
methods
operate on objects of various types
• public class GenericMethodExample {
public static <T> void printArray(T[] inputArray) {
for (int i = 0; i < inputArray.length; i++) {
System.out.println(element);}}
public static void main(String[] args) {
Integer[] intArray = {1, 2, 3, 4, 5};
Double[] doubleArray = {1.1, 2.2, 3.3, 4.4, 5.5};
String[] stringArray = {"Hello", "World",
"Generic", "Methods"};
printArray(intArray); // Pass an Integer array
printArray(doubleArray); // Pass a Double array
System.out.println("\nArray of Strings:");
printArray(stringArray); // Pass a String array
}
}
Wildcards in Generics
• Wildcards in generics provide flexibility and reusability
when dealing with parameterized types.
• Unbounded Wildcard (?)
• Bounded Wildcard with an Upper Bound (? extends
Type)
• Bounded Wildcard with a Lower Bound (? super Type)
Wildcard Type Definition Use Case
Represents an unknown General methods that can
?
type accept any type
? extends Type Represents a type that is a Read-only operations where
subtype of Type the type is a specific class or
its subtype
? super Type Represents a type that is a Write operations where the
supertype of Type type is a specific class or its
supertype