Object Oriented Programming Flashcards

1
Q

In OOP what does object mean

A

A real world entity such as a pen, chair, table, computer, watch, etc.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Define Object-Oriented Programming

A

A methodology or paradigm to design a program using classes & objects

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Object-Oriented programming simplifies software development & maintenance by providing these 6 concepts

A

Object, Classes, Inheritance, Polymorphism, Abstraction, Abstraction, & Encapsulation

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

List the six concepts of OOP in order from lowest to highest, also see the object oriented programming system diagram

A

Object, Class, Inheritance, Polymorphism, Abstraction, Encapsulation

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is an object?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is a class in the context of Java?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

List two advantages of OOPs over Procedure Oriented Programming?

A
  1. 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.
  2. OOPs provides data hiding whereas in a procedure-oriented programming language a global data can be accessed from anywhere.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is a constructor in Java?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

When is a constructor called?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Rules to remember while creating a constructor

What are the three rules defined for the constructor?

A
  1. Constructor name must be the same as its class name
  2. A Constructor must have no explicit return type
  3. A Java constructor cannot be abstract, static, final, and synchronized
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What are the two types of constructors

A
  1. Default constructor

2. Parameterized constructor

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is a default constructor

A

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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is a parameterized constructor?

A

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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is Constructor Overloading?

A

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();      }   }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Constructor vs Method, list five differences for each

A

Constructor :

  1. A constructor is used to initialize the state of an object.
  2. A constructor must not have a return type.
  3. The constructor is invoked implicitly.
  4. The Java compiler provides a default constructor if you don’t have any constructor in a class.
  5. The constructor name must be same as the class name.

Method :

  1. A method is used to expose the behavior of an object.
  2. A method must have a return type.
  3. The method is invoked explicitly.
  4. The method is not provided by the compiler in any case.
  5. The method name may or may not be same as class name.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What are the four pillars of OOP?

A
  1. Inheritance
  2. Polymorphism
  3. Encapsulation
  4. Abstraction
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

What is Inheritance, what is the idea behind inheritance, & what does Inheritance represent?

A
  1. 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).
  2. 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.
  3. Inheritance represents the IS-A relationship which is also known as a parent-child relationship.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

What are the two advantages of Inheritance?

A
  1. For Method Overriding (so runtime polymorphism can be achieved).
  2. For Code Reusability.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

Define the term class, as used in inheritance

A

Class: A class is a group of objects which have common properties. It is a template or blueprint from which objects are created.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

Define the term sub class/child class, as used in inheritance

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

Define the term Super Class/Parent Class, as used in inheritance

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

Define the term reusability, as used in inheritance

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q
class Subclass-name extends Superclass-name  
{  
   //methods and fields added here 
}

In the above example, what does the extends keyword indicate?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q
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?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Q

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

A
  1. Single (ex. class b -> class a)
  2. Multilevel (ex. class c -> class b -> class a)
  3. Hierarchical (ex class b & class c -> class a)
  4. Multiple (ex class C -> Class a & Class b)
  5. Hybrid (ex class d -> class b & class c -> class a)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
26
Q

How is multiple & hybrid inheritance supported in Java programming?

A

Through interface only

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
27
Q
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?

A

Single Inheritance

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
28
Q
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?

A

Multilevel Inheritance

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
29
Q
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?

A

Hierarchical Inheritance

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
30
Q

Why is multiple inheritance not supported in Java?

A

To reduce the complexity and simplify the language, multiple inheritance is not supported in java.

31
Q
class A{  
void msg(){System.out.println("Hello");}  
}  
class B{  
void msg(){System.out.println("Welcome");}  
}  
class C extends A,B{  //suppose if it were  
 public static void main(String args[]){  
   C obj=new C();  
   obj.msg();  //Now which msg() method would be invoked?  
}  
} 

Explain the above example as it relates to why multiple inheritance is not supported in Java

A

Consider a scenario where A, B, and C are three classes. The C class inherits A and B classes. If A and B classes have the same method and you call it from child class object, there will be ambiguity to call the method of A or B class.

Since compile-time errors are better than runtime errors, Java renders compile-time error if you inherit 2 classes. So whether you have same method or different, there will be compile time error.

32
Q

What is polymorphism?

A

Polymorphism is the ability of an object to take on many forms

33
Q

When does the most common use of polymorphism in OOP occur?

A

The most common use of polymorphism in OOP occurs when a parent class reference is used to refer to a child class object.

34
Q

When is a Java object considered to be polymorphic?

A

Any Java object that can pass more than one IS-A test is considered to be polymorphic. In Java, all Java objects are polymorphic since any object will pass the IS-A test for their own type and for the class Object.

