Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
5 views44 pages

OOPS - Unit-2

The document contains multiple Java programming examples demonstrating concepts such as inheritance, method overloading, method overriding, abstract classes, interfaces, exception handling, and custom exceptions. Each section includes code snippets illustrating the respective concepts, along with explanations of their functionality. The examples cover various aspects of object-oriented programming in Java.
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)
5 views44 pages

OOPS - Unit-2

The document contains multiple Java programming examples demonstrating concepts such as inheritance, method overloading, method overriding, abstract classes, interfaces, exception handling, and custom exceptions. Each section includes code snippets illustrating the respective concepts, along with explanations of their functionality. The examples cover various aspects of object-oriented programming in Java.
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/ 44

1

2
class Employee{
String Name = "Raj";
private int empid=1201;
void work(){
System.out.println("This is a work() present in Employee class ");
}
}
class Manager extends Employee{
int age = 30;
void assignWork(){
System.out.println("This is a assignWork() in Manager class ");
System.out.println();
System.out.println(Name);
//System.out.println(empid);// error
}
}
public class InheritanceDemo {
public static void main(String[] args) {
Employee e1 =new Employee();
System.out.println("Name = " + e1.Name);
e1.work();
Manager m1 = new Manager();
System.out.println("Name = " + m1.Name);
System.out.println("Age = " + m1.age);
m1.work();
m1.assignWork();
}
}

3
4
5
6
7
public class MethodOverloading {
public static void main(String[] args) {
MethodOverloadingDemo obj1 = new MethodOverloadingDemo();
obj1.add(10,20);
obj1.add(10,20,30);
}
}
class MethodOverloadingDemo{
void add(int a, int b){
int sum = a+b;
System.out.println("sum = " + sum);
}
void add(int a, int b, int c){
int sum = a+b+c;
System.out.println("sum = " + sum);
}
}

8
//Method Overriding and Hierarchical inheritance
class Shape
{
float area()
{
return 0;
}
}
class Square extends Shape
{
float area(int s)
{
return s * s;
}
}
class Circle extends Shape
{
float area(int r)
{
return (3.14f * r * r);
}
}
public class MethodOverriding
{
public static void main(String[] args)
{
float area;
Square obj1 = new Square();
area = obj1.area(2);
System.out.println("Area of the Square = " + area);
Circle obj2 = new Circle();
area = obj2.area(4);
System.out.println("Area of the Circle = " + area);
}
}

9
10
class A1{
final float pi=3.14f;
final void display(){
System.out.println("This is a final method");
System.out.println("This method cant be overridden");
//pi=4.34f;
}
}
final class B1 extends A1{
// void display(){
// System.out.println("this is display method without final key word");
// }
void displayB1(){
System.out.println("this is display method of Class B1");
}
}
//class C1 extends B1{
//
//}
public class FinalDemo {
public static void main(String[] args) {
B1 obj1 = new B1();
System.out.println("pi = " + obj1.pi);
//obj1.pi = 23.6f;
obj1.display();
obj1.displayB1();
}
}

11
12
abstract class Person{
abstract void display();
//non abstract method
static void addition()
{
int a=20, b=67;
System.out.println("sum = "+ (a+b));
}
}
class Student1 extends Person{
void display(){
System.out.println("This is is the implementation of display method of abstract class");
}
}
public class AbstractDemo {
public static void main(String[] args) {
// Person p1 = new Person() ;// Abstract method cant be instatiated
Student1 s1 = new Student1();
s1.display();
Person.addition(); // non abstract static method
}
}

13
14
class A
{
int i=20;
void display()
{
System.out.println("display() in Class A");
}
}
class B extends A
{
int i=30;
void display()
{
System.out.println("display() in Class B");
}
void my_display()
{
display();
super.display();
System.out.println("Sub1 i= "+i);
System.out.println("Super1 i= "+super.i);
}
}
public class SuperDemo {
public static void main(String args[])
{
B obj1=new B();
obj1.my_display();
obj1.display();
}
}

15
class Student {
int no, marks;
Student() {
System.out.println("Super class constructor");
}
Student(int marks) {
this.marks=marks;
System.out.println("marks= " + marks);
}
}
class Topper extends Student {
Topper() {
System.out.println("Derived Class Constructor");
}
Topper(int marks) { // Student(marks)
super(marks);
}
Topper(double cgpa)
{
System.out.println("CGPA = " + cgpa);
}
}
public class SuperClassConstructorDemo extends Student {
public static void main(String args[]) {
Topper obj1 = new Topper();
Topper obj2 = new Topper(30);
Topper obj3 = new Topper(9.8);
}
}

