Object Oriented Programming Flashcards
In OOP what does object mean
A real world entity such as a pen, chair, table, computer, watch, etc.
Define Object-Oriented Programming
A methodology or paradigm to design a program using classes & objects
Object-Oriented programming simplifies software development & maintenance by providing these 6 concepts
Object, Classes, Inheritance, Polymorphism, Abstraction, Abstraction, & Encapsulation
List the six concepts of OOP in order from lowest to highest, also see the object oriented programming system diagram
Object, Class, Inheritance, Polymorphism, Abstraction, Encapsulation
What is an object?
Any entity that has state and behavior is known as an object. For example a chair, pen, table, keyboard, bike, etc. It can be physical or logical.
An Object can be defined as an instance of a class. An object contains an address and takes up some space in memory. Objects can communicate without knowing the details of each other’s data or code. The only necessary thing is the type of message accepted and the type of response returned by the objects.
Example: A dog is an object because it has states like color, name, breed, etc. as well as behaviors like wagging the tail, barking, eating, etc.
What is a class in the context of Java?
A class, in the context of Java, are templates that are used to create objects, and to define object data types and methods. Core properties include the data types and methods that may be used by the object
A class can also be defined as a blueprint from which individual objects are created. Class doesn’t consume any space.
List two advantages of OOPs over Procedure Oriented Programming?
- OOPs makes development and maintenance easier whereas in a procedure-oriented programming language it is not easy to manage if code grows as project size increases.
- OOPs provides data hiding whereas in a procedure-oriented programming language a global data can be accessed from anywhere.
What is a constructor in Java?
In Java, a constructor is a block of codes similar to the method. It is called when an instance of the object is created, and memory is allocated for the object.
It is a special type of method which is used to initialize the object.
When is a constructor called?
When an object is created, compiler makes sure that constructors for all of its subobjects (its member and inherited objects) are called. If members have default constructors or constructor without parameter then these constructors are called automatically, otherwise parameterized constructors can be called using initializer list.
Note: It is called constructor because it constructs the values at the time of object creation. It is not necessary to write a constructor for a class. It is because java compiler creates a default constructor if your class doesn’t have any.
Rules to remember while creating a constructor
What are the three rules defined for the constructor?
- Constructor name must be the same as its class name
- A Constructor must have no explicit return type
- A Java constructor cannot be abstract, static, final, and synchronized
What are the two types of constructors
- Default constructor
2. Parameterized constructor
What is a default constructor
The default constructor is a no-args constructor that the Java compiler inserts on your behalf; it contains a default call to super(); which is the default behavior. If you implement any constructor then you no longer receive a default constructor.
Note : If there is no constructor in the class, the compiler adds a default constructor
No-Argument Constructor public Bicycle() { gear = 1; cadence = 10; speed = 0; } Bicycle yourBike = new Bicycle(); invokes the no-argument constructor to create a new Bicycle object called yourBike.
What is a parameterized constructor?
A constructor which has a specific number of parameters is called a parameterized constructor.
The parameterized constructor is used to provide different values to the distinct objects. However, you can provide the same values also.
class Student4{ int id; String name; Student4(int i,String n){ id = i; name = n; } void display(){System.out.println(id+" "+name);}
public static void main(String args[]){ Student4 s1 = new Student4(111,"Karan"); Student4 s2 = new Student4(222,"Aryan"); s1.display(); s2.display(); } } In the above example, we have created the constructor of Student class that have two parameters. We can have any number of parameters in the constructor.
What is Constructor Overloading?
In Java, a constructor is just like a method but without return type. It can also be overloaded like Java methods.
Constructor overloading in Java is a technique of having more than one constructor with different parameter lists. They are arranged in a way that each constructor performs a different task. They are differentiated by the compiler by the number of parameters in the list and their types.
class Student5{ int id; String name; int age; //creating two argument constructor Student5(int i,String n){ id = i; name = n; } //creating three argument constructor Student5(int i,String n,int a){ id = i; name = n; age=a; } void display(){System.out.println(id+" "+name+" "+age);}
public static void main(String args[]){ Student5 s1 = new Student5(111,"Karan"); Student5 s2 = new Student5(222,"Aryan",25); s1.display(); s2.display(); } }
Constructor vs Method, list five differences for each
Constructor :
- A constructor is used to initialize the state of an object.
- A constructor must not have a return type.
- The constructor is invoked implicitly.
- The Java compiler provides a default constructor if you don’t have any constructor in a class.
- The constructor name must be same as the class name.
Method :
- A method is used to expose the behavior of an object.
- A method must have a return type.
- The method is invoked explicitly.
- The method is not provided by the compiler in any case.
- The method name may or may not be same as class name.
What are the four pillars of OOP?
- Inheritance
- Polymorphism
- Encapsulation
- Abstraction
What is Inheritance, what is the idea behind inheritance, & what does Inheritance represent?
- Inheritance in Java is a mechanism in which one object acquires all the properties and behaviors of a parent object. It is an important part of OOPs (Object Oriented programming system).
- The idea behind inheritance in Java is that you can create new classes that are built upon existing classes. When you inherit from an existing class, you can reuse methods and fields of the parent class. Moreover, you can add new methods and fields in your current class also.
- Inheritance represents the IS-A relationship which is also known as a parent-child relationship.
What are the two advantages of Inheritance?
- For Method Overriding (so runtime polymorphism can be achieved).
- For Code Reusability.
Define the term class, as used in inheritance
Class: A class is a group of objects which have common properties. It is a template or blueprint from which objects are created.
Define the term sub class/child class, as used in inheritance
Sub Class/Child Class: Subclass is a class which inherits the other class. It is also called a derived class, extended class, or child class.
Define the term Super Class/Parent Class, as used in inheritance
Super Class/Parent Class: Superclass is the class from where a subclass inherits the features. It is also called a base class or a parent class.
Define the term reusability, as used in inheritance
Reusability: As the name specifies, reusability is a mechanism which facilitates you to reuse the fields and methods of the existing class when you create a new class. You can use the same fields and methods already defined in the previous class.
class Subclass-name extends Superclass-name { //methods and fields added here }
In the above example, what does the extends keyword indicate?
The extends keyword indicates that you are making a new class that derives from an existing class. The meaning of “extends” is to increase the functionality.
class Employee{ float salary=40000; } class Programmer extends Employee{ int bonus=10000; public static void main(String args[]){ Programmer p=new Programmer(); System.out.println("Programmer salary is:"+p.salary); System.out.println("Bonus of Programmer is:"+p.bonus); } }
In the above example, what class is programmer, & what is employee, & what is the relationship between the two classes?
As displayed in the above figure, Programmer is the subclass and Employee is the superclass. The relationship between the two classes is Programmer IS-A Employee. It means that Programmer is a type of Employee.
On the basis of class, there can be three types of inheritance in java, what are they?
Also list two additional types which are supported through interface only
- Single (ex. class b -> class a)
- Multilevel (ex. class c -> class b -> class a)
- Hierarchical (ex class b & class c -> class a)
- Multiple (ex class C -> Class a & Class b)
- Hybrid (ex class d -> class b & class c -> class a)
How is multiple & hybrid inheritance supported in Java programming?
Through interface only
class Animal{ void eat(){System.out.println("eating...");} } class Dog extends Animal{ //Single Inheritance void bark(){System.out.println("barking...");} } class TestInheritance{ public static void main(String args[]){ Dog d=new Dog(); d.bark(); d.eat(); }}
What type of inheritance is the above code an example of?
Single Inheritance
class Animal{ void eat(){System.out.println("eating...");} } class Dog extends Animal{ //Level 1 - Inheritance void bark(){System.out.println("barking...");} } class BabyDog extends Dog{ //Level 2 - Inheritance void weep(){System.out.println("weeping...");} } class TestInheritance2{ public static void main(String args[]){ BabyDog d=new BabyDog(); d.weep(); d.bark(); d.eat(); }}
What type of inheritance is the above code an example of?
Multilevel Inheritance
class Animal{ void eat(){System.out.println("eating...");} } class Dog extends Animal{ // Hierarchical Inheritance void bark(){System.out.println("barking...");} } class Cat extends Animal{ // Hierarchical Inheritance void meow(){System.out.println("meowing...");} } class TestInheritance3{ public static void main(String args[]){ Cat c=new Cat(); c.meow(); c.eat(); //c.bark(); //Error }}
What type of inheritance is the above code an example of?
Hierarchical Inheritance