0% found this document useful (0 votes)
9 views2 pages

Abstract Class - Example 1

Uploaded by

Juan Dela Cruz
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 views2 pages

Abstract Class - Example 1

Uploaded by

Juan Dela Cruz
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/ 2

Animal.

java

public abstract class Animal {

// Abstract method (no implementation)

abstract void makeSound();

// Concrete method

void sleep() {

System.out.println("Zzzz...");

Dog.java

public class Dog extends Animal {

@Override

void makeSound() {

System.out.println("Woof! Woof!");

Cat.java

public class Cat extends Animal {

@Override

void makeSound() {

System.out.println("Meow!");

=====================================================================================
AbstractExample.java – Main

public class AbstractExample {

public static void main(String[] args) {

Dog myDog = new Dog();

Cat myCat = new Cat();

// Invoking methods

myDog.makeSound();

myDog.sleep();

myCat.makeSound();

myCat.sleep();

Output

You might also like