Java Assignments

You might also like

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 4

Java Assignment#20

1.What is super keyword in java?


A. Super keyword in java is used to refer parent class Properties in child
class,whenever we override parent class implementation in child class and to call
parentclass methods in child class we use super keyword.

2. Write the simple java program on super keyword usage.


A. public class ClassA {
public void getText(String Xyz,int Money){
Xyz="Balance";
Money=10000;
System.out.println("This is my "+Xyz+" "+Money);
}
public static void main(String[] args){
ClassA map=new ClassA();
String Name = "Srisaiteja";
int Balance = 100000;
map.getText(Name,Balance);
}
}

public class ClassB extends ClassA{

public static void main (String[] args) {


ClassB obj= new ClassB();
obj.getText(null, 0);
obj.oneMoreMethod();
}
public void oneMoreMethod() {
super.getText(null, 0);
}
public void getText(String car,int Price ) {
car="TATA Motors";
Price=1000000;
System.out.println("This is my "+car+" "+Price);
}
}

Output:
This is my TATA Motors 1000000
This is my Balance 10000

3.Make use of super class method which is overriden in child class using super
keyword.
A. public class ClassA {
public void getText(String Xyz,int Money){
Xyz="Balance";
Money=10000;
System.out.println("This is my "+Xyz+" "+Money);
}
public static void main(String[] args){
ClassA map=new ClassA();
String Name = "Srisaiteja";
int Balance = 100000;
map.getText(Name,Balance);
}
}

public class ClassB extends ClassA{


public static void main (String[] args) {
ClassB obj= new ClassB();
obj.getText(null, 0);
obj.oneMoreMethod();
}
public void oneMoreMethod() {
super.getText("Ariel",100);
String name = "Ariel";
int Price=100;
System.out.println("Price of "+name+" is "+Price);
}
public void getText(String car,int Price ) {
car="TATA Motors";
Price=1000000;
System.out.println("This is my "+car+" "+Price);
}
}

4. Call super class constructor from child class


A. public class Person {
String name;
int age;
public Person(String Television) {
Television="Sony";
System.out.println("This is Parameterized Constructor in Parent
Class");
}

public class Student extends Person{


String college;
public Student(int number) {
super("Television");
number=570;
System.out.println("This is parametrized in child class for "
+number);
}

public static void main(String[] args) {


Student led= new Student(1000);
}
}

5. When we have parent and child relationship between two classes and when we
create the child class object(without parameters), which constructor will be called
(from child class and parent class )? Write the simple example to demonstrate on
this step by step.
A. public class Person {
String name;
int age;
public Person() {
System.out.println("This is no Parameterized Constructor in Parent
Class");
}

public class Student extends Person{


String college;
public Student() {
System.out.println("This is no parametrized in child class");
}

public static void main(String[] args) {


Student led= new Student();
}
}

6.When we have parent and child relationship between two classes and when we create
the child class object(with parameters), which constructor will be called(from
child class and parent class )? Write the simple example to demonstrate on this
step by step.
A.
public class Person {
String name;
int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
System.out.println("This is Two Parameterized Constructor");
}
public String getName() {
return name;
}

public void setName(String name) {


this.name = name;
}

public int getAge() {


return age;
}

public void setAge(int age) {


this.age = age;
}
}

public class Student extends Person{


String college;
public Student(String name, int age) {
super(name, age);
this.college = college;
}

public String getCollege() {


return college;
}

public void setCollege(String college) {


this.college = college;
}
public static void main(String[] args) {
Student abc = new Student("John Doe", 20);
System.out.println("Name: " + abc.getName());
System.out.println("Age: " + abc.getAge());
}
}

You might also like