Search Java Code Snippets

  Help us in improving the repository. Add new snippets through 'Submit Code Snippet ' link.

#Java - Code Snippets for '#Overriding' - 3 code snippet(s) found

 Sample 1. Tricky Code for Overloading and Overriding

class BuggyBread1 {
public String method() {
return "Base Class - BuggyBread1";

class BuggyBread2 extends BuggyBread1{

private static int counter = 0;

public String method(int x) {
return "Derived Class - BuggyBread2";

public static void main(String[] args) {
BuggyBread1 bg = new BuggyBread2();
System.out.println(bg.method()); // prints Base Class - BuggyBread1

   Like      Feedback     overloading   overriding   tricky code examples  inheritance

 Sample 2. Using Pojomatic for overriding equals,hashcode and toString methods

import org.pojomatic.Pojomatic;
import org.pojomatic.annotations.AutoProperty;

public class Employee {
   public String name;
   public int age;
   public int salary;

   public String getName() {
      return name;

   public void setName(String name) { = name;

   public int getAge() {
      return age;

   public void setAge(int age) {
      this.age = age;

   public int getSalary() {
      return salary;

   public void setSalary(int salary) {
      this.salary = salary;

   public int hashCode() {
      return Pojomatic.hashCode(this);

   public boolean equals(Object other) {
      return Pojomatic.equals(this, other);

   public String toString() {
      return Pojomatic.toString(this);

   Like      Feedback     pojomatic  toString method  toString  hashcode method  equals method   overrding equals method  overriding hashcode method  overrding tostring method  @autoproperty  pojomatic autoproperty

 Sample 3. Overriding equals method

public class ClassInfoBean {
public String url;

public boolean equals(Object o) {
if (this.url.equals(((ClassInfoBean)o).url)){
return true;
return false;

   Like      Feedback     equals  overriding equals method   @override

Subscribe to Java News and Posts. Get latest updates and posts on Java from
Enter your email address:
Delivered by FeedBurner