Joh Raul
9
Q:

abstract class java

An abstract method is the method which does’nt have any body. 
Abstract method is declared with
keyword abstract and semicolon in place of method body.

  public abstract void <method name>();
Ex : public abstract void getDetails();
It is the responsibility of subclass to provide implementation to 
abstract method defined in abstract class
2
// abstract class
abstract class Shape
{
   // abstract method
   abstract void sides();
}
class Triangle extends Shape
{
   void sides()
   {
      System.out.println("Triangle shape has three sides.");
   }
}
class Pentagon extends Shape
{
   void sides()
   {
      System.out.println("Pentagon shape has five sides.");
   }
   public static void main(String[] args)
   {
      Triangle obj1 = new Triangle();
      obj1.sides();
      Pentagon obj2 = new Pentagon();
      obj2.sides();
   }
}
3
Sometimes we may come across a situation where we cannot provide 
implementation to all the methods in a class. We want to leave the 
implementation to a class that extends it. In such case we declare a class
as abstract.To make a class abstract we use key word abstract. 
Any class that contains one or more abstract methods is declared as abstract. 
If we don’t declare class as abstract which contains abstract methods we get 
compile time error.
  
  1)Abstract classes cannot be instantiated
  2)An abstarct classes contains abstract method, concrete methods or both.
  3)Any class which extends abstarct class must override all methods of abstract
    class
  4)An abstarct class can contain either 0 or more abstract method.    
1
//abstract parent class
abstract class Animal{
   //abstract method
   public abstract void sound();
}
//Dog class extends Animal class
public class Dog extends Animal{

   public void sound(){
	System.out.println("Woof");
   }
   public static void main(String args[]){
	Animal obj = new Dog();
	obj.sound();
   }
}
1
// Abstract class
abstract class Animal {
  // Abstract method (does not have a body)
  public abstract void animalSound();
  // Regular method
  public void sleep() {
    System.out.println("Zzz");
  }
}

// Subclass (inherit from Animal)
class Pig extends Animal {
  public void animalSound() {
    // The body of animalSound() is provided here
    System.out.println("The pig says: wee wee");
  }
}

class MyMainClass {
  public static void main(String[] args) {
    Pig myPig = new Pig(); // Create a Pig object
    myPig.animalSound();
    myPig.sleep();
  }
}
 
 
 
5
public abstract class GraphicObject {

   abstract void draw();
}
2
/**
 * Simple Java program to prove that abstract class can have constructor in Java.
 * @author http://java67.blogspot.com
 */
public class AbstractConstructorTest {

    public static void main(String args[]) {
       Server server = new Tomcat("Apache Tomcat");
       server.start();
    }
}

abstract class Server{
    protected final String name;
   
    public Server(String name){
        this.name = name;
    }
   
    public abstract boolean start();
}

class Tomcat extends Server{
   
    public Tomcat(String name){
        super(name);
    }

    @Override
    public boolean start() {
       System.out.println( this.name + " started successfully");
       return true;
    }
   
}

Output:
Apache Tomcat started successfully

0
abstract class Pesan {
   public void success() {
     System.out.println("Mobil Berhasil Dibeli");
   }
   
   public void error() {
     System.out.println("Uang Anda Tidak Cukup");
   }
}
 
class Car extends Pesan {
    protected String nama = "toyota supra";
    protected String warna = "merah";
    protected int harga = 2000000000;
    protected String brand = "toyota";
}
 
class ShowRoom extends Car {
  protected String namaShowroom = "Catur Sentosa Raya";
  protected String alamatShowroom = "Jl.siliwangin kec pancoranmas kota depok 16436";
}
 
class Pembeli extends ShowRoom {
    protected String namaPembeli = "anto jayabaya";
    protected String alamatPembeli = "jl.swadaya rt.01/rw.04 no.112 kec pancoranmas kota depok";
    protected int saldoPembeli = 50000000;
}
 
class BeliMobil extends Pembeli {
 
 public BeliMobil(String nama, String warna, int harga, String brand, String nsr, String asr, String np, String ap, int sdp) {
    super();
    
    super.nama = nama;
    super.warna = warna;
    super.harga = harga;
    super.brand = brand;
    super.namaShowroom = nsr;
    super.alamatShowroom = asr;
    super.namaPembeli = np;
    super.alamatPembeli = ap;
    super.saldoPembeli = sdp;
 }
    
void getResult(String nama, String warna, int harga, String brand, String np, String ap) {
    
     if(super.harga > super.saldoPembeli) {
       System.out.println("=======================");
       super.error();
       System.out.println("=======================");
     } else {
       System.out.println("=======================");
       super.success();
       System.out.println("=======================");
       System.out.println("");
       System.out.println("=======================");
       System.out.println("Jenis Mobil");
       System.out.println("=======================");
       System.out.println("");
       System.out.println("Nama Mobil:" + nama);
       System.out.println("Warna Mobil:" + warna);
       System.out.println("Harga Mobil:" + harga);
       System.out.println("Brand Mobil:" + brand);
       System.out.println("");
       System.out.println("=======================");
       System.out.println("Nama Pembeli Mobil");
       System.out.println("=======================");
       System.out.println("Nama Pembeli:" + np);
       System.out.println("Nama Pembeli:" + ap);
     }
  }
    
 public static void main(String[] args) {
   BeliMobil beli = new BeliMobil("avanza", "hitam", 128000000, "toyota", "Jaya Mobil", "Jakarta", "Anton", "Depok", 228000000);
   beli.getResult(beli.nama, beli.warna, beli.harga, beli.brand, beli.namaPembeli, beli.alamatPembeli);
  }
}
 
0
interface methods{
    public void hey();
    public void bye();
}

//unable to implement all the abstract methods in the interface so 
// the other class automatically becomes abstract
abstract class other implements methods{
    public void hey(){
        System.out.println("Hey");
    }
}

//able to implement all the methods so is not abstract
class scratch implements methods {
    public void hey(){
        System.out.println("Hey");
    }
    public void bye() {
        System.out.println("Hey");
    }
}
0

New to Communities?

Join the community