Basic Flashcards
Name the 4 OOP principles.
Abstraction
Polymorphism
Inheritance
Encapsulation
What is Polymorphism?
Polymorphism is the
- provision of single interface which can be implemented by different classes to provide their customized implementation
or - use of single symbol to represent multiple different types
Polymorphism has two types: static (Compile time) and dynamic (runtime).
Explain the types of Polymorphism.
- Compile-time/static
The implementation to call is determined at compile-time. This is divided into two types:
(i) Function Overloading: multiple functions, same name but different number/types of parameters
(ii) Operator overloading: Using â+â to add two numbers and also to concatenate two strings
2. Run-time/dynamic The implementation to call is determined at run-time. There is only one type: (i) Function overriding: parent and child class have methods is exact same signature.
Can we overload static/final/private methods in same class?
Yes we can.
We cannot âoverrideâ static/final/private methods anywhere.
Can we override static/final/private methods of parent class?
No.
It is possible to override parent classâs static methods as in we can have methods with exact same signatures in both parent and child class, but Java doesnât call it overriding, as static methods are tied to the class as a whole and inheritance deals with providing customized behaviors for objects.
Final declaration by default does not allow overriding anywhere.
Private methods of parent class will not be visible outside and cannot be overridden.
Explain output.
class Animal { protected void getData() { System.out.println("Inside Animal"); } } class Dog extends Animal { protected void getData() { System.out.println("Inside Dog"); } }
public class Test { public static void main(String[] args) { Animal animal = new Dog(); animal.getData(); } }
Can we write this:
Dog dog = new Animal ( );
Reference variable of parent class Animal is pointing to object of child class Dog, hence the method implementation by class Dog will be called.
===========================================
We cannot write Dog dog = new Animal ( ); as it will give compiler error âType mismatch: cannot convert from Animal to Dogâ
With ânew Dog( )â we are assigning the Dog object type to Animal type, which is valid as Dog IS-A Animal.
With ânew Animal( )â we are assigning Animal object type to Dog type, which is not valid for inheritance (IS-A relationship).
Explain output.
class Employee { void foo ( ) { System.out.println("Employee"); } }
class Manager extends Employee { void foo ( ) { System.out.println("Manager"); }
public static void main(String [ ] args) { Manager john = new Employee( ); john.foo ( ); } }
Compilation error (type mismatch) --------------------------- The object john is of type Employee (superclass) but referes to Manager (subclass), which violates the IS-A relationship.
Explain output.
class Animal { protected void run ( ) { System.out.println("Animal runs"); } } class Dog extends Animal { protected void eat ( ) { System.out.println("Dog eats"); }
protected void leap() { System.out.println("Dog leaps"); } }
public class Test { public static void main(String [ ] args) { Animal animal = new Dog(); animal.leap ( ); } }
Although the reference variable of parent class is pointing to object of child class, the method leap ( ) does not exist for parent class.
Explain output.
interface Animal { void run ( ); void leap ( ); }
abstract class Dog implements Animal { void eat ( ) { System.out.println (" Dog eats "); } }
public class Corgi extends Dog { public void run ( ) { System.out.println("Corgi runs"); } public void leap ( ) { System.out.println("Corgi leaps"); }
public static void main ( String [ ] args) { Dog dog = new Corgi ( ); dog.leap ( ); } }
Dog implements Animal interface, hence all the methods in Animal replicate for class Dog.
As the reference variable of parent class Dog points to object of child class Corgi, child classâs method implementation will be called.
Can we overload main() method?
Yes, we can overload main() method by passing different number/types of arguments to it. The JVM will call public static void main (String[] args) as its main method even though we overload it.
Why is method overloading not possible by changing only the return type of method?
Two otherwise similar methods having int and double as their respective return types will confuse JVM at the time of method calling and hence we cannot have only return type as a way of method overloading.
Explain output.
class Demo{ void test ( ) { System.out.println("No parameter"); }
void test (int a, int b) { System.out.println("a and b: "+a+" and "+b); }
void test (double a) { System.out.println("a: "+a); }
public static void main(String[] args){ test(); test(10,20); test(88); test(12.3); } }
No parameter a and b: 10 and 20 a: 88 a: 12.3 ----------------------------- When the test() method has only one input (88) and there is no exact match for this int parameter, Java will automatically type convert and call the test(double a) method.
What is upcasting?
When reference variable is of parent class and object is of child class, it is called upcasting. ParentClass var = new ChildClass();
Vice versa is not possible.
What is Inheritance?
It is the process by which one object (child) acquires properties of another object (parent).
The class which provides basic attributes to be inherited is called Parent Class/Superclass/Base Class.
The class which inherits the above class is called Child Class/Subclass/Derived Class.
Inheritance makes it possible for subclass to define only the unique qualities, as general attributes can be inherited from superclass.
How many superclasses can one subclass have?
Only one
Can a subclass act as a superclass to other classes?
Yes
Can a class be superclass of itself?
No
Can a subclass access private member of its superclass?
No
Is this valid?
class Super{ //class body }
class Sub extends Super{ //class body }
class Test{
public static void main (String[] args){ Super sup = new Super(); Sub sub = new Sub(); sup=sub; // 1 sub=sup; //2 } }
1 is valid, 2 will give compile-time type mismatch error
What can you access with below objects?
- Superclass sup = new Superclass();
- Superclass sup = new Subclass();
- Subclass sub = new Subclass();
- Subclass sub = new Superclass();
- Objects defined by Superclass only
- Objects defined by Superclass only
- All the objects defined by Superclass and Subclass except Superclassâs private objects
- Compilation error (type mismatch: cannot convert from Superclass to Subclass)
How to access method of a superclass?
Using super.methodName()
Where should call to superclassâs constructor be placed?
On the first line inside subclassâs constructor
Explain output.
class A{ void show () { System.out.println("Inside A"); }
class B extends A{ void show (String msg) { System.out.println(msg); }
class Test{ public static void main(String[] args){ B obj = new B(); obj.show("Inside B"); obj.show(); } }
Inside B
Inside A
âââââ
The methods are not overridden here, as method overridding needs methods with exact same signature in parent and child class. This is simply method overloading.
How JVM determines which overridden method to be called at runtime?
From the type of object (not the type of reference variable)