Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
84 views

Java Assignment

The document discusses different Java concepts including access modifiers, inheritance, and exception handling. It provides examples to illustrate: 1) The three main access modifiers in Java - private, protected, and public - and how they restrict access to classes, methods and variables. 2) The different types of inheritance in Java - single, multilevel, hierarchical, multiple and hybrid - and provides code examples of each. 3) Key aspects of exception handling in Java using try-catch blocks, finally blocks, and the throw and throws keywords, along with examples demonstrating their usage.

Uploaded by

Nemo K
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
84 views

Java Assignment

The document discusses different Java concepts including access modifiers, inheritance, and exception handling. It provides examples to illustrate: 1) The three main access modifiers in Java - private, protected, and public - and how they restrict access to classes, methods and variables. 2) The different types of inheritance in Java - single, multilevel, hierarchical, multiple and hybrid - and provides code examples of each. 3) Key aspects of exception handling in Java using try-catch blocks, finally blocks, and the throw and throws keywords, along with examples demonstrating their usage.

Uploaded by

Nemo K
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

2. a.

Different access modifiers in Java


i. Private Access Modifier

When variables and methods are declared private, they cannot be accessed outside of the class.

class Data {
// private variable
private String name;
}

public class Main {


public static void main(String[] main){
// create an object of Data
Data d = new Data();
// access private variable and field from another class
d.name = "Nemo"; // This will return an error
}
}
Output:
ii. Protected Access Modifier

When methods and data members are declared protected, we can access them within the same
package as well as from subclasses.

class Animal {
// protected method
protected void display() {
System.out.println("I am an animal");
}
}

class Main extends Animal {


public static void main(String[] args) {

// create an object of Dog class


Dog dog = new Dog();
// access protected method
dog.display();
}
}

Output:
iii. Public Access Modifier

When methods, variables, classes, and so on are declared public, then we can access them from
anywhere. The public access modifier has no scope restriction.

Animal.java

// public class
public class Animal {
// public variable
public int legCount;

// public method
public void display() {
System.out.println("I am an animal.");
System.out.println("I have " + legCount + " legs.");
}
}

Main.java

public class Main {


public static void main( String[] args ) {
// accessing the public class
Animal animal = new Animal();

// accessing the public variable


animal.legCount = 4;
// accessing the public method
animal.display();
}
}
Output:
2. b. Different types of Inheritance in Java
i. Single Inheritance

// Java program to illustrate the


// concept of single inheritance
import java.io.*;
import java.lang.*;
import java.util.*;

class one {
public void print_hello()
{
System.out.println("Hello");
}
}

class two extends one {


public void print_world() { System.out.println("World"); }
}
// Driver class
public class Main {
public static void main(String[] args)
{
two g = new two();
g.print_hello();
g.print_world();
}
}

Output:
ii. Multilevel Inheritance

import java.io.*;
import java.lang.*;
import java.util.*;

class one {
public void print_hello()
{
System.out.println("Hello");
}
}

class two extends one {


public void print_world() { System.out.println("world"); }
}

class three extends two {


public void print_hello()
{
System.out.println("Hello");
}
}

// Drived class
public class Main {
public static void main(String[] args)
{
three g = new three();
g.print_hello();
g.print_world();
}
}

Output:
iii. Hierarchical Inheritance

class A {
public void print_A() { System.out.println("Class A"); }
}

class B extends A {
public void print_B() { System.out.println("Class B"); }
}

class C extends A {
public void print_C() { System.out.println("Class C"); }
}

class D extends A {
public void print_D() { System.out.println("Class D"); }
}

// Driver Class
public class Main {
public static void main(String[] args)
{
B obj_B = new B();
obj_B.print_A();
obj_B.print_B();

C obj_C = new C();


obj_C.print_A();
obj_C.print_C();

D obj_D = new D();


obj_D.print_A();
obj_D.print_D();
}
}

Output:
iv. Multiple Inheritance

import java.io.*;
import java.lang.*;
import java.util.*;

interface one {
public void print_hello();
}

interface two {
public void print_world();
}

interface three extends one, two {


public void print_hello();
}
class child implements three {
@Override public void print_hello()
{
System.out.println("Hello");
}

public void print_world() { System.out.println("world"); }


}

// Drived class
public class Main {
public static void main(String[] args)
{
child c = new child();
c.print_hello();
c.print_world();
}
}

Output:
v. Hybrid Inheritance

class C
{
public void disp()
{
System.out.println("C");
}
}

class A extends C
{
public void disp()
{
System.out.println("A");
}
}

class B extends C
{
public void disp()
{
System.out.println("B");
}

class D extends A
{
public void disp()
{
System.out.println("D");
}
public static void main(String args[]){

D obj = new D();


obj.disp();
}
}

Output:
2. c. Exception Handling in Java
i. Exception Handling with try-catch block

class Main {
public static void main(String[] args) {

try {

// code that generate exception


int divideByZero = 5 / 0;
System.out.println("Rest of code in try block");
}

catch (ArithmeticException e) {
System.out.println("ArithmeticException => " + e.getMessage());
}
}
}

Output:
ii. Exception Handling with finally

class Main {
public static void main(String[] args) {
try {
// code that generates exception
int divideByZero = 5 / 0;
}

catch (ArithmeticException e) {
System.out.println("ArithmeticException => " + e.getMessage());
}

finally {
System.out.println("This is the finally block");
}
}
}

Output:
iii. Exception Handling with throw

class Main {
public static void divideByZero() {

// throw an exception
throw new ArithmeticException("Trying to divide by 0");
}

public static void main(String[] args) {


divideByZero();
}
}

Output:
iv. Exception Handling with throws

import java.io.*;

class Main {
// declareing the type of exception
public static void findFile() throws IOException {

// code that may generate IOException


File newFile = new File("test.txt");
FileInputStream stream = new FileInputStream(newFile);
}

public static void main(String[] args) {


try {
findFile();
}
catch (IOException e) {
System.out.println(e);
}
}
}

Output:

You might also like