35
Q

List the three bullet points about Method overloading from the study guide

A
  1. If a class has multiple methods having same name but different in parameters, it is known as Method Overloading.
  2. If we have to perform only one operation, having same name of the methods increases the readability of the program.
  3. Suppose you have to perform addition of the given numbers but there can be any number of arguments, if you write the method such as a(int,int) for two parameters, and b(int,int,int) for three parameters then it may be difficult for you as well as other programmers to understand the behavior of the method because its name differs.
36
Q

What are the two ways to overload the method in java?

A
  1. By changing the number or arguments

2. By changing the data type

37
Q
class Adder{  
static int add(int a,int b){return a+b;}  // 2 arguments
static int add(int a,int b,int c){return a+b+c;}  //3 arguments
}  
class TestOverloading1{  
public static void main(String[] args){  
System.out.println(Adder.add(11,11));  
System.out.println(Adder.add(11,11,11));  
}} 

In the example above how many methods have been created, what is the functionality of the first method?
What is the functionality of the second method?

In the example above what type of methods are we creating, and why are we doing this?

A

In the above example, we have created two methods, first add() method performs addition of two numbers and second add method performs addition of three numbers.

In the above example, we are creating static methods so that we don’t need to create instance for calling methods.

38
Q
class Adder{  
static int add(int a, int b){return a+b;}  // 2 arguments of int data type
static double add(double a, double b){return a+b;}  // 2 arguments of double data type 
}  
class TestOverloading2{  
public static void main(String[] args){  
System.out.println(Adder.add(11,11));  
System.out.println(Adder.add(12.3,12.6));  
}} 

In the above example we have created two methods that differ in _____ type.
What does the first method do?
What does the second method do?

Note: Method overloading is not possible by changing the _________ type of the method

A

Method Overloading - Changing data type of arguments

In the above example, we have created two methods that differs in data type. The first add method receives two integer arguments and second add method receives two double arguments.

Note : Method overloading is not possible by changing the return type of the method.

39
Q
class TestOverloading4{  
public static void main(String[] args){System.out.println("main with String[]");}  
public static void main(String args){System.out.println("main with String");}  
public static void main(){System.out.println("main without args");}  
}  

Refer to above example, fill in the blanks

Yes, by method overloading. You can have any number of ________ methods in a ______ .
But ______ calls _____ method which receives string array as arguments only.

A
Overloading the main() method in Java
class TestOverloading4{  
public static void main(String[] args){System.out.println("main with String[]");}  
public static void main(String args){System.out.println("main with String");}  
public static void main(){System.out.println("main without args");}  
}  
Yes, by method overloading. You can have any number of main methods in a class by method overloading. But JVM calls main() method which receives string array as arguments only.
40
Q

What is method overriding in Java?

A
  1. If subclass (child class) has the same method as declared in the parent class, it is known as method overriding in Java.
  2. In other words, If a subclass provides the specific implementation of the method that has been declared by one of its parent class, it is known as method overriding.
41
Q

What are the two uses for method overriding?

A
  1. Method overriding is used to provide the specific implementation of a method which is already provided by its superclass.
  2. Method overriding is used for runtime polymorphism
42
Q

What are the three rules for method overriding?

A
  1. The method must have the same name as in the parent class
  2. The method must have the same parameter as in the parent class.
  3. There must be an IS-A relationship (inheritance).
43
Q
class ABC{
   //Overridden method
   public void disp()
   {
     System.out.println("disp() method of parent class");
   }     
}
class Demo extends ABC{
   //Overriding method
   public void disp(){
     System.out.println("disp() method of Child class");
   }
   public void newMethod(){
      System.out.println("new method of child class");
   }
   public static void main( String args[]) {
  ABC obj = new ABC();
  obj.disp();
  ABC obj2 = new Demo(); // Covariance with reference types
  obj2.disp();
   }
}

Fill in the blanks

In the above example, we have defined the ______ method in the subclass as defined in the parent class but it has some specific implementation. The name and parameter list of the methods are the same, and there is ______ relationship between the classes.

A

Example for Method Overriding

In the above example, we have defined the disp() method in the subclass as defined in the parent class but it has some specific implementation. The name and parameter list of the methods are the same, and there is IS-A relationship between the classes

44
Q

Can a static method be overridden?

A

No, a static method cannot be overridden. It can be proved by runtime polymorphism, so we will learn it later.

It is because the static method is bound with class whereas instance method is bound with an object. Static belongs to the class area, and an instance belongs to the heap area.

45
Q

Method overloading vs Method Overriding

