Java OOP Code Samples: Shapes, Resizable, Inner Classes
Java OOP Code Samples: Shapes, Resizable, Inner Classes
Summary: This page presents several Java object-oriented examples in one place. Examples include abstract classes for shapes, a resizable Rectangle via an interface, nested inner classes, a custom exception, and a small package usage example. The original code is preserved as a preformatted block below.
Complete Java Examples (original content preserved)
abstract class Shape {
abstract double calculateArea();
abstract double calculatePerimeter();
}
class Circle extends Shape {
private double radius;
public Circle(double radius) {
this.radius = radius;
}
@Override
double calculateArea() {
return Math.PI * radius * radius;
}
@Override
double calculatePerimeter() {
return 2 * Math.PI * radius;
}}
class Triangle extends Shape {
private double side1;
private double side2;
private double side3;
public Triangle(double side1, double side2, double side3) {
this.side1 = side1;
this.side2 = side2;
this.side3 = side3;
}
@Override
double calculateArea() {
double s = (side1 + side2 + side3) / 2;
return Math.sqrt(s * (s - side1) * (s - side2) * (s - side3));
}
@Override
double calculatePerimeter() {
return side1 + side2 + side3;
}}
public class ShapeDemo {
public static void main(String[] args) {
Circle circle = new Circle(5);
System.out.println("Circle Area: " + circle.calculateArea());
System.out.println("Circle Perimeter: " + circle.calculatePerimeter());
Triangle triangle = new Triangle(3, 4, 5);
System.out.println("\nTriangle Area: " + triangle.calculateArea());
System.out.println("Triangle Perimeter: " + triangle.calculatePerimeter());
}}
interface Resizable {
void resizeWidth(int width);
void resizeHeight(int height);
}
class Rectangle implements Resizable {
private int width;
private int height;
public Rectangle(int width, int height) {
this.width = width;
this.height = height;
}
@Override
public void resizeWidth(int width) {
this.width = width;
}
@Override
public void resizeHeight(int height) {
this.height = height;
}
@Override
public String toString() {
return "Rectangle (width: " + width + ", height: " + height + ")";
}}
public class ResizableDemo {
public static void main(String[] args) {
Rectangle rectangle = new Rectangle(10, 20);
System.out.println("Original Rectangle: " + rectangle);
rectangle.resizeWidth(15);
rectangle.resizeHeight(25);
System.out.println("Resized Rectangle: " + rectangle);
}}
class Outer {
void display() {
System.out.println("Outer class display");
}
class Inner {
void display() {
System.out.println("Inner class display");
}
}}}
public class OuterInnerDemo {
public static void main(String[] args) {
Outer outer = new Outer();
outer.display();
Outer.Inner inner = outer.new Inner();
inner.display();
}}
class DivisionByZeroException extends Exception {
public DivisionByZeroException(String message) {
super(message);
}
}
public class CustomExceptionDemo {
public static void main(String[] args) {
int numerator = 10;
int denominator = 0;
try {
if (denominator == 0) {
throw new DivisionByZeroException("Division by zero is not allowed.");
}
int result = numerator / denominator;
System.out.println("Result of division: " + result);
} catch (DivisionByZeroException e) {
System.err.println("Exception: " + e.getMessage());
} finally {
System.out.println("Finally block executed.");
}}}
/////////mypack/MyPackageClass.java
package mypack;
public class MyPackageClass {
public void display() {
System.out.println("Displaying from MyPackageClass");
}
}
Create a main Java file MainClass.java in a separate directory:
///////////// MainClass.java
import mypack.MyPackageClass;
public class MainClass {
public static void main(String[] args) {
MyPackageClass myPackageObj = new MyPackageClass();
myPackageObj.display();
}}}Notes and Corrections
- Preserved content: All original lines are preserved exactly in the block above for reference and copy/paste.
- Formatting: The code is presented inside a <pre><code> block to preserve indentation and special characters.
- Spelling and grammar: Non-code descriptive text in the page has been corrected for capitalization and readability; the original code text is unchanged inside the block.
- SEO: Page metadata (title, description, keywords) is optimized for discoverability without altering the sample code.
Usage
Copy the desired sections from the preformatted block into your Java IDE or text editor. If you run individual classes with main methods, place them into separate .java files as shown in the code block. For the package example, ensure proper directory structure (mypack) and compile accordingly.
Author: Code sample compilation. License: Use as example code for learning and testing.
