When you start learning ABCD of Java,surely you will see
this(Inheritance) keyword . If you come from C++,then you know what is
inheritance. However there is some difference between in inheritance in
c++ and inheritance in Java. When we proceed through this blog, i will
explain .
First of all, inheritance means accessing properties of parent class in child class.Inheritance can be acheived by using extends Class_Name in front of the child class . Ex. class Child extends Parent .
Some important points about inheritance in Java
public class Inheritance {
public static void main(String[] args) {
// TODO Auto-generated method stub
Child c=new Child();
c.printParent();
c.printChild();
}
}
class Parent
{
void printParent()
{
System.out.println("In parent");
}
}
class Child extends Parent
{
void printChild()
{
System.out.println("In child");
}
}
If you think any points are missed the please let me know.
First of all, inheritance means accessing properties of parent class in child class.Inheritance can be acheived by using extends Class_Name in front of the child class . Ex. class Child extends Parent .
Some important points about inheritance in Java
- You can only access public ,default,protected member variables and methods in child class.
- You can not access private members of parent class.
- Class constructor does not inherits.
- You can not inherit member variables.
- You can override (Overriding and overloading) the methods of parent class in child class.
- Java does not support multiple inheritance as it leads to diamond problem.
- You can use reference of parent class and can create object of child class.
public class Inheritance {
public static void main(String[] args) {
// TODO Auto-generated method stub
Child c=new Child();
c.printParent();
c.printChild();
}
}
class Parent
{
void printParent()
{
System.out.println("In parent");
}
}
class Child extends Parent
{
void printChild()
{
System.out.println("In child");
}
}
If you think any points are missed the please let me know.
No comments:
Post a Comment