0% found this document useful (0 votes)
1 views12 pages

Java object oriented programming

The document is a student study guide covering Object-Oriented Programming concepts from Chapters 3-6, including inheritance, polymorphism, exception handling, and abstraction. It details key terms, types, and examples related to each topic, such as method overriding, types of exceptions, and the use of try-catch blocks. The guide serves as a comprehensive review for students preparing for exams in Java programming.

Uploaded by

rarasoso99
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views12 pages

Java object oriented programming

The document is a student study guide covering Object-Oriented Programming concepts from Chapters 3-6, including inheritance, polymorphism, exception handling, and abstraction. It details key terms, types, and examples related to each topic, such as method overriding, types of exceptions, and the use of try-catch blocks. The guide serves as a comprehensive review for students preparing for exams in Java programming.

Uploaded by

rarasoso99
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Object-Oriented Programming Review

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.1 Purpose of Inheritance


• Method overriding (runtime polymorphism)
• Code reusability

1.2 Terms Used in Inheritance


• Class: A group of objects with common properties
• Subclass/Child Class: A class that inherits from another class
• Superclass/Parent Class: The class from which a subclass inherits
• Reusability: Mechanism to reuse fields and methods of the parent class

1.3 Types of Inheritance in Java


• Single Inheritance: One subclass inherits from one superclass
• Multilevel Inheritance: A class inherits from a class which itself inherits
from another class
• Hierarchical Inheritance: Multiple classes inherit from a single class

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 Class Relationships


1.5.1 Association
A general binary relationship that describes an activity between two classes.
For example, a student taking a course is an association between the Student
class and the Course class.

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.”

2.1 Types of Polymorphism


1. Static Polymorphism (Compile-time polymorphism)
2. Dynamic Polymorphism (Runtime polymorphism)

2.2 Static Polymorphism


Method overloading: Having more than one method with the same name but
different parameters.
Three ways to overload a method:
1. Different number of parameters

2. Different data types of parameters


3. Different sequence of data types of parameters

1 public class Calculator {


2 // Method with two parameters
3 int add ( int a , int b ) {
4 return a + b ;
5 }
6
7 // Method with three parameters ( different number )
8 int add ( int a , int b , int c ) {
9 return a + b + c ;
10 }
11
12 // Method with different data types
13 float add ( float a , float b ) {
14 return a + b ;
15 }
16
17 // Method with different sequence of parameters
18 float add ( int a , float b ) {
19 return a + b ;

5
20 }
21
22 float add ( float a , int b ) {
23 return a + b ;
24 }
25 }
Listing 4: Example of method overloading

2.3 Type Promotion


When a data type of smaller size is promoted to the data type of bigger size,
this is called type promotion. For example, byte data type can be promoted to
short, a short data type can be promoted to int, long, double, etc.

2.4 Dynamic Polymorphism


Method overriding: Declaring a method in a subclass that is already present in
the parent class.
1 // Parent class
2 class Human {
3 void eat () {
4 System . out . println ( " Human is eating " ) ;
5 }
6 }
7
8 // Child class
9 class Boy extends Human {
10 @Override
11 void eat () {
12 System . out . println ( " Boy is eating " ) ;
13 }
14 }
15
16 // Usage
17 class Test {
18 public static void main ( String [] args ) {
19 Human h1 = new Human () ;
20 h1 . eat () ; // Outputs : Human is eating
21
22 Human h2 = new Boy () ;
23 h2 . eat () ; // Outputs : Boy is eating ( Runtime polymorphism )
24 }
25 }
Listing 5: Example of method overriding

2.5 Rules of Method Overriding


1. The argument list must match exactly
2. The access modifier must be less restrictive than the overridden method

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

2.6 super Keyword in Method Overriding


1 // Parent class
2 class Person {
3 void display () {
4 System . out . println ( " Person display method " ) ;
5 }
6 }
7
8 // Child class
9 class Student extends Person {
10 @Override
11 void display () {
12 super . display () ; // Calling parent class method
13 System . out . println ( " Student display method " ) ;
14 }
15 }
Listing 6: Example of super keyword in method overriding

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.”

3.1 Types of Exceptions


1. Checked Exceptions: Exceptions checked at compile-time (e.g., IOExcep-
tion, SQLException)
2. Unchecked Exceptions: Exceptions checked at runtime (e.g., ArithmeticEx-
ception, NullPointerException)
3. Errors: Irrecoverable at runtime (e.g., OutOfMemoryError, StackOver-
flowError)

3.2 Exception Handling Keywords


• try: Specifies a block where code that might throw an exception is placed

• catch: Handles exceptions that occur in the try block


• finally: Executes necessary code whether an exception is handled or not

7
• throw: Throws an exception explicitly
• throws: Declares exceptions that might occur in a method

3.3 Try-Catch Block