16
17
18
interface RBI{
void rateOfInterest(); //No body ; abstract method
// void display(){
// }
}
class ICICIBank implements RBI{
public void rateOfInterest(){
System.out.println("ICICI Bank rate of interest = 5%");
}
}
class SBIBank implements RBI{
public void rateOfInterest(){
System.out.println("SBI Bank rate of Interest = 6%");
}
}
public class InterfaceDemo{
public static void main(String[] args) {
ICICIBank b1 = new ICICIBank();
// RBI r1 = new RBI();// INterface cant be instatiated
b1.rateOfInterest();
SBIBank b2 = new SBIBank();
b2.rateOfInterest();
}
}

19
interface IF1{
void show();
}
interface IF2 extends IF1{
void print();
}
class AIF implements IF2{
public void print(){
System.out.println(" this is print method in AIF2 class");
}
public void show(){
System.out.println(" this is show method in AIF2 class");
}
}
public class InterfaceDemo2 {
public static void main(String[] args) {
AIF ob = new AIF();
ob.print();
ob.show();
}
}

20
interface TestInterface{
// abstract method
public void square(int a);

// default method
default void show(){
System.out.println("This is the Default Method.");
}
}
class DefaultMethodDemo implements TestInterface{
// implementation of square abstract method
public void square(int a){
System.out.println("Square of a = " + a*a);
}
public static void main(String args[]){
DefaultMethodDemo d = new DefaultMethodDemo();
d.square(4);
// default method executed
d.show();
}
}

21
22
interface ElectricCar{
// Abstract methods
void drive();
void electricKit();
}
interface PetrolCar{
// Abstract methods
void drive();
void petrolKit();
}
// Multiple Inheritance using Interface
class HybridCar implements ElectricCar, PetrolCar {
public void drive(){
System.out.println("Driving a Hybrid Car");
}
public void electricKit(){
System.out.println("Using the Electric kit for Hybrid Car");
}
public void petrolKit(){
System.out.println("Using the Petrol kit for Hybrid Car");
}
}
public class MultipleInheritance {
public static void main(String[] args) {
HybridCar c1 = new HybridCar();
c1.drive();
c1.electricKit();
c1.petrolKit();
}
}

23
24
25
26
27
28
29
30
31
32
33
34
35
public class ExceptionDemo {
public static void main(String[] args) {
// System.out.println("result = " + (10/0)); // ArithmeticException
try{
System.out.println("result = " + (10/0));
}
catch (ArithmeticException e){
System.out.println("Arithmetic Exception is occured, since number is divided by
zero");
// System.out.println("Exception is" + e);
}
}
}

36
public class ExceptionDemo2 {
public static void main(String[] args) {
int[] Array1 = {10, 20, 30};
System.out.println(Array1[0]);
System.out.println(Array1[1]);
System.out.println(Array1[2]);
// Array1[3]=40; //ArrayIndexOutOfBoundsException
try{
Array1[3]=40; //ArrayIndexOutOfBoundsException
}
catch(ArrayIndexOutOfBoundsException e){
System.out.println("ArrayIndexOutOfBounds exception is occured, size of an array is
crossed");
}
finally {
System.out.println("End of the Exception");
}
System.out.println("End of the program");
}
}

37
public class ThrowDemo {
static void checkAge(int age) {
if (age < 18) {
throw new ArithmeticException("Access denied - You must be at least 18 years old
to vote.");
}
else {
System.out.println("Access granted - You are old enough to vote!");
}
}

public static void main(String[] args) {


checkAge(15);
System.out.println("end"); //will not execute
}
}

38
import java.io.IOException;

public class ThrowsDemo {


void display() throws IOException {
System.out.println("No exception was found till now");
// throw new IOException("Error"); // try with commenting this line
}
public static void main(String args[]) {
try {
ThrowsDemo t=new ThrowsDemo();
t.display();
}
catch(Exception e) {
System.out.println("Now the exception occured");
}
System.out.println("End");
}

39
40
41
42
43
//Creating InvalidAgeException as user-defined exception
class InvalidAgeException extends Exception {
public InvalidAgeException (String str) {
// calling the constructor of parent class - Exception
super(str);
}
}

// class that uses custom exception InvalidAgeException


public class CustomExceptionDemo {
static void validate (int age) throws InvalidAgeException {
if(age < 18){
// throw an object of user defined exception
throw new InvalidAgeException("age is not valid to vote");
}
else {
System.out.println("welcome to vote");
}
}
public static void main(String args[]) {
try {
// calling the method
validate(13);
}
catch (InvalidAgeException ex) {
System.out.println("Caught the exception");
// printing the message from InvalidAgeException object
System.out.println("Exception occured: " + ex);
}
}
}

44

You might also like