| Forums.Sureshkumar.net : A Perfect Place to Share Knowledge Blogs Games Magazines |
|
|
#1 (permalink) |
|
Junior Member
Join Date: Feb 2006
Location:
Posts: 3
Thanks: 0 Thanked 0 Times in 0 Posts Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 3
|
hi friends, i had one doubt regarding abstract classes.any abstract class may have concretemethods without any abstract methods.so even it is having only concrete methods why we can't create an object of abstract class? reply soon bye swathi |
|
|
|
|
|
#2 (permalink) |
|
Senior Member
Join Date: Feb 2006
Location: India
Posts: 546
Thanks: 7 Thanked 7 Times in 5 Posts Thanks: 7
Thanked 7 Times in 5 Posts
Rep Power: 8
|
hi swathi when u r creating a abstract class it means that u can't create obj for that.hence u can't create obj for concrete methods too. if u need to access concrete methods of abstract class extend abstract class in another class create obj for sub class access concrete methods thro subclass obj.
__________________
try try try till u succeed meenakshi
|
|
|
|
|
|
#3 (permalink) |
|
Moderator
Join Date: Feb 2006
Posts: 1,413
Thanks: 0 Thanked 9 Times in 8 Posts Thanks: 0
Thanked 9 Times in 8 Posts
Rep Power: 18
|
hello
Abstract class must be extended/subclassed (to be useful). It serves as a template. A class that is abstract may not be instantiated (ie, you may not call its constructor), abstract class may contain static data. Any class with an abstract method is automatically abstract itself, and must be declared as such. A class may be declared abstract even if it has no abstract methods. This prevents it from being instantiated. abstract class must contain atleast one abstract method rest might be concrete /abstract . Abstract class must have subclasses so as meena said u need to extend abstract class in another class [/b]
__________________
http://livetolead.blogspot.com/ all the best Arise Awake N Stop Not Until Ur Goal Is Reached! |
|
|
|
|
|
#4 (permalink) |
|
Moderator
Join Date: Feb 2006
Posts: 1,413
Thanks: 0 Thanked 9 Times in 8 Posts Thanks: 0
Thanked 9 Times in 8 Posts
Rep Power: 18
|
hello
raed this A class is called abstract if it has at least one abstract (not implemented) method. The keyword abstract has to be placed in the definition of the method(s) and the class itself. For example, the following class has three concrete and one abstract method: Listing 1: Abstract class Person public abstract class Person { public void changeAddress(String address){ System.out.println("New address is" + address); } private void giveDayOff(){ System.out.println("Class Person: Adding a day off"); } public void promote(int percent){ System.out.println("Class Person: Promoting a worker..."); giveDayOff(); //calling an abstract method raiseSalary(percent); } public abstract boolean raiseSalary (int percent); } Abstract classes cannot be instantiated, but they allow you to create superclasses, which implement some of the functionality, while leaving one or more methods to be implemented in subclasses. The class Person can contain dozens of concrete methods that are the same for every person like changeAddress and giveDayOff, but since the process of raising salary is different for employees and consultants, the method raiseSalary should remain abstract. Please note that even though this method is abstract, it could be called in an abstract class because by the time the concrete class will be instantiated, the method will be already implemented. Since we have two type of workers, let’s create subclasses Employee and Consultant, and implemented the method raiseSalary based on different rules. Listing 2: Class Employee, version 1 public class Employee extends Person{ public boolean raiseSalary(int percent){ if (percent<10){ // The code to increase // the salary goes here System.out.println( "Class Employee:Increasing salary by "+ percent + "%"); return true; } else{ System.out.println( "Our limit is 10%, sorry..."); return false; } } } Listing 3: Class Consultant, version 1 public class Consultant extends Person{ public boolean raiseSalary(int percent){ // The code to increase the // hourly rate goes here System.out.println( "Class Consultant: Increasing hourly rate by "+ percent + "%"); return true; } } Designer of the class Person may not know specifics of the raising salary process, which does not stop him from calling the method raiseSalary. Programmers writing subclasses are forced to write implementation of this method according to its signature declared in the abstract class. If they declare a method raiseSalary with a different argument list, this will be considered a method overloading and the subclass will remain abstract. The class Promoter in the Listing 4 shows how to use our classes Employee and Consultant for promoting workers.
__________________
http://livetolead.blogspot.com/ all the best Arise Awake N Stop Not Until Ur Goal Is Reached! |
|
|
|
|
|
#5 (permalink) |
|
Moderator
Join Date: Feb 2006
Posts: 1,413
Thanks: 0 Thanked 9 Times in 8 Posts Thanks: 0
Thanked 9 Times in 8 Posts
Rep Power: 18
|
hello
if u ahve doubts u can ask again bye sowmya
__________________
http://livetolead.blogspot.com/ all the best Arise Awake N Stop Not Until Ur Goal Is Reached! |
|
|
|
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|