An ADT is simply a base class that has one or more pure-virtual methods. A pure-virtual method is similar to a virtual method except that the ADT need not provide any implementation for that method. Even if it does provide a generic implementation, all derivatives must override that method, even if only to call the generic base class method. This ensures that all derivatives provide their own specialised implementations. A derivative that does not provide an implementation for all the pure-virtual methods it inherits becomes an ADT itself. However any implementations that it does provide can subsequently be inherited by its derivatives (those methods effectively become virtual methods rather than a pure-virtual method). Only classes that provide or inherit a complete implementation of all pure-virtual methods can physically be instantiated.
Going back to our shape class, the shape::draw() method is an ideal candidate for a pure-virtual function since it is impossible to draw a shape without knowing what type of shape it is. However, a circle class would be expected to know how to draw itself thus it can provide the specific implementation for drawing circles. Similarly, rectangles and triangles will provide their own specific implementations. This then makes it possible to store a collection of generic shapes and have them draw themselves without the need to know their actual type; as with all virtual methods, the derivatives behave according to their actual type.
Note that static classes have no need for any constructors, operators or destructors (they have no this pointer since there can never be any instances of the class), thus the compiler-generated default constructor, copy constructor, destructor and assignment operator must all be declared private. Some languages may do this automatically but in C++ (which has no concept of static classes) you must explicitly declare them as such.
While static classes do have their uses, bear in mind that the point of having a class in the first place is in order to instantiate independent objects from the class. Thus the only time you should ever use a static class is when the class data members must exist for the entire duration of the program and you want to ensure there can only ever be one instance of those members. However, for memory efficiency alone, a singleton class would be the preferred method. Moreover, if some or all of the static methods are closely associated with a generic class, then it makes more sense to encapsulate those methods as static members of that generic class rather than as a completely separate static class.
Abstract Classes in Detail:
An Abstract class is a special kind of class that cannot be instantiated. It has one or more methods which are not implemented in the class. These methods are declared abstract and they do not contain any code inside them.
Ex:
abstract class Parent {
public abstract String getSon();
public abstract String getDaughter();
....
....
//More methods that contain specific behaviour/code in them
}
The above is an abstract class "Parent" that has a lot of functionality but it has declared two abstract methods which have no code inside them. Any class that has one or more abstract methods has to be abstract. This abstract class cannot be instantiated.
i.e., the below piece of code will not work. The code will not even compile.
Parent object = new Parent();
Purpose of Abstract Classes:
Abstract classes are generally used where you want an amount of behaviour to be used by the class that extends the abstract class while at the same time giving options to the child class to provide a certain amount of behaviour itself.
A Child Class extending the Abstract Class:
public class Child extends Parent {
public String getSon() {
return "Sons Name";
}
public String getDaughter(){
return "Daughters Name";
}
...
... //Code specific to the Child class
}
Static:
You cannot have Static Classes. You can only have static variables and methods in a class.
Differences:Abstract class can also contain method definitions but an interface can contain only declarationsAll variables in an interface are by default public static and final whereas in Abstract class it is notAn interface can be considered as a pure abstract class that contains no method implementations and contains only declarations.
Any class which has one or more abstract methods is called an abstract class. But in the normal class we can't have any abstract methods. We cannot create an object for the abstract classes. When we inherit the abstract class we should implement the abstract method which we inherit.
Association class is describing the relationship between classes. An abstract class is just 1 class, provides some abstraction behaviors that may be (but do not have to) derived, overridden from.
Abstract class is built to promote inheritance whereas a final class is built to avoid inheritanceAn Abstract class can be extended by another class whereas a final class cannot be extended
We can't instantiate both interfaces and abstract classes.The only one difference between them is that an interface can't contain concrete(fully defined) methods where as an abstract class may contain them.An abstract class not necessarily contain abstract methods. we can make a class as abstract class even it does not has any abstract methods.When there is a need to write both abstract and concrete methods in a single unit we have to use an abstract class instead of an interface since an interface cant contain concrete methods.All the fields(or properties) of an interface are by default 'static final' even when you don't mention explicitly. And all methods are 'public abstract'.But in an abstract class we can have any type of fields and methods.
20
Differences:Abstract class can also contain method definitions but an interface can contain only declarationsAll variables in an interface are by default public static and final whereas in Abstract class it is notAn interface can be considered as a pure abstract class that contains no method implementations and contains only declarations.
Any class which has one or more abstract methods is called an abstract class. But in the normal class we can't have any abstract methods. We cannot create an object for the abstract classes. When we inherit the abstract class we should implement the abstract method which we inherit.
Association class is describing the relationship between classes. An abstract class is just 1 class, provides some abstraction behaviors that may be (but do not have to) derived, overridden from.
There is no difference with method declaration and implementation between abstract and non-abstract classes. You do the exact same thing when writing a concrete method in either an abstract or non-abstract class.
Abstract class is built to promote inheritance whereas a final class is built to avoid inheritanceAn Abstract class can be extended by another class whereas a final class cannot be extended
Abstract data types or abstract base classes.
We can't instantiate both interfaces and abstract classes.The only one difference between them is that an interface can't contain concrete(fully defined) methods where as an abstract class may contain them.An abstract class not necessarily contain abstract methods. we can make a class as abstract class even it does not has any abstract methods.When there is a need to write both abstract and concrete methods in a single unit we have to use an abstract class instead of an interface since an interface cant contain concrete methods.All the fields(or properties) of an interface are by default 'static final' even when you don't mention explicitly. And all methods are 'public abstract'.But in an abstract class we can have any type of fields and methods.
An interface can only have abstract methods or constants in it. A class can have both that and everything else in Java.
The classes which have one or more abstract methods are abstract. To declare a class as abstract, use the abstract keyword in front of the class keyword, before the class declaration. Abstract classes cannot be instantiated. Similarly the new keyword cannot be used to create an object of the abstract class. Remember that the constructors and static variables cannot be declared as abstract. Any subclass of an abstract class must either implement all of the abstract methods in the superclass or be itself declared abstract.
Static data is data that does not change from program load to program exit. Static data member do not apply for c. In c++, a static data member is one that is common for all instances of that class.
Because of the following reasons:static - If a constructor is static, an object instance cannot invoke it to initialize itself (Because static members are not linked to an object)abstract - because an abstract class cannot be instantiated and hence it will not have a constructor. If you make a concrete class's constructor abstract - it cannot be instantiated. eitherways it makes no sensefinal - a constructor cannot be final (thats the way java is designed)