Open kchaitanya-training opened 8 months ago
package keywords;
//abstract class
public abstract class Shape {
String shapeName;
final float pi = 3.14f;
Shape(String shapeName){
this.shapeName = shapeName;
}
// abstract method
abstract float calculateArea();
}
package keywords;
public class Circle extends Shape{
float radius;
Circle(String name, float radius){
super(name); //calling parent constructor with argument
this.radius = radius;
}
float calculateArea() {
return (pi*(radius*radius));}
}
package keywords;
public class Main {
public static void main(String[] args) {
float radius = 8.7f;
float area;
Circle shape1 = new Circle("Circle", radius);
area = shape1.calculateArea();
System.out.println(shape1.shapeName + " area with radius " + radius + " is: " + area);
}
}
// Sample output: Circle area with radius 8.7 is: 237.6666
public class Sample { String name; String email;
Sample(String name, String email){
this.name = name;
this.email = email;
}
void print() {
System.out.println("Name: "+ name);
System.out.println("Email: "+ email);
}
}
public class SampleInfo extends Sample { String Id;
SampleInfo( String name, String email, String Id){
super(name, email);
this.Id = Id;
}
void identity() {
System.out.println("Id: "+ Id);
}
void print() {
super.print();
System.out.println("Id: "+ Id);
}
}
public class Main { public static void main(String[] args) { SampleInfo S = new SampleInfo("Menda", "ex@takeo","80623"); System.out.println(S.name); System.out.println(S.email); S.identity(); S.print();
}
}
Output ex@takeo Id: 80623 Name: Menda Email: ex@takeo Id: 80623
//use of super and this keyword class World {
public void continents() {
System.out.println("There are 7 contenents with 195 countries in the world.");
}
} class Continents extends World{
String Continent = "North America";
public void continents() {
super.continents();
System.out.println("There are so many countries in each contenent.");
System.out.println("United States is in "+ this.Continent +".");
}
} public class Earth{
public static void main(String[] args) {
World ourEarth = new Continents();
ourEarth.continents();
}
}
OUTPUT
There are 7 contenents with 195 countries in the world. There are so many countries in each contenent. United States is in North America.
public class Test {
int number = 0;
String name = "Sajan Gurung";
void print(){
System.out.println(number + " " + name);
}
Test (){
System.out.println("This is the parent class constructor.");
}
Test (int a){
this.number = a;
System.out.println("Now the value of number is: " + number);
}
void sum(int num){
System.out.println(number + num);
}
}
public class TestDemo extends Test {
TestDemo(){
super(100);
System.out.println("This is child class constructor.");
}
void sum(int a){
System.out.println(a+34687);
super.sum(a); // super calls the sum method from the Test class which is parent class
}
public static void main(String[] args) {
TestDemo t = new TestDemo();
t.sum(100);
}
}
public class ThisKeyA {