Inheritance
Single Inheritance: It is the inheritance hierarchy wherein one derived class inherits from one base class.
Multiple Inheritance: It is the inheritance hierarchy wherein one derived class inherits from multiple base class(es)
Hierarchical Inheritance: It is the inheritance hierarchy wherein multiple subclasses inherit from one base class.
Multilevel Inheritance: It is the inheritance hierarchy wherein subclass acts as a base class for other classes.
Hybrid Inheritance: The inheritance hierarchy that reflects any legal combination of other four types of inheritance.
Friend class/function
A friend function of a class is defined outside that class’ scope but it has the right to access all private and protected members of the class. friends are not member functions.
class Distance
{
private:
int meter;
public:
Distance(): meter(0){ }
friend int func(Distance); //friend function
};
Immutable object – state cannot be modified after it is created. Immutability can have a performance cost, since when an object cannot be mutated we need to copy it if we want to write to it.
Immutable objects are often useful because
- they are inherently thread-safe.
- Immutable objects are good Map keys and Set elements, since these typically do not change once created.
- References to immutable objects can be cached as they are not going to change.
Make a class immutable by following these guidelines:
- ensure the class cannot be overridden – make the class final, or use static factories and keep constructors private.
- make fields private and final.
- force callers to construct an object completely in a single step, instead of using a no-argument constructor combined with subsequent calls to setXXX methods
- do not provide any methods which can change the state of the object in any way – not just setXXX methods, but any method which can change state.
- If the instance fields include references to mutable objects, don’t allow those objects to be changed:
- Don’t provide methods that modify the mutable objects.
- Don’t share references to the mutable objects.
Interface
Interface were primarily made popular by Java.
Below are the nature of interface and its C++ equivalents:
- interface can contain only body-less abstract methods; C++ equivalent is pure virtual methods, though they can/cannot have body
- interface can contain only static final data members; C++ equivalent is static const data members which are compile time constants
- Multiple interface can be implemented by a Java class, this facility is needed because a Java class can inherit only 1 class; C++ supports multiple inheritance straight away with help of virtual keyword when needed
Because of point 3 interface concept was never formally introduced in C++. Still one can have a flexibility to do that.
Interface in Java
An interface is a collection of abstract methods. A class implements an interface, thereby inheriting the abstract methods of the interface.
- You cannot instantiate an interface.
- An interface does not contain any constructors.
- All of the methods in an interface are abstract.
- An interface is not extended by a class; it is implemented by a class.
- An interface can extend multiple interfaces.
Interfaces have the following properties:
- An interface is implicitly abstract. You do not need to use the abstract keyword when declaring an interface.
- Each method in an interface is also implicitly abstract, so the abstract keyword is not needed.
- Methods in an interface are implicitly public.
Example
public interface NameOfInterface
{
//Any number of final, static fields
//Any number of abstract method declarations\
}
When overriding methods defined in interfaces there are several rules to be followed:
- Checked exceptions should not be declared on implementation methods other than the ones declared by the interface method or subclasses of those declared by the interface method.
- The signature of the interface method and the same return type or subtype should be maintained when overriding the methods.
- An implementation class itself can be abstract and if so interface methods need not be implemented.
When implementation interfaces there are several rules:
- A class can implement more than one interface at a time.
- A class can extend only one class, but implement many interfaces.
- An interface can extend another interface, similarly to the way that a class can extend another class.
Implements
/* File name : Animal.java */
interface Animal {
public void eat();
public void travel();
}
* File name : MammalInt.java */
public class MammalInt implements Animal{
public void eat(){
System.out.println("Mammal eats");
}
public void travel(){
System.out.println("Mammal travels");
}
public int noOfLegs(){
return 0;
}
public static void main(String args[]){
MammalInt m = new MammalInt();
m.eat();
m.travel();
}
}
Extends
public interface Sports
{
public void setHomeTeam(String name);
public void setVisitingTeam(String name);
}
//Filename: Hockey.java
public interface Hockey extends Sports
{
public void homeGoalScored();
public void visitingGoalScored();
public void endOfPeriod(int period);
public void overtimePeriod(int ot);
}
The Hockey interface has four methods, but it inherits two from Sports; thus, a class that implements Hockey needs to implement all six methods.
Extending Multiple Interfaces:
public interface Hockey extends Sports, Event
NOTE
class Sub extends Super implements interface // extends before implements
Final
Final can be:
- variable
- method
- class
final variable that have no value it is called blank final variable or uninitialized final variable. It can be initialized in the constructor or by calling this() only. The blank final variable can be static also which will be initialized in the static block only.
Static blocks are also called Static initialization blocks . A static initialization block is a normal block of code enclosed in braces, { }, and preceded by the static keyword.
Example:
class A{
static final int data; //static blank final variable
static { data=50;}
public static void main(String args[]){
System.out.println(A.data);
}
}
Final variable
If you make any variable as final, you cannot change the value of final variable
Final Method
If you make any method as final, you cannot override it. It can be inherited.
Constructor can’t be final as they are never inherited.
Final Class
If you make any class as final, you cannot extend it.
Final Parameter
If you declare any parameter as final, you cannot change the value of it.
Const Vs Final
A const object can only call const methods, and is generally considered immutable.
const Person* person = myself; person = otherPerson; //Valid... unless we declared it const Person* const! person->setAge(20); //Invalid, assuming setAge isn't a const method (it shouldn't be)
A final object cannot be set to a new object, but it is not immutable – there is nothing stopping someone from calling any set methods.
final Person person = myself; person = otherPerson; //Invalid person.setAge(20); //Valid!
Java has no inherent way of declaring objects immutable; you need to design the class as immutable yourself.
When the variable is a primitive type, final/const work the same.
const int a = 10; //C++ final int a = 10; //Java a = 11; //Invalid in both languages
FEW POINTS
- Final member variable must be initialized at the time of declaration or inside constructor.
- Local final variable must be initializing during declaration.
- All variable declared inside java interface are implicitly final.
- final class can not be abstract in java.
- Final methods are bonded during compile time also called static binding.
- Making a class, method or variable final in Java helps to improve performance because JVM gets an opportunity to make assumption and optimization.
- Making a collection reference variable final means only reference can not be changed but you can add, remove or change object inside collection.
Stack Unwinding
When an exception occurs, the function call stack is linearly searched for the exception handler, and all the entries before the function with exception handler are removed from the function call stack.
When an exception is thrown and control passes from a try block to a handler, the C++ run time calls destructors for all automatic objects constructed since the beginning of the try block. The automatic objects are destroyed in reverse order of their construction.
Issues related to stack unwinding:
- you should never throw an exception before any existing exception has been handled. This means that the stack unwinding process should never throw an exception







