Saltar al contenido

interfaz y clase abstracta en el ejemplo de código java

Al fin luego de mucho luchar hemos hallado el resultado de esta cuestión que ciertos los usuarios de nuestro sitio han tenido. Si tienes algún dato que aportar no dudes en compartir tu información.

Ejemplo 1: diferencia entre abstracto e interfaz

Interface1)Interface contains only abstract methods 
2)AccessSpecifiersfor methods in interface
must be public3)Variables defined must be public,static,final4)MultipleInheritance in java is implemented
using interface5)To implement an interface we use
implements keyword

AbstractClass1)Abstractclass can contain abstract methods,
concrete methods or both
2)Exceptprivate we can have any access
specifier for methods in abstractclass.3)Exceptprivate variables can have any access
specifiers
4)We cannot achieve multiple inheritance using
abstractclass.5)To implement an interface we use implements
keyword

Ejemplo 2: clase abstracta en java

Sometimes we may come across a situation where we cannot provide 
implementation toall the methods in a class. We want toleave the 
implementation toaclass that extendsit. In such case we declare a class
as abstract.To make a classabstract we use key word abstract. 
Anyclass 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)Anyclass which extends abstarct class must override all methods of abstractclass4)An abstarct class can contain either 0 or more abstract method.

Ejemplo 3: ¿Qué son los métodos abstractos en Java?

Anabstract method is the method which does’nt have any body. 
Abstract method is declared withkeywordabstract and semicolon in place of method body.publicabstractvoid<method name>();Ex:publicabstractvoidgetDetails();It is the responsibility of subclass toprovide implementation toabstract method defined in abstractclass

Ejemplo 4: clase abstracta vs interfaz

Interfaces specify what a class must do and not how. 
It is the blueprint of the class.
It is used toachieve total abstraction. 

We are using implements keyword forinterface.

Abstract=Sometimes we may come across a situation
where we cannot provide implementation toall the methods in a class. We want toleave the 
implementation toaclass that extendsit.
  In that case we declare a class
as abstract by using abstract keyword on method
signature.In my framework I have created my
PageBaseclass as superclass of the all page classes. 
I have collected all common elements
and functions into PageBaseclass and
all other page classes extent PageBaseclass.
By doing so,I don't have tolocate very
common WebElements and it providesreusability in my framework.
Also1)Abstract classes cannot be instantiated
2)An abstarct classes contains abstract method,
concrete methods or both.3)Anyclass which extends abstarct class must
  override all methods of abstractclass4)An abstarct class can contain either
  0 or more abstract method.

Ejemplo 5: resumen de Java

abstractclassPesanpublicvoidsuccess()System.out.println("Mobil Berhasil Dibeli");publicvoiderror()System.out.println("Uang Anda Tidak Cukup");classCarextendsPesanprotectedString nama ="toyota supra";protectedString warna ="merah";protectedint harga =2000000000;protectedString brand ="toyota";classShowRoomextendsCarprotectedString namaShowroom ="Catur Sentosa Raya";protectedString alamatShowroom ="Jl.siliwangin kec pancoranmas kota depok 16436";classPembeliextendsShowRoomprotectedString namaPembeli ="anto jayabaya";protectedString alamatPembeli ="jl.swadaya rt.01/rw.04 no.112 kec pancoranmas kota depok";protectedint saldoPembeli =50000000;classBeliMobilextendsPembelipublicBeliMobil(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;voidgetResult(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("=======================");elseSystem.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);publicstaticvoidmain(String[] args)BeliMobil beli =newBeliMobil("avanza","hitam",128000000,"toyota","Jaya Mobil","Jakarta","Anton","Depok",228000000);
   beli.getResult(beli.nama, beli.warna, beli.harga, beli.brand, beli.namaPembeli, beli.alamatPembeli);

¡Haz clic para puntuar esta entrada!
(Votos: 0 Promedio: 0)



Utiliza Nuestro Buscador

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *