Java object oriented programming
Java object oriented programming
Chapters 3-6
Student Study Guide
May 18, 2025
Contents
1 Inheritance 3
1.1 Purpose of Inheritance . . . . . . . . . . . . . . . . . . . . . . . . 3
1.2 Terms Used in Inheritance . . . . . . . . . . . . . . . . . . . . . . 3
1.3 Types of Inheritance in Java . . . . . . . . . . . . . . . . . . . . . 3
1.4 Multiple and Hybrid Inheritance . . . . . . . . . . . . . . . . . . 4
1.5 Class Relationships . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.5.1 Association . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.5.2 Aggregation . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.5.3 Composition . . . . . . . . . . . . . . . . . . . . . . . . . 4
2 Polymorphism 5
2.1 Types of Polymorphism . . . . . . . . . . . . . . . . . . . . . . . 5
2.2 Static Polymorphism . . . . . . . . . . . . . . . . . . . . . . . . . 5
2.3 Type Promotion . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
2.4 Dynamic Polymorphism . . . . . . . . . . . . . . . . . . . . . . . 6
2.5 Rules of Method Overriding . . . . . . . . . . . . . . . . . . . . . 6
2.6 super Keyword in Method Overriding . . . . . . . . . . . . . . . 7
3 Exception Handling 7
3.1 Types of Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . 7
3.2 Exception Handling Keywords . . . . . . . . . . . . . . . . . . . . 7
3.3 Try-Catch Block . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
3.4 Multiple Catch Blocks . . . . . . . . . . . . . . . . . . . . . . . . 8
3.5 Nested Try Block . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
3.6 Finally Block . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
3.7 Throw and Throws . . . . . . . . . . . . . . . . . . . . . . . . . . 9
3.8 Exception Propagation . . . . . . . . . . . . . . . . . . . . . . . . 9
3.9 Custom Exception . . . . . . . . . . . . . . . . . . . . . . . . . . 9
1
4 Abstraction 10
4.1 Ways to Achieve Abstraction . . . . . . . . . . . . . . . . . . . . 10
4.2 Abstract Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
4.3 Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
4.4 Java 8 Interface Improvements . . . . . . . . . . . . . . . . . . . 11
4.5 Differences between Abstract Class and Interface . . . . . . . . . 12
5 Conclusion 12
2
1 Inheritance
“Inheritance is a mechanism in which one object acquires all the
properties and behaviours of a parent object.”
1 // Parent class
2 class Animal {
3 void eat () {
4 System . out . println ( " eating ... " ) ;
5 }
6 }
7
8 // Child class
9 class Dog extends Animal {
10 void bark () {
11 System . out . println ( " barking ... " ) ;
12 }
13 }
14
15 // Usage
16 class Te st I nh er it a nc e {
17 public static void main ( String [] args ) {
18 Dog d = new Dog () ;
19 d . bark () ; // Method from Dog class
20 d . eat () ; // Method from Animal class
21 }
22 }
Listing 1: Example of single inheritance
3
1.4 Multiple and Hybrid Inheritance
Multiple and hybrid inheritance are not supported in Java through classes (to
reduce complexity and ambiguity), but are supported through interfaces.
1.5.2 Aggregation
A special form of association representing an “has-a” relationship between ob-
jects. The owned object can exist independently of the owning object.
1 public class Address {
2 private String city ;
3 private String state ;
4 // Constructor , getters , setters
5 }
6
7 public class Student {
8 private int id ;
9 private String name ;
10 private Address address ; // Aggregation
11
12 // Constructor
13 public Student ( int id , String name , Address address ) {
14 this . id = id ;
15 this . name = name ;
16 this . address = address ;
17 }
18 }
Listing 2: Example of aggregation
1.5.3 Composition
A special case of aggregation where the owned object is exclusively owned by
the aggregating object and cannot exist independently.
1 public class Name {
2 private String firstName ;
3 private String lastName ;
4 // Constructor , getters , setters
5 }
6
7 public class Student {
8 private int id ;
9 private Name name ; // Composition
10
4
11 // Constructor
12 public Student ( int id , String firstName , String lastName ) {
13 this . id = id ;
14 this . name = new Name ( firstName , lastName ) ; // Name object
created inside Student
15 }
16 }
Listing 3: Example of composition
2 Polymorphism
“Polymorphism allows us to perform a single action in different
ways.”
5
20 }
21
22 float add ( float a , int b ) {
23 return a + b ;
24 }
25 }
Listing 4: Example of method overloading
6
3. Private, static, and final methods cannot be overridden
4. The overriding method can throw unchecked exceptions
5. Binding happens at runtime (dynamic binding)
6. A class extending an abstract class must override all abstract methods
unless the class itself is abstract
3 Exception Handling
“In Java, an exception is an event that disrupts the normal flow of
the program. It is an object which is thrown at runtime.”
7
• throw: Throws an exception explicitly
• throws: Declares exceptions that might occur in a method
8
1 public class FinallyE xample {
2 public static void main ( String [] args ) {
3 try {
4 int result = 50 / 0; // A r i t h m e t i c E x c e p t i o n
5 } catch ( A r i t h m e t i c E x c e p t i o n e ) {
6 System . out . println ( " Exception : " + e . getMessage () ) ;
7 } finally {
8 // This block always executes
9 System . out . println ( " Finally block executed " ) ;
10 }
11 }
12 }
Listing 9: Example of finally block
9
8
9 // Using custom exception
10 public class C u s t o m E x c e p t i o n E x a m p l e {
11 static void validate ( int age ) throws I n v a l i d A g e E x c e p t i o n {
12 if ( age < 18) {
13 throw new I n v a l i d A g e E x c e p t i o n ( " Age is not valid for
voting " ) ;
14 } else {
15 System . out . println ( " Welcome to vote " ) ;
16 }
17 }
18
19 public static void main ( String [] args ) {
20 try {
21 validate (13) ;
22 } catch ( I n v a l i d A g e E x c e p t i o n e ) {
23 System . out . println ( " Exception : " + e . getMessage () ) ;
24 }
25 }
26 }
Listing 11: Example of custom exception
4 Abstraction
“Abstraction is a process of hiding the implementation details and
showing only functionality to the user.”
10
17 }
18 }
Listing 12: Example of abstract class
4.3 Interface
A mechanism to achieve abstraction containing only abstract methods (except
in Java 8+).
1 // Interface
2 interface Drawable {
3 void draw () ; // Abstract method ( by default )
4 }
5
6 // Implementing class
7 class Rectangle implements Drawable {
8 @Override
9 public void draw () {
10 System . out . println ( " Drawing rectangle " ) ;
11 }
12 }
Listing 13: Example of interface
11
4.5 Differences between Abstract Class and Interface
5 Conclusion
This focused review has covered the key concepts from chapters 3-6 of the
Object-Oriented Programming course:
• Inheritance: Mechanism for code reuse where one class acquires proper-
ties of another class
• Polymorphism: Ability to perform a single action in different ways,
through method overloading and overriding
12