0% found this document useful (0 votes)
7 views6 pages

Java OOP Exam Guide

This document serves as an exam preparation guide for Java OOP concepts, covering key topics such as encapsulation, method overriding, method overloading, constructor overloading, and the use of interfaces. It provides definitions, examples, and code snippets to illustrate each concept, emphasizing the importance of controlled access and memory optimization in object-oriented programming. Additionally, it discusses the use of the static keyword and multiple inheritance through interfaces.
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)
7 views6 pages

Java OOP Exam Guide

This document serves as an exam preparation guide for Java OOP concepts, covering key topics such as encapsulation, method overriding, method overloading, constructor overloading, and the use of interfaces. It provides definitions, examples, and code snippets to illustrate each concept, emphasizing the importance of controlled access and memory optimization in object-oriented programming. Additionally, it discusses the use of the static keyword and multiple inheritance through interfaces.
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/ 6

Java OOP Concepts - Exam Preparation Guide

1. Encapsulation with Controlled Access

Definition:

Encapsulation is one of the fundamental principles of object-oriented programming (OOP). It refers

to the mechanism of hiding internal data and providing controlled access through public methods.

How to Achieve Encapsulation:

1. Declare variables as private.

2. Provide public getter and setter methods.

3. Perform validation in setters.

4. Allow read/write access only when necessary.

5. Keep internal data safe and maintainable.

Code Example:

class Student {

private String name;

public void setName(String n) { name = n; }

public String getName() { return name; }

2. Encapsulation Example with Student Class

class Student {

private int age;


public void setAge(int a) { if (a > 0) age = a; }

public int getAge() { return age; }

public class Test {

public static void main(String[] args) {

Student s = new Student();

s.setAge(18);

System.out.println("Age: " + s.getAge());

3. Method Overriding with Shape Example

Definition:

Method overriding is when a subclass provides a specific implementation for a method that is

already defined in its superclass.

class Shape {

void area() { System.out.println("Area of shape"); }

class Rectangle extends Shape {

void area() { System.out.println("Area = length * breadth"); }

class Circle extends Shape {


void area() { System.out.println("Area = pi * r * r"); }

public class Test {

public static void main(String[] args) {

Shape s1 = new Rectangle();

Shape s2 = new Circle();

s1.area();

s2.area();

4. Method Overloading

Definition:

Method overloading allows a class to have multiple methods with the same name but different

parameters.

class MathUtils {

void add(int a, int b) { System.out.println("Sum: " + (a + b)); }

void add(int a, int b, int c) { System.out.println("Sum: " + (a + b + c)); }

5. Constructor Overloading

Definition:

Constructor overloading means creating multiple constructors in a class with different parameter
lists.

class Student {

String name;

int age;

Student() { name = "Unknown"; age = 0; }

Student(String n, int a) { name = n; age = a; }

void show() {

System.out.println(name + " - " + age);

6. Method Overriding with Vehicle Example

class Vehicle {

void fuelType() { System.out.println("Fuel type: General"); }

class Car extends Vehicle {

void fuelType() { System.out.println("Fuel type: Petrol"); }

class Truck extends Vehicle {

void fuelType() { System.out.println("Fuel type: Diesel"); }

}
7. Using super in Method Overriding

class Employee {

void getDetails() { System.out.println("Employee: Rahul"); }

class Manager extends Employee {

void getDetails() {

super.getDetails();

System.out.println("Department: Sales");

8. Multiple Inheritance using Interfaces

interface Person { void introduce(); }

interface Worker { void work(); }

class Employee implements Person, Worker {

public void introduce() { System.out.println("I am an employee."); }

public void work() { System.out.println("I work in IT."); }

9. Use of static Keyword

class Student {
String name;

static String school = "ABC School";

Memory Optimization:

Static members are shared across all objects, saving memory.

You might also like