I am Hirun Yapa. I live in Maharagama. Actually I like ICT, I would like to post articles about Java and many more about ICT. Hope my articles will be useful to you all.
I have explained you in a past article about Inheritance. But here I'm going to talk about Multiple Inheritance. Multiple Inheritance is extending one class to many classes. But its not allowed in Java. As a solution OOP gave us Interfaces. Interface is like a class (That means it has all the features that a class have) But Interface is not a class. We can use Interfaces in /java if we need to use multiple inheritance. Java has no limit for multiple inheritance using Interfaces. but when we are inheriting Interfaces we use the keyword implements not extends Creating an Interface Ex :- 1) Interface Saman { } We create an interface in the way we are creating a class. Inheriting an Interface to a Class Ex :- 2) class A { } ...
Refer to this code sample. class Monkey { Monkey(){ } } class Man extends Monkey{ Man(){ Super(); } } Super = Super class The keyword super is used to call the constructor in the super class when you are in the sub class. When we are using the super keyword in a constructor of the sub class, the constructor of super class will run in the constructor of the sub class. We can access methods and variables in the super class using the keyword super.
When we're coding, we will meet methods that we can't apply codes, So we can name them as Meaningless Methods . We can't store meaningless methods in an ordinary way. In that situation, We have to make Abstract methods using the keyword "Abstract". We can't store Abstract methods in an ordinary class. Therefore, we have to make the class also an abstract class using Abstract keyword. We can store abstract methods only in abstract classes. But we can store any method in an abstract class. The important thing is, we cannot create objects in an abstract class . Refer to the code sample given below, Abstract class Vehicle{ abstract void park (); } Abstract class Vehicle { Abstract void park (); void reverse (){ } } There is a code inside the meaningless method highlighted with Orange like , Ex :- class Vehicle { void park (){ } } The part highlighted in blue is a meaningless ...
Well done Hirun
ReplyDelete