Fiveable
Fiveable

public class

Definition

A public class is one that can be accessed and used by any other class or program. It serves as the blueprint for creating objects and contains variables and methods.

Analogy

Imagine a public class as an open house where anyone can enter freely. Just like how people can visit and interact with different rooms and objects inside an open house, other classes or programs can access and use variables and methods defined within a public class.

Related terms

private class: This refers to a class that can only be accessed within its own file. Other classes cannot directly access its variables or methods.

static keyword: When applied to variables or methods, it means they belong to the class itself rather than instances of the class.

inheritance: Inheritance allows one class (the child) to inherit properties and behaviors from another (the parent).

"public class" appears in:

Practice Questions (13)

  • What is the output of the following code? public class Calculator { public int add(int x, int y) { return x + y; } public int multiply(int x, int y) { return x * y; } public static void main(String[] args) { Calculator calc = new Calculator(); System.out.println(calc.add(5, 6) + calc.multiply(2, 3)); } }
  • What will the following code print out? public class NamePrinter { public void printName(String name) { System.out.println("Your name is: " + name); } public void printAge(int age) { System.out.println("Your age is: " + age); } public static void main(String[] args) { NamePrinter np = new NamePrinter(); np.printName("John"); np.printAge(32); } }
  • What is the result of the following code? public class Shapes { public double calculateCircleArea(double radius) { return Math.PI * Math.pow(radius, 2); } public double calculateRectangleArea(double length, double width) { return length * width; } public static void main(String[] args) { Shapes shapes = new Shapes(); System.out.println(shapes.calculateCircleArea(5) + shapes.calculateRectangleArea(10, 2)); } }
  • What will the following code output? public class Book { public String title; public String author; public int pages; public Book(String bookTitle, String bookAuthor, int bookPages) { title = bookTitle; author = bookAuthor; pages = bookPages; } public void printBookInfo() { System.out.println("Title: " + title); System.out.println("Author: " + author); System.out.println("Pages: " + pages); } public static void main(String[] args) { Book book = new Book("The Great Gatsby", "F. Scott Fitzgerald", 180); book.printBookInfo(); } }
  • What is the output of the following code? public class Employee { public String name; public int salary; public Employee(String empName, int empSalary) { name = empName; salary = empSalary; } public void printEmployeeInfo() { System.out.println("Name: " + name); System.out.println("Salary: $" + salary); } public void giveRaise(int raiseAmount) { salary += raiseAmount; } public static void main(String[] args) { Employee emp = new Employee("John", 50000); emp.printEmployeeInfo(); emp.giveRaise(2000); emp.printEmployeeInfo(); } }
  • Consider the following class definition. public class Animal { private String species; private int age; private double weight; public Animal(String animalSpecies, int animalAge, double animalWeight) { species = animalSpecies; age = animalAge; weight = animalWeight; } public void eat() { weight += 5; } public double getWeight() { return weight; } } Assume that the following code segment appears in a class other than Animal. java Copy code Animal dog = new Animal("Dog", 3, 15); dog.eat(); System.out.println(dog.getWeight()); What is printed as a result of executing the code segment?
  • Consider the following class definition: public class Student { private String name; private int age; private String major; public Student(String studentName, int studentAge, String studentMajor) { name = studentName; age = studentAge; major = studentMajor; } public String getName() { return name; } public int getAge() { return age; } public String getMajor() { return major; } } Assume that the following code segment appears in a class other than Student: Student student = new Student("Alice", 19, "Computer Science"); String name = student.getName(); int age = student.getAge(); String major = student.getMajor(); System.out.println(name + " is " + age + " years old and majoring in " + major + "."); What is printed as a result of executing the code segment?
  • Consider the following code: ``` public class A { public static void doSomething() { System.out.println("Method in A"); } } public class B extends A { public static void doSomething() { System.out.println("Method in B"); } } ``` What will be the output when the following code is executed? ``` A obj = new B(); obj.doSomething(); ```
  • What will be the output when the following code is executed? ``` public class A { public void print() { System.out.println("A"); } } public class B extends A { public void print() { System.out.println("B"); } } public class C extends B { public void print() { System.out.println("C"); } } public class Main { public static void main(String[] args) { A obj = new C(); obj.print(); } } ```
  • Consider the following superclass and subclass declarations: ```java public class Shape { private String color; public Shape(String color) { this.color = color; } public String getColor() { return color; } } public class Circle extends Shape { private double radius; public Circle(String color, double radius) { // Circle constructor implementation goes here } } ```
  • Why does the following code not compile? public class Student { private int gpa; public getGPA() { return gpa; } // Constructor not shown }
  • What does the class header “public class B extends C” mean?
  • If classes E and F are created using the headers “public class E extends C” and “public class F extends C” respectively, what is true about them?


© 2024 Fiveable Inc. All rights reserved.

AP® and SAT® are trademarks registered by the College Board, which is not affiliated with, and does not endorse this website.


© 2024 Fiveable Inc. All rights reserved.

AP® and SAT® are trademarks registered by the College Board, which is not affiliated with, and does not endorse this website.