0% found this document useful (0 votes)
27 views8 pages

Csc 303 Continuation

Uploaded by

Ozioko Johnpaul
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)
27 views8 pages

Csc 303 Continuation

Uploaded by

Ozioko Johnpaul
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/ 8

INTRODUCTION TO JAVA

PROGRAMMING
INHERITANCE
In object-oriented programming (OOP), Education System
inheritance is a mechanism that allows a class Educational institutions have hierarchical
(called the child class or subclass) to acquire the roles.
properties and behaviors (fields and methods) of Parent Class: Person
another class (called the parent class or Common properties: name, age.
superclass). Common methods: speak(), walk().
Inheritance promotes code reuse, making it
easier to create and maintain applications. Child Classes: Student, Teacher
Student might have study() and takeExam().
Animals and Their Traits Teacher might have teach() and gradePapers()
Different animals share common characteristics
but also have unique features. Banking System
Parent Class: Animal Different types of accounts in a bank share
Common properties: name, age. common features.
Common methods: eat(), sleep(). Parent Class: BankAccount
Common properties: accountNumber, balance.
Child Classes: Dog, Bird, Fish Common methods: deposit(), withdraw().
Dog might have bark() and wagTail().
Bird might have fly() and sing(). Child Classes: SavingsAccount, CurrentAccount
Fish might have swim(). SavingsAccount might add calculateInterest().
CurrentAccount might add overdraftLimit()
class Vehicle {
void start() { Inheritance:
System.out.println("Vehicle starting");
The Car class inherits all the methods and
}
} properties of the Vehicle class.
// Child class (Inherits from Vehicle)
Inheritance allows code reuse, as we don’t have to
class Car extends Vehicle {
void honk() { redefine the start method in the Car class.
System.out.println("Car honking");
Calling Methods:
}
} When calling myCar.start(), Java looks for the start
public class Main {
method in the Car class. Since it’s not explicitly
public static void main(String[] args) {
Car myCar = new Car(); // Create Car object defined there, it finds the method in the parent
myCar.start(); // Call method from Vehicle (parent class)
class (Vehicle).
myCar.honk(); // Call method from Car (child class)
} The myCar.honk() call directly uses the method
} defined in the Car class.
Polymorphism class Calculator {

Polymorphism means "many forms." In Java, it allows // Method with two parameters
one method to behave differently based on the object int add(int a, int b) {
calling it. There are two types of polymorphism: return a + b;
Method Overloading (Compile-time polymorphism): }
Same method name but different parameters.
// Method with three parameters (overloading)
int add(int a, int b, int c) {
return a + b + c;
}
}
public class Main {
public static void main(String[] args) {
Calculator calc = new Calculator();
System.out.println(calc.add(5, 10)); //
Output: 15
System.out.println(calc.add(5, 10, 20)); //
Output: 35
Polymorphism class Calculator {

Polymorphism means "many forms." In Java, it allows // Method with two parameters
one method to behave differently based on the object int add(int a, int b) {
calling it. There are two types of polymorphism: return a + b;
Method Overloading (Compile-time polymorphism): }
Same method name but different parameters.
// Method with three parameters (overloading)
Real Life Example: int add(int a, int b, int c) {
1. A calculator can have multiple methods called `add()`. return a + b + c;
If you provide 2 numbers, it adds 2; if you provide 3
}
numbers, it adds 3. The name of the method remains
the same, but it behaves differently depending on the }
input. public class Main {
2. Shape Area Calculation public static void main(String[] args) {
A method to calculate the area of different Calculator calc = new Calculator();
shapes.
Methods Overloaded: System.out.println(calc.add(5, 10)); //
area(int side) for square. Output: 15
area(int length, int breadth) for rectangle. System.out.println(calc.add(5, 10, 20)); //
area(double radius) for circle. Output: 35
Method Overriding (Runtime polymorphism): A method public class Main {
in a subclass overrides a method in the parent class. public static void main(String[] args) {
// Parent class Animal myAnimal = new Animal();
class Animal { myAnimal.sound(); // Output: Animal makes a sound
void sound() { Dog myDog = new Dog();
System.out.println("Animal makes a sound"); myDog.sound(); // Output: Dog barks (Overridden
} method)

} }

// Child class (Overriding the parent method) }

class Dog extends Animal { Explanation:

@Override Method Overriding: The Animal class has a sound() method.


The Dog class extends Animal and overrides the sound()
void sound() { method to provide its specific behavior (barking).
System.out.println("Dog barks"); Polymorphism in Action:
} When you create an Animal object, it uses the sound() method
} of Animal and outputs "Animal makes a sound."
When you create a Dog object, it uses the overridden sound()
method and outputs "Dog barks."
Real-world analogy: Animal Sounds
Animals all make sounds, but each species has a
Think of a generalized job title like "engineer." unique way of doing so:
•Parent Behavior: Animals "make a sound."
Depending on the specific person (object), they may be a
•Override by Subclasses:
software engineer, mechanical engineer, etc. • A dog barks.
The title is the same, but the actual role (method) varies • A cat meows.
based on who you're talking to. • A cow moos.
The general behavior ("make a sound") is
overridden in each animal species to reflect its
Printing Documents specific sound.
Printing can vary depending on the type of
printer used:
•Parent Behavior: All printers "print
documents."
•Override by Subclasses:
• An inkjet printer may use ink-based
printing with slower speeds.
• A laser printer prints faster and uses
toner instead of ink.
Although the action of printing remains
consistent, the process depends on the
printer type.
} else {
class Person { System.out.println("Please enter a valid age.");
// Private fields for encapsulation }
private String name; }
private int age; }
// Constructor to initialize Person object public class EncapsulationExample {
public Person(String name, int age) { public static void main(String[] args) {
this.name = name; // Creating a new Person object
this.age = age; Person person = new Person("Alice", 25);
} // Accessing and modifying private fields using getter and setter
// Public getter method for name methods
public String getName() { System.out.println("Name: " + person.getName());
return name; System.out.println("Age: " + person.getAge());
} // Modifying name and age using setter methods
// Public setter method for name person.setName("Bob");
public void setName(String name) { person.setAge(30);
this.name = name;
} // Displaying updated values
// Public getter method for age System.out.println("Updated Name: " + person.getName());
public int getAge() { System.out.println("Updated Age: " + person.getAge());
return age;
} // Trying to set an invalid age
// Public setter method for age with validation person.setAge(-5); // This will show a validation message
public void setAge(int age) { }
if (age > 0) { // Validating age to ensure it's positive }
this.age = age;

You might also like