1 public class E x c e p t i o n E x a m p l e {
2 public static void main ( String [] args ) {
3 try {
4 // Code that may throw exception
5 int result = 50 / 0; // A r i t h m e t i c E x c e p t i o n
6 } catch ( A r i t h m e t i c E x c e p t i o n e ) {
7 // Exception handler
8 System . out . println ( " Cannot divide by zero " ) ;
9 }
10
11 // The program continues execution
12 System . out . println ( " Rest of the code " ) ;
13 }
14 }
Listing 7: Example of try-catch block

3.4 Multiple Catch Blocks


1 public class M u l t i p l e C a t c h E x a m p l e {
2 public static void main ( String [] args ) {
3 try {
4 int [] arr = new int [5];
5 arr [10] = 30 / 0; // Multiple exceptions possible
6 } catch ( A r i t h m e t i c E x c e p t i o n e ) {
7 System . out . println ( " Arithmetic Exception : " + e.
getMessage () ) ;
8 } catch ( A r r a y I n d e x O u t O f B o u n d s E x c e p t i o n e ) {
9 System . out . println ( " A r r a y I n d e x O u t O f B o u n d s Exception : "
+ e . getMessage () ) ;
10 } catch ( Exception e ) {
11 System . out . println ( " Parent Exception : " + e . getMessage
() ) ;
12 }
13 }
14 }
Listing 8: Example of multiple catch blocks

3.5 Nested Try Block


Using a try block inside another try block is permitted and is called a nested
try block. Useful when a part of a block may cause one error and the entire
block itself may cause another error.

3.6 Finally Block

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

3.7 Throw and Throws


1 public class ThrowExample {
2 // Method declaring exceptions with throws
3 public static void validate ( int age ) throws A r i t h m e t i c E x c e p t i o n
{
4 if ( age < 18) {
5 // Throwing an exception explicitly
6 throw new A r i t h m e t i c E x c e p t i o n ( " Person is not eligible
to vote " ) ;
7 } else {
8 System . out . println ( " Person is eligible to vote " ) ;
9 }
10 }
11
12 public static void main ( String [] args ) {
13 try {
14 validate (15) ;
15 } catch ( A r i t h m e t i c E x c e p t i o n e ) {
16 System . out . println ( " Exception : " + e . getMessage () ) ;
17 }
18 }
19 }
Listing 10: Example of throw and throws keywords

3.8 Exception Propagation


An exception is first thrown from the top of the stack and if it is not caught, it
drops down the call stack to previous methods until caught or until it reaches
the bottom of the call stack.

3.9 Custom Exception


1 // Custom exception class
2 class I n v a l i d A g e E x c e p t i o n extends Exception {
3 // Constructor
4 public I n v a l i d A g e E x c e p t i o n ( String message ) {
5 super ( message ) ; // Calling parent constructor
6 }
7 }

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.”

4.1 Ways to Achieve Abstraction


1. Abstract classes (0 to 100% abstraction)

2. Interfaces (100% abstraction)

4.2 Abstract Class


A class declared with the abstract keyword. It can have abstract and non-
abstract methods.
1 // Abstract class
2 abstract class Shape {
3 // Abstract method ( no i mplemen tation )
4 abstract void draw () ;
5
6 // Non - abstract method
7 void display () {
8 System . out . println ( " This is a shape " ) ;
9 }
10 }
11
12 // Concrete class
13 class Rectangle extends Shape {
14 @Override
15 void draw () {
16 System . out . println ( " Drawing rectangle " ) ;

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

4.4 Java 8 Interface Improvements


Since Java 8, interfaces can have:

• Default methods (with implementation)


• Static methods

1 // Interface with default and static methods


2 interface Drawable {
3 void draw () ; // Abstract method
4
5 // Default method
6 default void msg () {
7 System . out . println ( " Default message " ) ;
8 }
9
10 // Static method
11 static void info () {
12 System . out . println ( " Drawable interface " ) ;
13 }
14 }
Listing 14: Example of Java 8 interface features

11
4.5 Differences between Abstract Class and Interface

Abstract Class Interface


Can have abstract and non-abstract methods Can have only abstract methods (before Java
8)
Doesn’t support multiple inheritance Supports multiple inheritance
Can have final, non-final, static and non-static Has only static and final variables
variables
Can provide implementation of an interface Cannot provide implementation of an abstract
class
Uses the abstract keyword Uses the interface keyword
An abstract class can extend another class and An interface can only extend other interfaces
implement interfaces
Members can have access modifiers like pri- Members are public by default
vate, protected, etc.

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

• Exception Handling: Managing runtime errors to maintain normal pro-


gram flow
• Abstraction: Hiding implementation details and showing only function-
ality to users

These concepts form the backbone of object-oriented programming and pro-


vide powerful tools for creating modular, maintainable, and robust software.

12

You might also like