OOP in Java (BCA 153): Classes, Inheritance and Exam Patterns
Object orientation is a way of organising programs, not a list of keywords. This guide covers classes, inheritance, polymorphism, exceptions and collections in Java.
OOP in Java is BCA 153, a three-credit course in the second semester with three lecture hours and three practical hours a week. By the time you meet it you have already written procedural programs in C, so the new material is not syntax but a different way of deciding where code should live.
Object orientation is easier to learn if you treat it as a design habit rather than a set of keywords. The keywords matter, but they exist to express four ideas: keeping data and the operations on it together, hiding what should not be touched from outside, reusing behaviour through inheritance, and letting different types respond to the same request in their own way.
Classes, objects and encapsulation
A class is a template; an object is an instance of it. Fields hold the state and methods hold the behaviour. The constructor runs when an object is created and is the right place to establish a valid initial state. If a class promises that an object is always usable, the constructor is where that promise is kept.
Encapsulation means making fields private and exposing only the operations that make sense. A well-encapsulated class cannot be put into an invalid state by an outside caller, because every change goes through a method that can check it. Getters and setters are the simplest form of this, but the real skill is deciding which operations belong on the class at all.
Inheritance and polymorphism
Inheritance lets a subclass reuse and extend a superclass. The subclass inherits accessible fields and methods, can add its own, and can override a method to change behaviour. Every constructor begins by calling a superclass constructor, either explicitly with super(...) or implicitly. If the superclass has no no-argument constructor, the call must be explicit or the code will not compile.
Polymorphism is where inheritance pays off. A reference of the superclass type can point at any subclass object, and the method that runs is chosen from the actual object type at run time. That is dynamic dispatch, and it is why you can write a loop over a list of superclass references and have each element behave correctly without a chain of type checks.
Overloading and overriding are different things and are commonly confused in examinations. Overloading means several methods with the same name but different parameter lists, resolved at compile time. Overriding means a subclass replaces a superclass method with the same signature, resolved at run time.
| Concept | Mechanism | What it gives you |
|---|---|---|
| Encapsulation | private fields with public methods | A class that cannot be left in an invalid state |
| Inheritance | extends | Reuse of behaviour with the option to specialise |
| Overloading | Same name, different parameters | One readable name for related operations |
| Overriding | Same signature in a subclass | Subtype-specific behaviour chosen at run time |
| Abstraction | abstract class or interface | A contract separated from any one implementation |
| Runtime errors | try, catch, finally | Recovery paths instead of a crashed program |
Interfaces and abstract classes
An abstract class can hold state and partial implementations but cannot be instantiated. An interface declares a contract that a class promises to fulfil, and a class can implement several interfaces while extending only one class. Use an abstract class when related types share code and an interface when unrelated types need to be usable in the same way.
A practical rule: program against interfaces. If a method accepts a List rather than an ArrayList, the caller can pass any implementation, and tests can substitute a simple one. That habit also makes the collections framework far easier to use.
Exceptions
Java separates problems into checked exceptions, which the compiler requires you to handle or declare, and unchecked exceptions, which usually indicate programming errors. A try block contains the risky work, catch handles a specific type, and finally runs whether or not an exception occurred. Catch the most specific exception you can handle, and never write an empty catch block.
- Catch only what you can act on; anything else should propagate to a caller that can.
- Use finally for cleanup such as closing a stream, or use a try-with-resources block.
- An exception message should say what operation failed and on what value.
- Do not use exceptions for ordinary control flow; test conditions normally where you can.
Collections you will actually use
The collections framework gives you ready-made containers: List for ordered sequences, Set for uniqueness, Map for key-value lookup, and Queue and Deque for ordered processing. Generics let you state the element type, so List<String> is checked at compile time and needs no casting. Choose ArrayList for indexed access and frequent iteration, LinkedList when you insert and remove at the ends, HashSet for fast membership tests, and HashMap for lookups by key.
Two details repay attention in exams and in code. Strings are immutable, so concatenation in a loop creates many objects and StringBuilder is the appropriate tool. And equals and hashCode must agree: if you override one, override the other, or your objects will behave unpredictably inside a HashSet or HashMap.
What examiners tend to ask
Written papers usually combine three question styles: define and distinguish concepts, trace the output of a short program, and write a small class or hierarchy to a given specification. Lab work tends to ask for a working program with sensible class design rather than clever tricks. Confirm the exact weightings with your department, since the published assessment scheme is general.
- For definition questions, write one sentence of meaning and one concrete difference, and give a short example.
- For output tracing, draw objects and references on paper, then step through the code in order.
- For coding questions, name variables clearly and keep methods short, because readability is part of the mark.
- Rehearse the classic examples: a shape hierarchy, a bank account, and a small student record system.
Do I need C before learning Java?
No, but it helps. C teaches memory and control flow explicitly, which makes Java’s reference model and garbage collection easier to appreciate. In the BCA structure, Programming in C comes first and Java follows.
What is the difference between an abstract class and an interface?
An abstract class can store state and provide partial implementations, and a class may extend only one of them. An interface defines a contract with no instance state, and a class may implement many. Use the abstract class for shared code, the interface for shared capability.
Why does my program print an object reference instead of its contents?
Because you have not overridden toString. The default implementation prints the class name and a hash value. Override toString to return a readable description, and it will appear wherever the object is concatenated to a string.
How should I prepare for the lab component?
Practise writing small complete programs rather than isolated methods. Before each lab, sketch the classes, their fields and their methods, then implement. Keeping one working reference program per topic makes revision far faster.
Java sits alongside five other courses in the second semester, so it helps to plan the term as a whole. The second semester guide covers the workload, the lab blocks and how the courses fit together.