KEMBAR78
Java | PDF | Method (Computer Programming) | Class (Computer Programming)
0% found this document useful (0 votes)
9 views7 pages

Java

Uploaded by

kefep22283
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)
9 views7 pages

Java

Uploaded by

kefep22283
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/ 7

Here is a short and clear explanation of each Java concept you mentioned:

1. Difference between Class and Object

 Class: A blueprint or template (e.g., Car).


 Object: An instance of a class (e.g., Car myCar = new Car();).

2. Encapsulation

 Binding data (variables) and methods in one unit (class).

class Student {
private int age; // private data

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


public int getAge() { return age; }
}

3. Inheritance

 One class inherits features of another.


 Types:
o Single
o Multilevel
o Hierarchical
o Hybrid (via interfaces)

4. Polymorphism

 One task, many forms.


 Method Overloading: Same method name, different parameters.

class Add {
int sum(int a, int b) { return a + b; }
int sum(int a, int b, int c) { return a + b + c; }
}

5. Abstraction

 Hiding internal details, showing only functionality.

abstract class Shape {


abstract void draw();
}

class Circle extends Shape {


void draw() { System.out.println("Drawing Circle"); }
}

6. Access Modifiers

Modifier Class Package Subclass World


private ✔ ✘ ✘ ✘
default ✔ ✔ ✘ ✘
protected ✔ ✔ ✔ ✘
public ✔ ✔ ✔ ✔

7. Abstract Class vs Interface

 Abstract Class: Can have method implementations.


 Interface: Only method declarations (Java 8+ allows default methods).

abstract class Animal {


abstract void sound();
}

interface Flyable {
void fly();
}

8. == vs .equals()

 ==: Compares references (memory addresses).


 .equals(): Compares content (values).

9. Method Overloading Example


class Print {
void show(int a) { System.out.println(a); }
void show(String s) { System.out.println(s); }
}

10. Abstract Class Use Case

 When you want to define a common base with shared code and abstract methods.

abstract class Vehicle {


abstract void start();
}

11. Inheritance Example


class Animal {
void sound() { System.out.println("Animal sound"); }
}

class Dog extends Animal {


void bark() { System.out.println("Dog barks"); }
}

Let me know if you'd like this as a PDF or formatted notes for study!

5 marks

Here is a detailed explanation for each Java topic, including theoretical concepts and sample
programs where required:

1. Main Features of Java

 Platform Independent (via JVM)


 Object-Oriented
 Simple and Secure
 Robust and Portable
 Multithreaded
 Architecture Neutral
 High Performance
 Dynamic

2. Object-Oriented Programming in Java

Java follows OOP principles:

 Class & Object


 Encapsulation (data hiding)
 Inheritance (code reuse)
 Polymorphism (method behavior change)
 Abstraction (hiding complexity)

3. Difference: JDK vs JRE vs JVM


Component Description
JVM Java Virtual Machine, runs bytecode
JRE Java Runtime Environment (JVM + libraries)
JDK Java Development Kit (JRE + development tools)

4. Constructor in Java

 Special method called when object is created.

class Student {
String name;
Student(String n) { name = n; }
}

5. Method Overloading vs Method Overriding

Feature Overloading Overriding


Class Same class Subclass
Parameters Must differ Must be same
Runtime Compile-time Runtime

6. Use of static Keyword

 Belongs to the class, not instance.

class Example {
static int count = 0;
}

7. Inheritance Example
class Animal {
void sound() { System.out.println("Animal sound"); }
}
class Dog extends Animal {
void bark() { System.out.println("Dog barks"); }
}

8. Interface in Java

 A reference type with abstract methods (no body).

interface Drawable {
void draw();
}
class Circle implements Drawable {
public void draw() { System.out.println("Drawing Circle"); }
}

9. Use of final Keyword

 final variable: constant value.


 final method: cannot be overridden.
 final class: cannot be extended.

10. Java Program to Check Prime Number


import java.util.Scanner;

public class PrimeCheck {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
boolean isPrime = true;
for(int i = 2; i <= num/2; i++) {
if(num % i == 0) {
isPrime = false; break;
}
}
System.out.println(isPrime ? "Prime" : "Not Prime");
}
}

11. Access Modifiers in Java

Modifier Scope
private Class only
(default) Package
protected Package + subclass
public Everywhere

12. Reverse a String in Java


import java.util.Scanner;

public class ReverseString {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String input = sc.nextLine();
String reversed = new StringBuilder(input).reverse().toString();
System.out.println("Reversed: " + reversed);
}
}
13. Java Switch-based Calculator
import java.util.Scanner;

public class Calculator {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter two numbers:");
double a = sc.nextDouble(), b = sc.nextDouble();
System.out.println("Enter operator (+, -, *, /):");
char op = sc.next().charAt(0);

switch(op) {
case '+': System.out.println(a + b); break;
case '-': System.out.println(a - b); break;
case '*': System.out.println(a * b); break;
case '/': System.out.println(a / b); break;
default: System.out.println("Invalid operator");
}
}
}

14. Count Vowels and Consonants


import java.util.Scanner;

public class VowelConsonantCount {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String input = sc.nextLine().toLowerCase();
int vowels = 0, consonants = 0;

for(char c : input.toCharArray()) {
if(Character.isLetter(c)) {
if("aeiou".indexOf(c) != -1) vowels++;
else consonants++;
}
}

System.out.println("Vowels: " + vowels + ", Consonants: " +


consonants);
}
}

15. Copy One Array to Another


public class ArrayCopy {
public static void main(String[] args) {
int[] original = {1, 2, 3, 4};
int[] copy = new int[original.length];

for(int i = 0; i < original.length; i++) {


copy[i] = original[i];
}
for(int val : copy) System.out.print(val + " ");
}
}

16. Student Info Entry System for Presidency University


import java.util.Scanner;

public class StudentInfo {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);

System.out.print("Enter name: ");


String name = sc.nextLine();
System.out.print("Enter course: ");
String course = sc.nextLine();
System.out.print("Enter gender: ");
String gender = sc.nextLine();
System.out.print("Enter age: ");
int age = sc.nextInt();
System.out.print("Enter total fees: ");
double fees = sc.nextDouble();
System.out.print("Fees paid (true/false): ");
boolean paid = sc.nextBoolean();

System.out.println("\n--- Student Information ---");


System.out.println("Name: " + name);
System.out.println("Course: " + course);
System.out.println("Gender: " + gender);
System.out.println("Age: " + age);
System.out.println("Total Fees: ₹" + fees);
System.out.println("Fees Paid: " + (paid ? "Yes" : "No"));
}
}

Let me know if you'd like all this compiled into a PDF file, notes format, or if you want
explanations in Hindi as well.

You might also like