[Go to site: main page, start]

0% found this document useful (0 votes)
6 views3 pages

Java Abstract Classes Examples

The document provides examples of abstract classes in Java programming. It illustrates how abstract classes can define methods that must be implemented by subclasses, as seen in the examples of 'Animal' and 'MotorBike'. Additionally, it shows how concrete classes like 'Dog', 'SportsBike', and 'MountainBike' implement these abstract methods.

Uploaded by

mitchellekoech31
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views3 pages

Java Abstract Classes Examples

The document provides examples of abstract classes in Java programming. It illustrates how abstract classes can define methods that must be implemented by subclasses, as seen in the examples of 'Animal' and 'MotorBike'. Additionally, it shows how concrete classes like 'Dog', 'SportsBike', and 'MountainBike' implement these abstract methods.

Uploaded by

mitchellekoech31
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Abstract Classes: Example 1

// Online Java Compiler


// Use this editor to write, compile and run your Java code online

class Main extends Language{


public static void main(String[] args) {
Main obj=new Main();
[Link]();
}
}

abstract class Language{


public void display(){
[Link]("This is Java Programming");
}
}

Abstract Class: Example 2

// Online Java Compiler


// Use this editor to write, compile and run your Java code online

class Main {
public static void main(String[] args) {
Dog d1=new Dog();
[Link]();
[Link]();
}
}

class Dog extends Animal{


public void makeSound(){
[Link]("Bark Bark");
}
}

abstract class Animal{


abstract void makeSound();
public void eat(){
[Link]("I can eat");
}
}

Example 3
abstract class MotorBike {
abstract void brake();
}

class SportsBike extends MotorBike {

// implementation of abstract method


public void brake() {
[Link]("SportsBike Brake");
}
}

class MountainBike extends MotorBike {

// implementation of abstract method


public void brake() {
[Link]("MountainBike Brake");
}
}
class Main {
public static void main(String[] args) {
MountainBike m1 = new MountainBike();
[Link]();
SportsBike s1 = new SportsBike();
[Link]();
}
}

You might also like