508
508
[ abstraction] ( https://en.wikipedia.org/wiki/Abstraction_(software_engineering) ) , and
509
509
[ encapsulation] ( https://en.wikipedia.org/wiki/Encapsulation_(computer_programming) ) .
510
510
511
- * ** What is the difference between a constructor and a method?**
511
+ * ** What is the difference between a constructor and a method?**
512
+ - The name of the constructor is same as that of the class name, whereas the name of the method can be anything.
513
+ - There is no return type of a constructor.
514
+ - When you make an object of a class, then the constructor of that class will be called automatically.
515
+ But for methods, we need to call it explicitely.
516
+ - Constructors can't be inherited but you can call the constructor of the parent class by calling ` super() ` .
512
517
- Constructor and a method they both run a block of code but the difference is in calling them.
513
- - Constructor gets called automatically when we create an object of that class, we cannot call it explicitly
514
- though we can call parent class constructor using 'super' keyword.
515
- - We call method directly using their name.
518
+ - We can call method directly using their name.
516
519
- Constructor Syntax -
517
520
public class SomeClass{
518
521
SomeClass(parameter_list){
519
522
...
520
523
}
521
524
...
522
525
}
523
- Note:
524
- Constructor name is the same as class name
525
- Constructor does not have return type
526
-
526
+ - Note:
527
+ Constructor name is the same as class name
528
+ Constructor does not have return type
529
+
527
530
- Method Syntax
528
531
public class SomeClass{
529
532
public void someMethod(parameter_list){
@@ -532,12 +535,6 @@ Constructor does not have return type
532
535
// call method
533
536
someMethod(parameter_list)
534
537
}
535
-
536
- - Name of the method can be anything.
537
- - When you make an object of a class, then the constructor of that class will be called automatically.
538
- But for methods, we need to call it explicitely.
539
- - Constructors can't be inherited but you can call the constructor of the parent class by calling `super()`.
540
-
541
538
* ** Differences between abstract classes and interfaces?**
542
539
- An abstract class, is a class that contains both concrete and abstract methods
543
540
(methods without implementations). An abstract method must be implemented by the abstract class
0 commit comments