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!

Multithreading is important in modern programming because it can improve program responsiveness and resource utilization and handle complex concurrent tasks. JVM ensures the consistency and efficiency of multithreads on different operating systems through thread mapping, scheduling mechanism and synchronization lock mechanism.

Java's platform independence means that the code written can run on any platform with JVM installed without modification. 1) Java source code is compiled into bytecode, 2) Bytecode is interpreted and executed by the JVM, 3) The JVM provides memory management and garbage collection functions to ensure that the program runs on different operating systems.

Javaapplicationscanindeedencounterplatform-specificissuesdespitetheJVM'sabstraction.Reasonsinclude:1)Nativecodeandlibraries,2)Operatingsystemdifferences,3)JVMimplementationvariations,and4)Hardwaredependencies.Tomitigatethese,developersshould:1)Conduc

Cloud computing significantly improves Java's platform independence. 1) Java code is compiled into bytecode and executed by the JVM on different operating systems to ensure cross-platform operation. 2) Use Docker and Kubernetes to deploy Java applications to improve portability and scalability.

Java'splatformindependenceallowsdeveloperstowritecodeonceandrunitonanydeviceorOSwithaJVM.Thisisachievedthroughcompilingtobytecode,whichtheJVMinterpretsorcompilesatruntime.ThisfeaturehassignificantlyboostedJava'sadoptionduetocross-platformdeployment,s

Containerization technologies such as Docker enhance rather than replace Java's platform independence. 1) Ensure consistency across environments, 2) Manage dependencies, including specific JVM versions, 3) Simplify the deployment process to make Java applications more adaptable and manageable.

JRE is the environment in which Java applications run, and its function is to enable Java programs to run on different operating systems without recompiling. The working principle of JRE includes JVM executing bytecode, class library provides predefined classes and methods, configuration files and resource files to set up the running environment.

JVM ensures efficient Java programs run through automatic memory management and garbage collection. 1) Memory allocation: Allocate memory in the heap for new objects. 2) Reference count: Track object references and detect garbage. 3) Garbage recycling: Use the tag-clear, tag-tidy or copy algorithm to recycle objects that are no longer referenced.


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

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

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

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

Dreamweaver CS6
Visual web development tools