List the five main bullet points for each

A

Method Overloading :

  1. Method overloading is used to increase the readability of the program.
  2. Method overloading is performed within class.
  3. In case of method overloading, parameter must be different.
  4. Method overloading is the example of compile time polymorphism.
  5. In java, method overloading can’t be performed by changing return type of the method only. Return type can be same or different in method overloading. But you must have to change the parameter.

Method Overriding :

  1. Method overriding is used to provide the specific implementation of the method that is already provided by its super class.
  2. Method overriding occurs in two classes that have IS-A (inheritance) relationship.
  3. In case of method overriding, parameter must be same.
  4. Method overriding is the example of run time polymorphism.
  5. Return type must be same or covariant in method overriding.
46
Q

What is encapsulation in Java?

What is an example of this?

A

A process of wrapping code and data togethter into a single unit, for example, a capsule which is mixed of several medicines.

47
Q

How can we create a fully encapsulated class in Java?

A

We can create a fully encapsulated class in Java by making all the data members of the class private. Now we can use setter and getter methods to set and get the data in it.

48
Q

What is an example of a fully encapsulated class?

A

The Java Bean

49
Q

What are the four advantages of encapsulation?

A
  1. By providing only a setter or getter method, you can make the class read-only or write-only. In other words, you can skip the getter or setter methods.
  2. It provides you the control over the data. Suppose you want to set the value of id which should be greater than 100 only, you can write the logic inside the setter method. You can write the logic not to store the negative numbers in the setter methods.
  3. It is a way to achieve data hiding in Java because other class will not be able to access the data through the private data members.
  4. The encapsulate class is easy to test. So, it is better for unit testing.
50
Q
package com.revature;  
public class Student{  
//private data member  
private String name;  
//getter method for name  
public String getName(){  
return name;  
}  
//setter method for name  
public void setName(String name){  
this.name=name  
}  
}
//A Java class to test the encapsulated class.  
package com.revature;  
class Test{  
public static void main(String[] args){  
//creating instance of the encapsulated class  
Student s=new Student();  
//setting value in the name member  
s.setName("vijay");  
//getting value of the name member  
System.out.println(s.getName());  
}  
}
Fill in the blanks
The above is an example of \_\_\_\_\_\_\_\_ of a class called \_\_\_\_\_\_\_ that has only one \_\_\_\_\_\_\_ with it's \_\_\_\_\_ & \_\_\_\_\_\_ methods
A

Example of Encapsulation

The above is an example of encapsulation of a class called Student that has only one field with its setter and getter methods.

51
Q
//A Java class which has only getter methods.  
public class Student{  
//private data member  
private String college="AKG";  
//getter method for college  
public String getCollege(){  
return college;  
}  
} 
Fill in the blanks
The above class is an example of a \_\_\_\_\_\_\_ class because it has only a \_\_\_\_\_\_\_ to access the college name. If the user tries to change the value of the college name, a \_\_\_\_\_\_\_\_\_  is rendered.
A

Read-Only Class

The above class is an example of a Read-Only class because it has only a getter to access the college name. If the user tries to change the value of the college name, a compile-time error is rendered.

52
Q
//A Java class which has only setter methods.  
public class Student{  
//private data member  
private String college;  
//getter method for college  
public void setCollege(String college){  
this.college=college;  
}  
} 

Fill in the blanks

The above class is an example of a _____ class because it has only ______ to change the value of the college name and cannot _____ the college name. Even if tried to access outside of this class a ________ is displayed only because the variable is declared as _______.

A

Write-Only Class

The above class is an example of a Write-Only class because it has only setters to change the value of the college name and cannot read the college name. Even if tried to access outside of this class a compile-time error is displayed only because the variable is declared as private.

53
Q

What are the two types of modifiers in java?

A

There are two types of modifiers in java: access modifiers and non-access modifiers.

54
Q

The access modifiers in java specifies _________ of a _______, ______, _____ or ______.

A

The access modifiers in java specifies accessibility (scope) of a data member, method, constructor or class.

55
Q

What are the types of java access modifiers?

A
  1. private
  2. default
  3. protected
  4. public
56
Q

There are many non-access modifiers such as ____,

_____, _____, ______, ______, _______, etc.

A

There are many non-access modifiers such as static, abstract, synchronized, native, volatile, transient etc.

57
Q

Specify yes or no for each of the following types of access modifiers - private, default, protected, public

  1. within class?
  2. within package?
  3. outside package by subclass only?
  4. outside package?
