BIT401Semester VII10 units48 solved questions561 study cardsFull study modes
The free sample is open to everyone. Sign in for 2 free weeks of every answer, flashcards and progress.
Object-oriented programming (OOP) builds a program from classes and objects. This chapter covers the Java features that give OOP its power: overloading, overriding, inheritance, interfaces, inner classes, packages and the Object class.
Method overloading means writing many methods with the same name in one class. Their parameter lists must differ in number, type or order. The return type alone cannot separate them. Java picks the method at compile time, so this is compile-time polymorphism.
public class Calc {
int add(int a, int b) { return a + b; }
double add(double a, double b) { return a + b; }
int add(int a, int b, int c) { return a + b + c; }
public static void main(String[] args) {
Calc c = new Calc();
System.out.println(c.add(2, 3));
System.out.println(c.add(2.5, 1.5));
System.out.println(c.add(1, 2, 3));
}
}
Method overriding means a subclass gives its own body to a method it inherits. The name, parameters and return type must match. The access level cannot be reduced. Java picks the method at run time from the real object type, so this is run-time polymorphism.
class Person {
int id; String name;
void getPerson(int id, String name) { this.id = id; this.name = name; }
void showPerson() { System.out.println(id + " " + name); }
}
class Student extends Person {
@Override
void showPerson() {
super.showPerson();
System.out.println("Role: student");
}
}
public class OverrideDemo {
public static void main(String[] args) {
Person p = new Student();
p.getPerson(1, "Sita");
p.showPerson();
}
}
| Overloading | Overriding |
|---|---|
| Same class | Parent and child class |
| Parameters must differ | Parameters must be the same |
| Resolved at compile time | Resolved at run time |
| Static and private methods allowed | Static, private and final methods cannot be overridden |
Inheritance lets a class (the subclass) reuse the fields and methods of another class (the superclass). It uses the extends keyword. It supports code reuse and a clear "is-a" relation. Java allows single, multilevel and hierarchical inheritance among classes. A class cannot extend two classes, which avoids the diamond problem. Multiple inheritance of type is done through interfaces.
class Animal { void eat() { System.out.println("eats"); } }
class Dog extends Animal { void bark() { System.out.println("barks"); } }
The super keyword reaches the parent's members and constructors. Private members are not inherited directly.
Advanced Java Programming for TU BIT is covered here in full, from syllabus notes to solved past questions. This subject has 10 chapters mapped to the official syllabus, and 48 past exam questions with written model answers. The opening of every note and answer is free, and the most-searched questions are free in full. A free account gets 2 free weeks of the whole program plus flashcards and progress tracking so weak topics come back before the exam.
This TU BIT Advanced Java Programming guide covers Object-Oriented Programming Fundamentals, Exception Handling and Input/Output, GUI Programming with AWT and Swing, JavaFX Programming, Database Connectivity with JDBC, Web Programming with Servlets and JSP, Network Programming with Sockets, Multithreading and Concurrency, and more, across 10 chapters. It includes model answers to 48 past exam questions, plus flashcards for quick revision.
Advanced Java Programming has 10 chapters mapped to the official syllabus.
Yes. Each of the 48 past questions comes with a written model answer; the most-searched ones are free in full, the rest open with a free account's 2 free weeks or a pass.
Free to start. The opening of every note and answer is free, the most-searched questions are free in full, and a free account gets 2 free weeks of the whole program plus flashcards and progress tracking. After that a program pass keeps everything open.
Based on the official syllabus.