Ejemplo 1: clase abstracta en java
Sometimes we may come across a situation wherewe cannot provide
implementation to all the methods in a class. We want to leave the
implementation to a classthat extends it. In such case we declare a classasabstract.To make a classabstract we use key word abstract.
Any classthat contains one or more abstract methods isdeclaredasabstract.
If we don’t declare classasabstract 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 classwhich extends abstarct classmustoverride all methods of abstractclass4)An abstarct classcan contain either 0or more abstract method.
Ejemplo 2: ¿Qué son los métodos abstractos en Java?
An abstract method isthe method which does’nt have any body.
Abstract method isdeclared with
keyword abstractand semicolon in place of method body.publicabstractvoid<method name>();
Ex :publicabstractvoidgetDetails();
It isthe responsibility of subclass to provide implementation to
abstractmethod defined inabstractclass
Ejemplo 3: abstracción en java
Abstraction isdefinedashidinginternal implementation and showing only
necessary information.// abstract classabstractclassAddition// abstract methodspublicabstractintaddTwoNumbers(int number1,int number2);publicabstractintaddFourNumbers(int number1,int number2,int number3,int number4);// non-abstract methodpublicvoidprintValues()
System.out.println("abstract class printValues() method");classAbstractMethodExampleextends Addition
publicintaddTwoNumbers(int number1,int number2)return number1 + number2;publicintaddFourNumbers(int number1,int number2,int number3,int number4)return number1 + number2 + number3 + number4;publicstaticvoidmain(String[] args)Additionadd=newAbstractMethodExample();
System.out.println(add.addTwoNumbers(6,6));
System.out.println(add.addFourNumbers(8,8,3,2));add.printValues();
Ejemplo 4: cómo hacer un método abstracto en java
publicabstractclassAccount//abstract class //perent classprotectedint accountNumber;protectedCustomer customerObj;protecteddouble balance;//constructorpublicAccount(int saccountNumber,Customer scustomerObj,double sbalance)
accountNumber = saccountNumber;
customerObj = scustomerObj;
balance = sbalance;// abstract Functionpublicabstractbooleanwithdraw(double amount);publicclassSavingsAccountextends Account // child classprivatedouble minimumBalance;// constructorpublicSavingsAccount(int saccountNumber,Customer scustomerObj,double sbalance,double sminimumBalance)super(saccountNumber, scustomerObj, sbalance);
minimumBalance = sminimumBalance;// Implementation of abstract function in child classpublicbooleanwithdraw(double amount)if(balance()> minimumBalance &&balance()- amount > minimumBalance)
super.setBalance(balance()- amount);returntrue;elsereturnfalse;
Ejemplo 5: clase abstracta de Java
// abstract classabstractclassShape// abstract methodabstractvoidsides();classTriangleextends Shape
voidsides()
System.out.println("Triangle shape has three sides.");classPentagonextends Shape
voidsides()
System.out.println("Pentagon shape has five sides.");publicstaticvoidmain(String[] args)Triangle obj1 =newTriangle();
obj1.sides();Pentagon obj2 =newPentagon();
obj2.sides();
Ejemplo 6: escriba un programa en el que se esté definiendo una clase abstracta que contenga un método abstracto omputer (int a, int b) y un método no abstracto también
abstractclassSumpublicabstractintcompute(int a,int b);publicvoiddisp()
System.out.println("Method of class Sum");classDemoextends Sumpublicintcompute(int a,int b)return a+b;publicstaticvoidmain(String args[])Sum obj =newDemo();
System.out.println(obj.compute(3,7));
obj.disp();
Si te gusta la programación, puedes dejar una crónica acerca de qué te ha gustado de este ensayo.
¡Haz clic para puntuar esta entrada!
(Votos: 0 Promedio: 0)