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

Java OOP Concepts and Programs Explained

The document covers key concepts of Object-Oriented Programming (OOP) using Java, including reusability, inheritance forms, and the use of keywords like super and final. It explains method overriding, packages, interfaces, exception handling, and multithreading, providing example programs for practical understanding. The content emphasizes the importance of encapsulation, code organization, and error management in Java programming.

Uploaded by

kingdarshan607
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 views4 pages

Java OOP Concepts and Programs Explained

The document covers key concepts of Object-Oriented Programming (OOP) using Java, including reusability, inheritance forms, and the use of keywords like super and final. It explains method overriding, packages, interfaces, exception handling, and multithreading, providing example programs for practical understanding. The content emphasizes the importance of encapsulation, code organization, and error management in Java programming.

Uploaded by

kingdarshan607
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

22CS561 – OOP USING JAVA

COMPLETE 10-MARK ANSWERS WITH PROGRAMS

1. Reusable properties in Java


Reusability is a fundamental concept of Object-Oriented Programming. It allows
developers to write code once and reuse it multiple times without modification. Java
supports reusability using classes, objects, inheritance, interfaces, methods, and
packages. Classes act as blueprints, inheritance allows subclasses to reuse superclass
features, interfaces define common behavior for multiple classes, methods avoid repetition
of logic, and packages group related classes. Reusability reduces code duplication,
improves maintainability, saves time, and enhances software reliability.

2. Forms of Inheritance in Java


Inheritance allows one class to acquire the properties of another class. Java supports
Single, Multilevel, and Hierarchical inheritance. Multiple inheritance using classes is not
supported due to ambiguity, but is achieved using interfaces. Hybrid inheritance is a
combination of inheritance types implemented through interfaces.

Program:
class A { void show(){ [Link]("A"); } } class B extends A { void
display(){ [Link]("B"); } } class C extends B { void print(){
[Link]("C"); } }

3. super and final keywords


The super keyword refers to the immediate parent class and is used to access parent
class variables, methods, and constructors. The final keyword restricts modification. A final
variable cannot change its value, a final method cannot be overridden, and a final class
cannot be inherited. These keywords improve security and control over inheritance.

Program:
class A { int x=10; } class B extends A { int x=20; void show(){
[Link](super.x); } }

4. Method Overriding
Method overriding occurs when a subclass provides a specific implementation of a method
already defined in its superclass. It supports runtime polymorphism where method calls
are resolved at runtime based on object reference.
Program:
class Parent{ void show(){ [Link]("Parent"); } } class Child extends
Parent{ void show(){ [Link]("Child"); } } class Test{ public static
void main(String args[]){ Parent p=new Child(); [Link](); } }

5. Java Packages
A package is a namespace that groups related classes and interfaces. Packages improve
code organization, avoid naming conflicts, provide access control, and promote reusability.
Java provides built-in and user-defined packages.

6. Defining and Importing Packages


Packages are defined using the package keyword and accessed using the import
keyword.

Program:
// File: [Link] package mypack; public class Demo{ public void show(){
[Link]("Hello"); } } // File: [Link] import [Link]; class
Test{ public static void main(String args[]){ Demo d=new Demo(); [Link](); } }

7. Member access in packages


Java provides private, default, protected, and public access specifiers to control visibility of
class members. Access control improves encapsulation and security.

8. Java Interfaces
An interface is a collection of abstract methods used to achieve abstraction. Interfaces
support multiple inheritance, loose coupling, flexibility, and scalability.

9. Interface implementation
A class implements an interface using the implements keyword and must override all
abstract methods.

Program:
interface Animal{ void sound(); } class Dog implements Animal{ public void
sound(){ [Link]("Bark"); } } class Test{ public static void
main(String args[]){ Dog d=new Dog(); [Link](); } }

10. Multiple inheritance using interfaces


Java supports multiple inheritance using interfaces. A class can implement multiple
interfaces safely.

Program:
interface A{ void show(); } interface B{ void display(); } class C implements
A,B{ public void show(){ [Link]("A"); } public void display(){
[Link]("B"); } }

11. Errors and Exceptions


Errors are serious system problems that cannot be handled, while exceptions are runtime
problems that can be handled. Exceptions are classified into checked and unchecked
exceptions.

12. Exception handling in Java


Java handles exceptions using try, catch, finally, throw, and throws keywords. Exception
handling prevents abnormal termination and improves reliability.

Program:
try{ int a=10/0; }catch(ArithmeticException e){ [Link](e); }finally{
[Link]("End"); }

13. Common Java Exceptions


Common exceptions include ClassNotFoundException, IOException, ArithmeticException,
NullPointerException, ArrayIndexOutOfBoundsException, NumberFormatException, and
IllegalArgumentException.

14. Multiple catch blocks


Multiple catch blocks allow handling different exceptions separately and must be ordered
from specific to general.

Program:
try{ int a=10/0; }catch(ArithmeticException e){ [Link]("Arithmetic");
}catch(Exception e){ [Link]("General"); }

15. Multithreading
Multithreading allows concurrent execution of multiple threads within a single program. It
improves CPU utilization, performance, and responsiveness.
16. Creating threads in Java
Threads can be created by extending the Thread class or implementing the Runnable
interface.

Program:
class MyThread extends Thread{ public void run(){ [Link]("Thread"); }
public static void main(String args[]){ MyThread t=new MyThread(); [Link](); } }

17. Thread priority


Thread priority determines execution preference. Java priorities range from 1 to 10.

Program:
Thread t=new Thread(); [Link](Thread.MAX_PRIORITY);

You might also like