Scenario-,3
Note: Add main method wherever necessary.
Each and every scenario presented here are for getting good understanding on OOP(Object Oriented Programming) through Java.
Scenario #1:
Expected Understanding: Access Modifiers, Single Inheritance, getter methods, Constructor Overloading
1) Create a Class named “Trainer”.
– Have default instance variables String dept, institute
– Assign values – “Java”, “Payilagam” to them
– Have private instance variable int salary
– Assign 10000 as value for salary.
– Create getter method for salary.
– Have instance method training() with void as return data type
– Add a print statement inside training() method
- Add main method [public static void main(String[] args)] – Have instance named as ‘trainerKumar’ and pass “CSE”, “payilagam” as arguments to it. – Handle above line with matching Constructor.
2) Create a sub class “SQLTrainer” under “Trainer”.
– Have main method in it.
– Create instance ram for this class
– Handle with proper super class constructor
– Access parent class instance variables
– Call parent class instance method training()
– Access salary using getter method in parent class
package B15; public class Trainer { String dept = "java"; String institute = "payilagam"; private int salary = 10000; Trainer(String dept, String intitute) { this.dept = dept; this.institute = institute; } public static void main(String[] args) { Trainer trainerkumar = new Trainer("cse", "payilagam"); String a = trainerkumar.traing(); trainerkumar.salary(); System.out.println(a); } public void salary() { System.out.println("salary = " + salary); } public String traing() { return dept + " " + institute; } }
output:
salary = 10000
cse payilagam
package B15; public class SQLtrainer extends Trainer { SQLtrainer(String dept, String intitute) { super(dept, intitute); } public static void main(String[] args) { SQLtrainer ram = new SQLtrainer("cse111", "srit"); String a = ram.traing(); System.out.println(a); ram.salary(); System.out.println(ram.dept); System.out.println(ram.institute); } }
output
cse111 payilagam
salary = 10000
cse111
payilagam
Scenario #2:
Expected Understanding: Interface, Class, static variables, dynamic binding
1) Create an interface called ‘Actor’
– Have variables boolean makeUpRequired
– Assign ‘true’ value for ‘makeUpRequired’
– Have variable String address.
– Assign value as “Chennai” to address.
– Have void methods act(), dance(), sing()
2) Create class named as ActorSivakumar with main method
– implement interface ‘Actor’ to this class.
– Give your own definitions for methods from interface
– Have static variable String address.
– Assign value to address as “Coimbatore”.
– Have instance method ‘speaking()’ with void return data type.
– Create instance for ActorSivakumar as below
ActorSivakumar as = new ActorSivakumar(65, “Audi Car”)
– Handle with proper Constructor
– Access all the methods from ActorSivakumar class
– Access variable address and print the value
– Create another instance of interface ‘Actor’ using dynamic binding approach
Actor ac = new Sivakumar();
– Handle with proper Constructor
– Access methods in ActorSivakumar class.
– Access variable address using ‘ac’ intance and print the value
– Observe and note down the difference between two instances
package B15; public class Trainer { String dept = "java"; String institute = "payilagam"; private int salary = 10000; Trainer(String dept, String intitute) { this.dept = dept; this.institute = institute; } public static void main(String[] args) { Trainer trainerkumar = new Trainer("cse", "payilagam"); String a = trainerkumar.traing(); trainerkumar.salary(); System.out.println(a); } public void salary() { System.out.println("salary = " + salary); } public String traing() { return dept + " " + institute; } }
package B15; public class SQLtrainer extends Trainer { SQLtrainer(String dept, String intitute) { super(dept, intitute); } public static void main(String[] args) { SQLtrainer ram = new SQLtrainer("cse111", "srit"); String a = ram.traing(); System.out.println(a); ram.salary(); System.out.println(ram.dept); System.out.println(ram.institute); } }
Output:
sivakumar is acting
sivakumar is speaking
sivakumar is dancing
sivakumar is singing
65
Audi car
sivakumar is acting
sivakumar is dancing
sivakumar is singing
coimbatore
chennai
true
Scenario #3:
Expected Understanding: Abstraction, Inheritance, return keyword, Method Arguments, Constructor
1) Create an abstract class named ‘SmartPhone’
– Add the below abstract methods
– int call(int seconds)
– void sendMessage()
– void receiveCall()
– Add non abstract method void browse()
– print ‘SmartPhone browsing’ inside browse() method definition
– Have constructor as below.
public SmartPhone()
{
System.out.println(“Smartphone under development”);
}
2) Create class called ‘FactoryDemo’ as abstract subclass of SmartPhone
– Add the below abstract methods
– void verifyFingerPrint()
– void providePattern()
– Add non abstract method void browse()
– print ‘Factory Demo browsing’ inside browse() method definition
– Add variable boolean isOriginalPiece and assign ‘false’ as value.
– Add static variable int price and set value as 0.
3) Create class called ‘Samsung’ with main method as sub class of FactoryDemo.
– Add unimplemented methods
– Add static variable int price and set value as 5000.
– Create instance for Samsung class called sam
– Access browse() method using sam instance.
– Access price variable using sam instance.
– Observe which method is called and note down.
package B15; public interface Actor { boolean makeupRequired = true; String address = "chennai"; void act(); void dance(); void sing(); }
package B15; public class ActorSivakumar implements Actor { static String address = "coimbatore"; int num; String car; public ActorSivakumar(int num, String car) { this.num = num; this.car = car; } public static void main(String[] args) { ActorSivakumar as = new ActorSivakumar(65, "Audi car"); Actor ac = new ActorSivakumar(55, "benz car");// dynamic binding as.act(); as.speaking(); as.dance(); as.sing(); as.sell(); // ac.speaking();//dynamic binding ac.act(); ac.dance(); ac.sing(); // ac.sell();//dynamic binding System.out.println(ActorSivakumar.address); System.out.println(Actor.address); System.out.println(as.makeupRequired); } public void sell() { System.out.println(num + "\n" + car); } public void speaking() { System.out.println("sivakumar is speaking"); } public void act() { System.out.println("sivakumar is acting"); } public void dance() { System.out.println("sivakumar is dancing"); } public void sing() { System.out.println("sivakumar is singing"); } }
package B15; public abstract class Smartphone { public Smartphone()// constructor { System.out.println("Smartphone under development"); } public abstract int call(int second); public abstract void sendMessage(); public abstract void receivecall(); public void browse() { System.out.println("smartphone browsing"); } }
Output:
Smartphone under development
FactoryDemo browsing
fingerprint
providepattern
receivecall
b = 100
pric = 5000
The above is the detailed content of Scenario-,3. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Troubleshooting and solutions to the company's security software that causes some applications to not function properly. Many companies will deploy security software in order to ensure internal network security. ...

Solutions to convert names to numbers to implement sorting In many application scenarios, users may need to sort in groups, especially in one...

Field mapping processing in system docking often encounters a difficult problem when performing system docking: how to effectively map the interface fields of system A...

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

Start Spring using IntelliJIDEAUltimate version...

Conversion of Java Objects and Arrays: In-depth discussion of the risks and correct methods of cast type conversion Many Java beginners will encounter the conversion of an object into an array...

Detailed explanation of the design of SKU and SPU tables on e-commerce platforms This article will discuss the database design issues of SKU and SPU in e-commerce platforms, especially how to deal with user-defined sales...

How does the Redis caching solution realize the requirements of product ranking list? During the development process, we often need to deal with the requirements of rankings, such as displaying a...
