Inheritance Error Program
1:-
class Demo
{
void animal ()
{
System.out.println("it is parent class");
}
}
class childDemo { // Ager yah pe ham parent class ko inherat karge to error nahi ayga
void Dog()
{
System.out.println("this is a child class");
}
public static void main(String arg[])
{
childDemo s1= new childDemo();
s1.animal();
s1.Dog();
}
}
Output
C:\java>javac Testing.java
Testing.java:27: error: cannot find symbol
s1.animal();
^
symbol: method animal()
location: variable s1 of type childDemo
1 error
C:\java>
yha pe error isliya aya hai ki ham child demo ka obj banye hai uske ander ham animal mathod ko call kar rhi hai isliya ye error find hua hai
or is program me inherate nahi kiya gaya hai parent class se
2:-
class Demo
{
void animal ()
{
System.out.println("it is parent class");
}
}
class childDemo { // Ager yah pe ham parent class ko inherat karge to error nahi ayga
void Dog()
{
System.out.println("this is a child class");
}
public static void main(String arg[])
{
Demo s1= new Demo();
s1.animal();
s1.Dog();
}
}
C:\java>javac Testing.java
Testing.java:28: error: cannot find symbol
s1.Dog();
^
symbol: method Dog()
location: variable s1 of type Demo
1 error
C:\java>
3:-
class Demo
{
void animal ()
{
System.out.println("it is parent class");
}
}
class childDemo extends Demo{
void Dog()
{
System.out.println("this is a child class");
}
public static void main(String arg[])
{
Demo s1= new Demo();
s1.animal();
s1.Dog();
}
}
C:\java>javac Testing.java
Testing.java:28: error: cannot find symbol
s1.Dog();
^
symbol: method Dog()
location: variable s1 of type Demo
1 error
C:\java>
yaha pr error aya hai ki Demo class ka obj create kar diya hai
ager mai childDemo ka obj create kar dige to error nahi ayga
4:-
class Demo
{
static void method ()
{
System.out.println("hello world");
}
}
class Demo2 extends Demo {
static void method2 ()
{
System.out.println(" hello2");
}
public static void main(String args[])
{
Demo.method(); // ye method to call ho jaye ga
Demo.method2(); // but ye nahi ho payega kuki Demo method ke ander
method2 difine nahi hai
}
}
C:\java>javac Testing.java
Testing.java:20: error: cannot find symbol
Demo.method2();
^
symbol: method method2()
location: class Demo
1 error
C:\java>
5:-
class Demo
{
static void method () // yha pe static hai
{
System.out.println("hello world");
}
}
class Demo2 extends Demo {
void method2 () // but yah pe static nahi hai isliya error aya ahi
{
System.out.println(" hello2");
}
public static void main(String args[])
{
Demo2.method();
Demo2.method2();
}
}
C:\java>javac Testing.java
Testing.java:20: error: non-static method method2() cannot be referenced from a
static context
Demo2.method2();
^
1 error
C:\java>
3:-
class Demo{
void show()
{
System.out.println("error find");
}
}
class mytest{
public static void main(String args[])
{
Demo obj = new Demo();
obj.show();
}
}
C:\java>javac Testing.java
C:\java>java Demo (ye progarm compile hua Run nahi hua kuki main method ka class call nahi kiya)
Error: Main method not found in class Demo, please define the main method as:
public static void main(String[] args)
or a JavaFX application class must extend javafx.application.Application
Comments
Post a Comment