KEMBAR78
Java Exam Preparation | PDF | Area | Class (Computer Programming)
0% found this document useful (0 votes)
23 views6 pages

Java Exam Preparation

The document provides an overview of key Java concepts including abstract classes, interfaces, and packages. It includes code examples demonstrating the implementation of abstract classes, method overrides, user-defined packages, and multiple inheritance through interfaces. Additionally, it compares abstract classes and interfaces, and showcases various inheritance models with practical examples.

Uploaded by

Harshit Odedra
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)
23 views6 pages

Java Exam Preparation

The document provides an overview of key Java concepts including abstract classes, interfaces, and packages. It includes code examples demonstrating the implementation of abstract classes, method overrides, user-defined packages, and multiple inheritance through interfaces. Additionally, it compares abstract classes and interfaces, and showcases various inheritance models with practical examples.

Uploaded by

Harshit Odedra
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

**7) Abstract Class in Java**

An abstract class in Java is a class that cannot be instantiated and can contain both abstract and non-abstract methods.
Abstract methods do not have a body and must be implemented by subclasses.

```java
abstract class Animal {
abstract void sound();
void eat() {
System.out.println("This animal eats food");
}
}

class Dog extends Animal {


void sound() {
System.out.println("Dog barks");
}
public static void main(String[] args) {
Dog d = new Dog();
d.sound();
d.eat();
}
}
```
**Output:**
```
Dog barks
This animal eats food
```

**8) Methods: equals(), toString(), finalize()**


```java
class Demo {
int id;
Demo(int id) { this.id = id; }

public boolean equals(Object o) {


if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Demo demo = (Demo) o;
return id == demo.id;
}

public String toString() {


return "Demo ID: " + id;
}

protected void finalize() throws Throwable {


System.out.println("Object is garbage collected");
}

public static void main(String[] args) {


Demo d1 = new Demo(1);
Demo d2 = new Demo(1);
System.out.println(d1.equals(d2));
System.out.println(d1);
}
}
```
**Output:**
```
true
Demo ID: 1
```

**9) Interface**
```java
interface Drawable {
void draw();
}

class Circle implements Drawable {


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

public static void main(String[] args) {


Drawable d = new Circle();
d.draw();
}
}
```
**Output:**
```
Drawing Circle
```

**10) Multiple Inheritance using Interface**


```java
interface A {
void showA();
}

interface B {
void showB();
}

class C implements A, B {
public void showA() { System.out.println("A"); }
public void showB() { System.out.println("B"); }

public static void main(String[] args) {


C obj = new C();
obj.showA();
obj.showB();
}
}
```
**Output:**
```
A
B
```

**11) Abstract Class vs Interface**


| Feature | Abstract Class | Interface |
|--------|----------------|-----------|
| Methods | Abstract & concrete | Only abstract (Java 7), default/static (Java 8+) |
| Inheritance | Single | Multiple |
| Fields | Can have instance variables | Only constants |
| Constructor | Yes | No |

**12) Package in Java**


A package is a namespace for organizing classes and interfaces.
**Advantages:**
- Avoids name conflicts
- Easier access control
- Maintains modular code

**13) Creating Package**


1. Declare package: `package mypackage;`
2. Compile: `javac -d . MyClass.java`
3. Use in another class: `import mypackage.MyClass;`

**14) User-Defined Package Example**


```java
// File: mypackage/Message.java
package mypackage;
public class Message {
public void show() {
System.out.println("Hello from package!");
}
}

// File: Test.java
import mypackage.Message;
class Test {
public static void main(String[] args) {
Message msg = new Message();
msg.show();
}
}
```
**Output:**
```
Hello from package!
```

**15) Hierarchical Inheritance Example**


```java
class Animal {
void sound() { System.out.println("Animal sound"); }
}
class Dog extends Animal {
void bark() { System.out.println("Dog barks"); }
}
class Cat extends Animal {
void meow() { System.out.println("Cat meows"); }
}

class Test {
public static void main(String[] args) {
Dog d = new Dog();
Cat c = new Cat();
d.sound();
d.bark();
c.sound();
c.meow();
}
}
```
**Output:**
```
Animal sound
Dog barks
Animal sound
Cat meows
```

**16) Multiple Inheritance using Interface Example**


```java
interface A { void show(); }
interface B { void display(); }
class C implements A, B {
public void show() { System.out.println("Show from A"); }
public void display() { System.out.println("Display from B"); }

public static void main(String[] args) {


C obj = new C();
obj.show();
obj.display();
}
}
```
**Output:**
```
Show from A
Display from B
```

**17) & 18) User Defined Package Use**


(Same as Q14)

**19) Car class with toString()**


```java
class Car {
int topSpeed;
String name;
Car(String name, int topSpeed) {
this.name = name;
this.topSpeed = topSpeed;
}
public String toString() {
return name + " has top speed of " + topSpeed + " km/h";
}
public static void main(String[] args) {
Car[] cars = {
new Car("Ferrari", 350),
new Car("Lamborghini", 340),
new Car("Tesla", 250),
new Car("BMW", 240),
new Car("Audi", 245)
};
for (Car c : cars) {
System.out.println(c);
}
}
}
```
**Output:**
```
Ferrari has top speed of 350 km/h
Lamborghini has top speed of 340 km/h
Tesla has top speed of 250 km/h
BMW has top speed of 240 km/h
Audi has top speed of 245 km/h
```

**20) Abstract Class Shape Example**


```java
abstract class Shape {
abstract double area();
}

class Circle extends Shape {


double radius;
Circle(double r) { radius = r; }
double area() { return Math.PI * radius * radius; }
}

class Rectangle extends Shape {


double length, width;
Rectangle(double l, double w) { length = l; width = w; }
double area() { return length * width; }
}

class Triangle extends Shape {


double base, height;
Triangle(double b, double h) { base = b; height = h; }
double area() { return 0.5 * base * height; }
}

class Test {
public static void main(String[] args) {
Shape s1 = new Circle(5);
Shape s2 = new Rectangle(4, 6);
Shape s3 = new Triangle(3, 7);
System.out.println("Circle Area: " + s1.area());
System.out.println("Rectangle Area: " + s2.area());
System.out.println("Triangle Area: " + s3.area());
}
}
```
**Output:**
```
Circle Area: 78.53981633974483
Rectangle Area: 24.0
Triangle Area: 10.5
```

You might also like