A
Private:
1. Within class: yes
2. Within package: No
3. Outside package by subclass only: No
4. Outside package: No
Default
1. Within Class: Yes
2. Within package: Yes
3. Outside package by subclass only: No
4. Outside package: No
Protected
1. Within class: Yes
2. Within package: Yes
3. Outside package by subclass only: Yes
4. Outside Package: No
Public
1. Within Class: Yes
2. Within Package: Yes
3. Outside Package by subclass only: Yes
4. Outside Package: Yes
58
Q
class A{  
protected void msg(){System.out.println("Hello java");}  
}  
public class Simple extends A{  
void msg(){System.out.println("Hello java");} // Error because Class Simple method msg() is    more restrictive that Class A method msg() 
 public static void main(String args[]){  
   Simple obj=new Simple();  
   obj.msg();  
   }  
}

The above code is an example of -_____

A

Access Modifiers with Method Overriding

If you are overriding any method, overridden method (i.e. declared in subclass) must not be more restrictive.

59
Q

What is Abstraction?

A

Abstraction is a process of hiding the implementation details and showing only functionality to the user.

60
Q

Abstraction shows only ______ to the user and hides ______ provide an example

A

Another way, it shows only essential things to the user and hides the internal details, for example, sending SMS where you type the text and send the message. You don’t know the internal processing about the message delivery.

61
Q

Abstraction lets you focus on _____ instead of ______

A

Abstraction lets you focus on what the object does instead of how it does it.

62
Q

What is Generalization?

A

Generalization is the process of extracting shared characteristics from two or more classes, and combining them into a generalized superclass. Shared characteristics can be attributes, associations, or methods.

63
Q

As it relates to generalization shared characteristics can be _____, _______, or _______.

A

Shared characteristics can be attributes, associations, or methods.

64
Q

Provide an example of Generalization

A

Eg : The classes Piece of Luggage and Piece of Cargo partially share the same attributes. From a domain perspective, the two classes are also very similar. During generalization, the shared characteristics are combined and used to create a new superclass Freight . Piece of Luggage and Piece of Cargo become subclasses of the class Freight. Therefore the properties that are common to the classes Piece of Luggage and Piece of Cargo are placed in the superclass Freight - Identification, Weight and ID-Number are those properties.

See diagram in study guide under OOP Pillars > Abstraction > Generalization

65
Q

What is specialization?

A

Specialization means creating new subclasses from an existing class. If it turns out that certain attributes, associations, or methods only apply to some of the objects of the class, a subclass can be created.

66
Q

Expand on the example of generalization as it relates to specialization

A

In the previous example for Generalization, we saw that the classes Piece of Luggage and Piece of Cargo shared similar properties that were placed in a superclass called Freight. When it comes to Specialization, if there is a property that is only applicable to a specific subclass, such as Degree of Hazardousness, that property is placed in the class Piece of Cargo where-in this class also has all the properties of the Freight class with the concept of Generalization.

See diagram in study guide under OOP Pillars > Abstraction > Specialization

67
Q

What are the two ways to achieve abstraction in java?

A

There are two ways to achieve abstraction in java

  1. Abstract class (0 to 100%)
  2. Interface (100%)
68
Q

What is an abstract class?

A

A class which is declared as abstract is known as an abstract class

69
Q

What sort of methods can an abstract class have?

A

An abstract class can have abstract and non-abstract methods

70
Q

An abstract class needs to be _____ & its _____ implemented. It cannot be _______.

A

It needs to be extended and its method implemented. It cannot be instantiated.

71
Q

What are the five key points to remember regarding an abstract class?

A

Some points to remember :

  1. An abstract class must be declared with an abstract keyword.
  2. It can have abstract and non-abstract methods.
  3. It cannot be instantiated.
  4. It can have constructors and static methods also.
  5. It can have final methods which will force the subclass not to change the body of the method.
72
Q

What is an abstract Method?

What would an abstract method look like in a line of code?

A

A method which is declared as abstract and does not have implementation is known as an abstract method.

example below of code

abstract void printStatus();

//no method body and abstract

73
Q
abstract class Bike{  // Abstract class 
  abstract void run();  // Abstract method
}  
class Honda4 extends Bike{  
void run(){System.out.println("running safely");}  
public static void main(String args[]){  
 Bike obj = new Honda4();  
 obj.run();  
}  
} 

The above code is an example of _____ class with an ______ method

A
Example of Abstract Class with an Abstract Method
abstract class Bike{  // Abstract class 
  abstract void run();  // Abstract method
}  
class Honda4 extends Bike{  
void run(){System.out.println("running safely");}  
public static void main(String args[]){  
 Bike obj = new Honda4();  
 obj.run();  
}  
}